Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart - Subtracting some double values gives wrong result

I have picked two random double numbers:

double a = 7918.52;
double b = 5000.00;

I would expect to get 2918.52 from a - b.
Well, it gives me a result of 2918.5200000000004, which seems odd.

print(a - b); // -> 2918.5200000000004

But if I change double a to 7918.54, I will get the expected result of 2918.54.

Can someone explain to me why some double values result in unexpected rounding issues and others do not?

like image 874
Zoka Avatar asked Jun 16 '19 13:06

Zoka


1 Answers

The reason for this is floating-point arithmetic and the fact that Dart uses the IEEE 754 standard as far as I am concerned.

This happens for all languages that use floating-point arithmetic. You can read through similar questions regarding other programming languages.

General question about floating-point arithmetic in modern programming languages.

like image 55
creativecreatorormaybenot Avatar answered Sep 30 '22 13:09

creativecreatorormaybenot