转换度和毫秒 [英] Convert between Degree and Miliseconds

查看:294
本文介绍了转换度和毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道从度数到毫秒转换的公式,反之亦然。它可以这样实现:

pre $ $ $ $ $ $ $ $ $ $保护函数decimal_to_milisecond($ dec){
if(!empty($ dec )){
$ vars = explode(。,$ dec);
if(count($ vars)== 2){
$ deg = $ vars [0];
$ tempma =0。。$ vars [1];
$ tempma = $ tempma * 3600;
$ min = floor($ tempma / 60);
$ sec = $ tempma - ($ min * 60); ((((($ deg * 60)+ $ min)* 60 + $ sec)* 1000));
return round
}
else else false;
}否则返回false;


函数milisecond_to_decimal($ sec){
if(!empty($ sec)){
$ s = $ sec / 1000;
$ d =(int)($ s / 3600);
$ s = $ s%3600;
$ m =(int)($ s / 60);
$ s%= 60;
$ ret = substr($ d +((($ m * 60)+($ s))/ 3600),0);
}否则返回null;
return $ ret;





场景:我将度数转换为毫秒,并继续从毫秒转换为度数。转换后的值与原始值有一定差异。我想要的价值是准确的原始价值以及。例如:

  $ lat =1284146; 
$ long =503136198;
$ lat1 = milisecond_to_decimal($ lat);
$ long1 = milisecond_to_decimal($ long);

$ result1 = decimal_to_milisecond($ lat1);
$ result2 = decimal_to_milisecond($ long1);
var_dump($ result1,$ result2);

输出是float(1284000)和float(503136000)

减少差异的另一种方法是由度和毫秒之间的转换引起的?这里有360度(经度),每度60分钟,每分钟60次,每次1000毫秒第二。因此,最多

  360 * 60 * 60 * 1000毫秒= 1 296 000 000毫秒

适合31位,所以这个想法将首先转换为一个整数,然后在整数中执行尽可能多的操作。 / p>

请注意,如果您使用单精度浮点数,您将得到一个24位有效数字,并且在十分之一秒( log2 360 * 60 * 60 * 10)约为 23.6 )。



建议以双精度(53位有效数字)存储结果。

编辑

我的建议是一次执行所有的转换,如果有一种方法来使用双精度代表 $ decimaldegrees (我不知道php足以说明所以如下所示:
$ b $ $ $ $ $ $ $ $ $ $ $ = $($)$($)$($) );

然后,如果您想分解成DMS(但是这些变量不在您的代码中使用):

  $ ms = $ millis%1000; 
$ sec =($ millis / 1000)%60;
$ min =($ millis /(60 * 1000))%60;
$ = =($ millis /(60 * 60 * 1000))%360;

仔细看看你的代码,看起来你是先分开小数部分 $ tempma =0。。$ vars [1];

如果您使用十进制表示字符串,那么可以工作,因为在这种情况下, ( log2(60 * 60 * 1000))大约是 21.8 )。所以开头可以被替换为:

$ $ $ $ $ $ $ $ $ = $ vars [0];
$ frac =0。。$ vars [1];
$ millis =(int)(round($ frac *(60 * 60 * 1000)));
$ millis = deg + millis;

从你给出的输出的例子来看,这听起来像问题来自其他转换 milisecond_to_decimal ,大概是因为某些算术运算是用整数运算来执行的,所以丢弃了毫秒。

再一次,我没有知道php足够,但不会 $ s = $ s%3600; 实际上在(int)(%s)因此放弃毫秒?

您将需要找到相当于C函数 fmod modf < code $。

再一次,如果有双精度的方法可以一次执行所有的操作:

  $ decimaldegrees =((double)($ millis))/(60 * 60 * 1000); 

如果您无法访问双精度,则无法安全地重构,单精度没有足够的位...

您需要在单独的字符串部分上进行操作,在小数部分关注前导零

无论如何,我强烈建议执行单元测试,即分别测试你的两个函数,这样你就可以更好地理解哪些是有效的,什么不是。

I know the formular for conversion from Degree to Miliseconds and vice-versa. It can be implemented like that:

 protected function decimal_to_milisecond($dec) {
        if (!empty($dec)) {         
            $vars = explode(".",$dec);
            if (count($vars) == 2) {
                $deg = $vars[0];
                $tempma = "0.".$vars[1];
                $tempma = $tempma * 3600;
                $min = floor($tempma / 60);
                $sec = $tempma - ($min*60);
                return round((((($deg * 60) + $min) * 60 + $sec) * 1000));
            } 
            else return false;
        } else return false;
    }

 function milisecond_to_decimal($sec) {
        if (!empty($sec)) {
            $s = $sec / 1000;
            $d = (int)($s / 3600);
            $s = $s % 3600;
            $m = (int)($s / 60);
            $s %= 60;       
            $ret = substr($d+((($m*60)+($s))/3600),0);
        } else return null;
        return $ret;
    }

Scenario: I convert from Degree to Miliseconds and continue converting from Miliseconds to Degree. The converted value has some difference with original value. I want the value is exact as the orginal value as well. For example:

$lat = "1284146";
$long = "503136198";
$lat1 = milisecond_to_decimal($lat);
$long1 = milisecond_to_decimal($long);

$result1 = decimal_to_milisecond($lat1);
$result2 = decimal_to_milisecond($long1);
var_dump($result1, $result2);

The output is float(1284000) and float(503136000)

Is there another way to reduce difference is caused by conversion between degree and miliseconds? Thanks so much.

解决方案

There are 360 degrees (longitude), 60 minutes per degree, 60 secondes per minute, 1000 milliseconds per second. So at most

360*60*60*1000 milliseconds = 1 296 000 000 milliseconds

That fits well on 31 bits, so the idea would be to first convert to an integer, and perform as much operations as possible in integer.

Note that if you use single precision floating point, you'll get a 24 bits significand and will loose accuracy under 1 tenth of second (log2(360*60*60*10) is about 23.6).

I would recommend to store results in double precision (53 bits significand).

EDIT

What I was suggesting is to perform the conversion all at once, if there is a way to use double precision for representing $decimaldegrees (I don't know php enough to tell so), something like:

$millis = (int)( round( $decimaldegrees * (60*60*1000) ) );

Then if ever you want to decompose into DMS (but these variables are not used in your code):

$ms  =  $millis % 1000;
$sec = ($millis / 1000) % 60;
$min = ($millis / (60*1000)) % 60;
$deg = ($millis / (60*60*1000)) % 360;

Ater a deeper look at you code, it seems you are separating the decimal part first $tempma = "0.".$vars[1];
That could work if you work on the decimal representation string, because in this case that fits well even on a single precision float (log2(60*60*1000) is about 21.8). So the beginning could be replaced with:

$deg  = (int) $vars[0];
$frac = "0.".$vars[1];
$millis = (int)( round( $frac * (60*60*1000) ) );
$millis = deg + millis;

From the example of output you gave, it sounds like the problem comes from the other conversion milisecond_to_decimal, presumably because some arithmetic operation is performed with integer arithmetic and thus discards the milliseconds.

Once again, I don't know php enough, but wouldn't $s = $s % 3600; in fact operate on (int)(%s) and thus discard the milliseconds?
You would need to find something equivalent to C function fmod or modf.

Once again, you could do all operation at once if there is a way to do it in double precision:

$decimaldegrees = ((double)($millis)) / (60*60*1000);

If you don't have access to double precision, you can't recompose safely, single precision does not have enough bits...

You would need to operate on separate string parts, caring of leading zeros in fraction part

Anyway, I strongly suggest to perform unit tests, that is to test your two functions separately, this way you'll get a better understanding of what works and what not.

这篇关于转换度和毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆