你能否建议 - 总结两个双打 [英] Could you please suggest - sum two doubles

查看:57
本文介绍了你能否建议 - 总结两个双打的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

double dr = 495494.3948402 + 3740.9729811 == 499235.36782130005(!)正确是499235.3678213



double dr = 0.1111111 + 0.2222222 == 0.3333333(!)正确


如何获得成功?



我的尝试:



double dr = 0.1111111 + 0.2222222 == 0.3333333(!)正确

解决方案

double是数字的近似值,数学很快但不保证100%准确。如果你需要精确度使用十进制



  decimal  dr =  495494  .3948402M +  3740  .9729811M; 


具有Double和Float(= Single)的算术是按设计不精确的。如果你需要精确的浮点运算,那么使用Decimal,就是它的设计目的。



与Decimal相比,Double和Float的优点是计算速度更快并且可以表示的数字范围更大。


大多数浮点数无法在计算机系统上精确表示,执行操作通常会导致其他错误。请参阅舍入错误 - 维基百科,免费的百科全书 [ ^ ]和浮动 - 点指南 - 每个程序员应该知道的浮点运算 [ ^ ]。

double dr = 495494.3948402 + 3740.9729811 == 499235.36782130005 (!) correct is 499235.3678213

double dr = 0.1111111 + 0.2222222 == 0.3333333 (!) correct

how to owercome?

What I have tried:

double dr = 0.1111111 + 0.2222222 == 0.3333333 (!) correct

解决方案

double is an approximation of the number, the maths is quick but not guaranteed to be 100% accurate. If you need accuracy use decimal

decimal dr = 495494.3948402M + 3740.9729811M;


Arithmetic with Double and Float (=Single) is inexact "by design". If you need exact floating point arithmetic then use Decimal, that's what it's been designed for.

The advantages of Double and Float in comparison with Decimal are that the computation is faster and that the range of numbers that can be represented is larger.


Most floating point numbers can not be represented exactly on computer systems and performing operations usually leads to additional errors. See Round-off error - Wikipedia, the free encyclopedia[^] and The Floating-Point Guide - What Every Programmer Should Know About Floating-Point Arithmetic[^].


这篇关于你能否建议 - 总结两个双打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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