Sinus(SIN)计算PHP和MySQL / Doctrine不同

时间:2016-09-23 09:22:32

标签: php math doctrine

我正在使用脚本来计算坐标之间的距离。 但是,总距离应该是8 KM,这在PHP中是正确的,在SQL / Doctrine中是3000+。

当我在PHP和SQL / Doctrine中输出一个单独的计算时;这是结果:

PHP:SIN(53.18882060000001 * 0.0174532925199433)= 0.8006144758170634

SQL:SIN(53.18882060000001 * 0.0174532925199433)= 0.8006144758170636

正如你所看到的;差异是 0.0000000000000002

看起来很小,但我正在使用它;不同的是一条路:

PHP:

(ACOS(SIN(53.18882060000001 * 0.0174532925199433)* SIN(53.1655606 * 0.0174532925199433)+ COS(53.18882060000001 * 0.0174532925199433)* COS(53.1655606 * 0.0174532925199433)* COS(5.556485699999939 * 0.0174532925199433 - 5.43966 * 0.0174532925199433))* 6371)

8.204067263544099 KM

SQL /学说:

(ACOS(SIN(53.18882060000001 * 0.0174532925199433)* SIN(53.1655606 * 0.0174532925199433)+ COS(53.18882060000001 * 0.0174532925199433)* COS(53.1655606 * 0.0174532925199433)* COS(5.556485699999939 * 0.0174532925199433 - 5.43966 * 0.0174532925199433))* 6371)

3296.761195521706 KM

如果我将PHP的精度设置为16(默认为14)并不重要,总距离保持不变。

我该如何解决这个问题?我在一个带有Doctrine扩展的Symfony 3项目中使用它来支持SIN,COS和ACOS。

更多计算:

PHP:

sin1:sin(53.18882060000001 * 0.0174532925199433)= 0.8006144758170634

sin2:sin(53.1655606 * 0.0174532925199433)= 0.8003711646618459

cos1:cos(53.18882060000001 * 0.0174532925199433)= 0.5991798236858188

cos2:cos(53.1655606 * 0.0174532925199433)= 0.5995047946245639

cos3:cos(5.556485699999939 * 0.0174532925199433 - 5.43966 * 0.0174532925199433)= 0.9999979212542568

SQL:

sin1:sin(53.18882060000001 * 0.0174532925199433)= 0.8006144758170636

sin2:sin(53.1655606 * 0.0174532925199433)= 0.800371164661846

cos1:cos(53.18882060000001 * 0.0174532925199433)= 0.3809279376844091

cos2:cos(53.1655606 * 0.0174532925199433)= 0.3820183930527858

cos3:cos(5.556485699999939 * 0.0174532925199433 - 5.43966 * 0.0174532925199433)= 1.568757332266098

这是我尝试使用Symfony运行的Doctrine查询:

$lat = 53.18882060000001;
$lng = 5.556485699999939;
$tlat = 53.1655606;
$tlng = 5.43966;

$sql = "SELECT  L,
                    L.latitude AS lat,
                    L.longitude AS lng,

                    SIN(1) AS tmp,
                    SIN($lat*$rad) AS sin1,
                    SIN($tlat*$rad) AS sin2,
                    COS($lat*$rad) AS cos1,
                    COS($tlat*$rad) AS cos2,
                    COS($lng*$rad - $tlng*$rad) AS cos3,
                    (SIN($lat*$rad) * SIN($tlat*$rad) + COS($lat*$rad) * COS($tlat*$rad)) AS test,
                    (sin($lat*$rad) * sin($tlat*$rad) + cos($lat*$rad) * cos($tlat*$rad) * COS($lng*$rad - $tlng*$rad)) AS test1,
                    ACOS(SIN($lat*$rad) * SIN($tlat*$rad) + COS($lat*$rad) * COS($tlat*$rad) * COS($lng*$rad - $tlng*$rad)) AS test2,
                    ACOS(0.5174636941643757) AS test3,

                    (ACOS(SIN($lat*$rad) * SIN($tlat*$rad) + COS($lat*$rad) * COS($tlat*$rad) * COS($lng*$rad - $tlng*$rad)) * 6371) as dist
            FROM    AppBundle:Location AS L
            ";
    return $this->getEntityManager()->createQuery($sql)->getResult();