1. Bug in math.e?

When running som test with round in std/math.e i got some weird result. 
This was what i got round(22/7,3) = 3 
I may be wrong in expecting 3.143? 
 
The following code gives the expexted result. 

function ROUND(atom n, integer s = 1) 
    atom r = n * power(10, s) + .5 
    r = floor(r) 
    return r / power(10, s) 
end function 
new topic     » topic index » view message » categorize

2. Re: Bug in math.e?

Thomas said...


When running som test with round in std/math.e i got some weird result.
This was what i got round(22/7,3) = 3
I may be wrong in expecting 3.143?

It's not well documented but to get the result you were expecting you need to code ...

round(22/7, 1000) 

The precision parameter is in terms of powers-of-ten.

Value Precision Result
1.2345 1 1
1.2345 10 1.2
1.2345 100 1.23
1.2345 1000 1.234
new topic     » goto parent     » topic index » view message » categorize

3. Re: Bug in math.e?

It's not well documented but to get the result you were expecting you need to code ... 
 
OK Thank you smile 
 

new topic     » goto parent     » topic index » view message » categorize

4. Re: Bug in math.e?

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 
new topic     » goto parent     » topic index » view message » categorize

5. Re: Bug in math.e?

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 

The output looks correct to me. The differences look like the rounding errors inherent in using floating point numbers.

Matt

new topic     » goto parent     » topic index » view message » categorize

6. Re: Bug in math.e?

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 message » categorize

7. Re: Bug in math.e?

The round() function controls the precision of a number, not how it will be displayed. If you wish to display a specific number of digits you should use a function like sprintf() or format(). These functions will give the results you expect.

new topic     » goto parent     » topic index » view message » categorize

8. Re: Bug in math.e?

sprintf should work just fine. I am still getting use to eu but am making a lot of progress.

Thanks for everyones input!

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu