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

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

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
  • Проголосовать: не нравится

Автор ahmad_mamdouh, 14 лет назад, По-английски
My Rating didn't change despite I participated in today's contest . Please Recheck it as this is an awful bug.

Полный текст и комментарии »

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