本文介绍了为什么双值55.68743被转换为55.6874299999?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF方法中,我要计算之间经纬度点的距离。距离计算方法采用双值作为参数。所以,当我送纬度值= 55.68743,.NET将其转换为55.6874299999我得到错误的距离。

I have a wcf method in which I have to calculate the distance between to latitude longitude point.the distance calculator method takes double value as argument.so when I send latitude value =55.68743, .net converts it to 55.6874299999and I get wrong distance.

为什么值转换?不会有人知道我怎么能解决呢?

Why the value is converting?? does anybody knows how can I solve it??

下面是一些code ...

Here is some code...

public double distance(double lat1, double lon1, double lat2, double lon2)
    {
        double theta = lon1 - lon2;
        double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
        dist = Math.Acos(dist);
        dist = rad2deg(dist);
        dist = dist * 60 * 1.1515;
        dist = dist * 1.609344;

        return (dist);
    }
    public double deg2rad(double deg)
    {
        return (deg * Math.PI / 180.0);
    }
    public double rad2deg(double rad)
    {
        return (rad / Math.PI * 180.0);
    }

在上面的code,我计算距离的两个点。LAT1 = 55.68743,lon1 = 12.50400LAT2 = 55.68758,lon2 = 12.50403

In the above code, I am calculating distance of two points.lat1=55.68743,lon1=12.50400lat2=55.68758, lon2=12.50403

当我执行,LAT1的价值转向55.687429999999999和lon1至12.504。

when I execute, value of lat1 turns to 55.687429999999999 and lon1 to 12.504.

推荐答案

这基本上是由于这样的事实,即一台电脑是二进制,而数学的传统理解是小数。

This is basically due to the fact, that a computer is binary whereas your conventional understanding of mathematics is decimal.

在本质上,电脑有没有办法完全 EX pressing 55.68743 在二进制浮点格式,因此使用最接近的可能值 55.6874299999

In essence, the computer has no way of exactly expressing 55.68743 in binary floating point formats and thus uses the nearest possible value 55.6874299999

要比较两个浮点数你减去两个数,检查结果是否是$ P $在门槛pviously aggreed以下。

To compare two floating point numbers you subtract the two numbers and check whether the result is below a previously aggreed upon threshold.

double actual = 55.6874299999;
double expected = 55.68743;
if (Math.Abs(actual- expected) < 0.00001) 
{
    // do something
}

的浮点运算(最常用的)的语义已经正式在 IEEE 754

延伸阅读:

这篇关于为什么双值55.68743被转换为55.6874299999?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 07:33