双重问题? [英] double type problem?

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

问题描述

大家好,

我有两个不同的参数money,并在c#代码中花费
钱正,花负值.

代码和mysql表中的两个参数类型为double.

我写了代码,然后从表名中选择总和(结果),其中id = xxxxx

如果您的有价值的钱>花费
,则会返回真实的受款人
前48的钱花了-2结果46,但

如果ı尝试金钱46花费-46金钱=花费结果必须为0零,但此选择返回7.105427357601E-15为什么?

请帮帮我.

在此先感谢您.

Hi All,

I have two different parameter money and spend in c# code
money positive, spend negative value.

Two parameter type double in code and in mysql table.

I wrote ın code behınd select sum(result)as total from tablename where id=xxxxx

This return true valueee if your value money>spend

For ex 48 money spend -2 result 46 but

If ı try money 46 spend -46 money=spend result must be 0 zero but this select returns 7.105427357601E-15 whyy?

Please help me.

Thanks in Advance.

推荐答案

不要使用double货币.货币始终是几美分的整数(或美分或您选择的货币中最小的分度数),除非您正在开办投资银行或居住在津巴布韦,否则长整数很容易就够大了(〜4× 10 18 ),并且精度比双精度值都要好.

将浮点差与零进行比较是一个众所周知的领域,该体系结构引入的精度问题会破坏数学逻辑结果.
Don''t use double for currency. Currency is always an integer number of pennies (or cents or whatever the smallest division in your currency of choice is), and unless you''re running an investment bank or live in Zimbabwe, a long int is easily big enough (~4×1018) and has better precision than a double anyway.

Comparing floating point differences to zero is a well known area where precision issues introduced by the architecture spoil mathematically logical outcomes.


应为0,但声称为7.105E-15.他们彼此相距不远.
这使我想到了Type double具有的表示问题:
它存储数字作为2的不同次幂的总和.我们称其为对偶".我们通常以10的不同幂的总和(十进制")来考虑.

在两种表示形式之间进行转换会引入错误.只要您不使用无数个数字,1/10(十进制)就没有与偶数完全相等的数字.

但是 IIRC,问题应该严格限于非整数.

以上述46为例,您的问题真的出现了吗?
然后请完全忽略此帖子.

还是将其转换为0.046?
然后,另一种数据类型可能会有所帮助.在C#中,该名称为decimal.也许,MySQL中也有类似的东西.
It should be 0, but claims to be 7.105E-15. They aren''t far from each other.
That makes me think of a representation problem that Type double has:
It stores numbers as sum of different powers of 2. Let''s call it "dual". We usually think in sums of different powers of 10 ("decimal").

Converting between the two representations can introduce errors. 1/10 (decimal) doesn''t have an exact equivalent in dual as long as you don''t use an infinite number of digits.

But IIRC, problems should strictly stick to non-integral numbers.

Does your problem really arise with the mentioned 46 as an example?
Then please ignore this post completely.

Or is it converted somewhere to 0.046?
Then another data type could help. In C#, that would be decimal. Maybe, there is something similar in MySQL.


这篇关于双重问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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