Casting to integer truncates the fraction part and almost never gives the expected result.

If you want to "isolate" the digits you can round with something like:
long y = (long)(x*10000 + 0.5);

For positive integer it will usually gives the expected result.

Like others said, it is all related to limited precision of floating points and the fact that most numbers to not have an exact representation in binary floating point.

Jerome


Jim McGowen wrote:
Found something strange... Looks like Palm OS 5 changes certain numbers multiplying. For example:
  double x = 0.141;
  long y = (long)( x * 10000.0 );

y becomes 1409.

Also:
  double x = 1410.0;
  double test = 0.141;
  double y = test * 10000.0;
  double z = 0.141 * 10000.0;

x = = 1410.0 = = 0100000010010110000010000000000000000000000000000000000000000000 y = = 1410.0 = = 0100000010010110000001111111111111111111111111111111111111111111 z = =1410.0 = = 0100000010010110000010000000000000000000000000000000000000000000

Casting z to a long gives 1409.
Only happens with certain "problem" numbers like this one. Haven't tried it in any other OS version. Is there a way to fix or get around this?

- Jim



--
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/

Reply via email to