addition of two ruby floats gives unexpected result [duplicate] - ruby

This question already has answers here:
Float precision in ruby
(2 answers)
Closed 8 years ago.
When I add two floats in an irb console, the result is not as expected
10.43 + 4.56 # should be 14.99
But the actual result is
irb(main):001:0> 10.43+4.56
=> 14.989999999999998
What causes this?

This is expected behavior. Floats are not guaranteed precision because, in short, computers are binary systems (as of 2013) and cannot correctly represent fractional values. This is why you get "unexpected" results, when in reality this is how it will work until there is a non-binary machine that can correctly represent fractional values.

Related

Ruby and mathematical problems [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 4 years ago.
I was trying to solve a mathematical problem:
37.9 - 6.05
Ruby gives me:
37.9 - 6.05
#=> 31.849999999999998
37.90 - 6.05
#=> 31.849999999999998
37.90 + 6.05
#=> 43.949999999999996
Why am I getting this?
In a nutshell, computers have a problem working with real numbers and use
floating point representation to deal with them. Much in the same way you can only represent 256 numbers with 8 bits for natural numbers, you can only represent a fixed amount of numbers with 64 bits for real numbers. For more details on this, read this http://floating-point-gui.de/ or google for "floating point arithmetic".
How should i deal with that?
Never store currency values in floating point variables. Use BigDecimal or do your calculations in cents using only integer numbers.
use round to round your floats to a user friendly length. Rounding errors will occur, especially when adding up a lot of floats.
In SQL systems use decimal data type, or use integers and divide them by a constant factor in the UI (say you need 3 decimal digits, you could store 1234 as integer and display 1.234.

Why is the Ruby Math.log function returning the wrong values? [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 6 years ago.
Given 3^5 (3**5 or 3*3*3*3*3) = 243.
Why/how does Ruby do the following:
n = 243
Math.log(n,3)
returns:
4.999999999999999
Math.log(243)/Math.log(3)
returns:
4.999999999999999
3**Math.log(n,3)
returns:
242.99999999999977
That last one really gets me. Something is going wrong here? I'm missing something? Both?
Thanks!
Values are not wrong, this is because floating point precision is limited.

Multiplying by 100 [duplicate]

This question already has answers here:
ruby floating point errors
(3 answers)
Closed 7 years ago.
Can someone explain to me why:
33.8 * 100 # => 3379.999999999995
but
23.8 * 100 # => 2380.0
Floating-point numbers cannot precisely represent all real numbers, and floating-point operations cannot precisely represent true arithmetic operations, this leads to many surprising situations.
I advise to read: https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
You may want to use BigDecimal to avoid such problems.

Why does ruby round like this? [duplicate]

This question already has answers here:
Why is division in Ruby returning an integer instead of decimal value?
(7 answers)
Closed 8 years ago.
Hi I have the following question:
When I put (1+7/100) in ruby it gives 1.
This is very strange because normally this is how I calculate a 7% increase in Excel.
But when I put (1+7.0/100) it gives me 1.07 which is the correct answer I expected.
Why is ruby doing this? And how do you solve this issue in your calculations in ruby?
This has nothing to do with rounding.
Ruby does division differently on float than it does on an integer.
If you divide integers, you will always get an integer result.
If you divide with floats (or a mixture of integer and float), you will always get a float result.
Remember your order of operations, too. Ruby is going to handle the division before it handles the addition.
7/100 = 0 so 1+0 = 1
7.0/100 = 0.07 so 1+0.07 = 1.07

Ruby subsecond time addition incorrect [duplicate]

This question already has answers here:
ruby floating point errors
(3 answers)
Closed 10 years ago.
I am having an issue with adding floating point numbers to time in ruby. In the example below, how is t2 not equal to 2013-02-15T01:17:17.996000000?
irb(main):094:0> t1.strftime("%Y-%m-%dT%H:%M:%S.%N")
=> "2013-02-15T01:16:47.785000000"
irb(main):095:0> t2 = t1 + 30.211
=> 2013-02-15 01:17:17 -0700
irb(main):096:0> t2.strftime("%Y-%m-%dT%H:%M:%S.%N")
=> "2013-02-15T01:17:17.995999999"
This is mainly about floating-point arithmetic.
Try 1.4 - 1.3 in the ruby shell.
What Every Computer Scientist Should Know About Floating-Point Arithmetic
This has been discussed before. You should check the following thread:
ruby floating point errors

Resources