I only cast doubles to store them, less space and better precision.
I was writing a rounding function when I discovered this problem.  My round 
function correctly rounded 0.1405 to 0.141, it's only when I multiply the 
result out and cast it for storage that it gets messed up.


"Chris Tutty" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> From: "Jim McGowen" <[EMAIL PROTECTED]>
>> 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
>>
> This is just a rounding issue isn't it?  0.141 * 10000.0 has a good chance
> of
> being resolved at compilation time rather than run time so it's just an
> assignment
> of a precalculated constant where the test * 10000.0 is subject to the
> accuracy
> with which that number can be stored and computed.
>
> To my knowledge casting isn't a good way to obtain an accurate conversion
> of a floating value because you don't have control over issues like
> truncation.
> I think you should be using an explicit conversion function so that
> 1409.99...
> is rounded to 1410 rather than being truncated to 1409.
>
> Chris Tutty
>
> 



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

Reply via email to