Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Java oddity

Author: Robert Hyatt

Date: 20:03:53 09/09/02

Go up one level in this thread


On September 09, 2002 at 10:04:12, Ed Panek wrote:

>long x;
>double y;
>
>y = 2.01;
>x = (long)(y * 1000);
>
>
>guess what x equals? ...
>
>No, not 2010, but 2009 !!!!!!!!!!!!!!!
>
>Ed


Of course.  .1, .01, ..., .00000001 have _no_ exact representation in
IEEE floating point form.  They turn into .099999997 roughly. for .1...

Multiply that by 1000, then truncate the result and you come up short by
just a hair...

Moral is to not use floating point like that.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.