Re: Bug in math.e?

new topic     » goto parent     » topic index » view thread      » older message » newer message
Anthill said...

Round seems to give me a problem. Is there a way to get the expected output?

 
atom a 
a = 183.4126   
puts(1,to_string(round(a)))      -- got 183, expected 183 
puts(1,to_string(round(a,10)))   -- got 183.400000000000006, expected 183.4 
puts(1,to_string(round(a,100)))  -- got 183.409999999999997, expected 183.42 
puts(1,to_string(round(a,1000))) -- got 183.413000000000011, expected 183.413 

Try this ...

 
atom a 
a = 183.4126   
? round(a,1) 
? round(a,10) 
? round(a,100) 
? round(a,1000) 


The issue you are seeing is that the "to_string" function outputs the number to the maximum number of decimals it can but the "?" statement only outputs a few decimal places.

The problem is that the rounded values cannot be exactly implemented in the IEEE floating point system that Euphoria uses. This can be demonstrated by this code ...

? atom_to_float64( round(a, 1000) ) 
? atom_to_float64( 183.413 ) 

In both cases above, you should get {240,167,198,75,55,237,102,64} which is the IEEE encoding for the nearest number to 183.413.

The rule-of-thumb is only use floating point numbers if you are prepared to accept a margin-of-error in your calculations. If you are working with currency, this is obviously not acceptable, in which case you need to only work with currency values as 'cents' and format them for output using dollars AND cents.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu