Блог пользователя ahmad_mamdouh

Автор ahmad_mamdouh, 15 лет назад, По-английски

In most of the courses studied in the college , I was always told to take care of the double precision errors and never to compare two double(float) numbers with the equal operator .
But the problem I faced now is a bit different , let's think of this JAVA snippet of code :


double x = 2.55 ;
double y = x * 100 ;
System.out.println(y) ;

You'll be astonished with the result , you'll see " 254.99999999999997 " .
The problem will propagate if you're going to use the integer portion of the result which will yield  " 254 " instead of " 255 " .

The problem arose due to the infinite double representation inside the computer which can't be accurate as it's not terminating . However , the solution is very easy .
If you added a very small value to the resulted double ( EPS = 1e-9 ) .
you'll find that y = 255.00000000099996 so you'll get the correct result up to the accuracy you adjusted ( i.e 10^-9 accuracy ) .

Some algorithms problems will always give you wrong answer if you don't take care of such gotchas like this one : Transmigration .

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

15 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
I fear there are very few people who would be really astonished with these facts. When you were told beware of comparing two real values (not necessarily "double") you should not only write it down to your copy-book, but also try to think why it is so.

You can find a lot of fun effects with floating-point values by the way.

For example (stolen from Java Language Specification):

double a = SECRET_VALUE_1;
double b = SECRET_VALUE_2;
System.out.println(a == b); // prints "true"
a = 1 / a;
b = 1 / b;
System.out.println(a == b); // prints "false"

Guess these "secret values"!