Dart double division precision

☆樱花仙子☆ 提交于 2021-01-24 05:46:16

问题


What is the correct way to perform this operation?

399.9 / 100

What I would expect to see is

3.999

but the result is

3.9989999999999997

回答1:


The result you see is correct, it's just not what you want.

Doubles are not precise values. The double you get by writing 399.9 is actually the precise value.

399.8999999999999772626324556767940521240234375

That's the closest available double to the exact value 399.9. Any other double is at least as far away from 399.9 as that.

Then you divide by 100. Again, the result is not precise, but the closest double has the exact value

3.99899999999999966604491419275291264057159423828125

That differs from what you would get by writing 3.999, which is the exact value:

3.999000000000000110134124042815528810024261474609375

At every step, the double operations have minimized the error, but because you are doing multiple steps, the final result diverges from the double closest to the mathematical result.

What you need to do depends on what your actual requirements are.

If you want to always calculate with two significant digits, then I'd just multiply my numbers with 100 and do all the operations as integer operations, until the very last division by 100.

If you have an intermediate result and wants to round it to two digits, I'd do what Fy Z1K says:

  result = (result * 100).round() / 100;



回答2:


import 'dart:math';

double roundDouble(double value, int places){ 
    double mod = pow(10.0, places); 
    return ((value * mod).round().toDouble() / mod); 
}

then you would basically get

double num1 = roundDouble(12.3412, 2);
// 12.34

double num2 = roundDouble(12.5668, 2);
// 12.57

double num3 = roundDouble(-12.3412, 2);
// -12.34

double num4 = roundDouble(-12.3456, 2);
// -12.35



回答3:


To make decimal operations you can use the decimal package.

final d = Decimal.parse;
print(d('399.9') / d('100')); // => 3.999


来源:https://stackoverflow.com/questions/60654642/dart-double-division-precision

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!