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

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.

Related

Pascal Lazaruz doesn't do its division properly for some instances [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Why are floating point numbers inaccurate?
(5 answers)
Closed 2 years ago.
program Project1;
var p : real;
const s=439 ;
begin
p:=s/100;
Write(p);
ReadLn;
end.
Output of the above code is expected to be 4.39, but this gives 4.3899999999999997E+000.
Where do these additional 89999999 came from? How to omit this and get the correct answer?
Thanks in advance!
P.S. If we assign s = 433 it gives the expected output as 4.3300000000000001E+000. My issue is, how this division can be number related?
Edit: I want to know a remedy to overcome this inaccuracy. It may be a code line or two, what ever possible to get rid of this issue.

Calculating percentage returns integer [duplicate]

This question already has answers here:
Why is division in Ruby returning an integer instead of decimal value?
(7 answers)
Closed 7 years ago.
I have two integer values dicount_percentage and price. When I run the following command, it always returns 0 rather than the discount value etc.
price*(discount_percentage/100)
How can I fix this?
Cast one of price or discount_percentage to float:
price*(discount_percentage.to_f/100)
price*(discount_percentage/100.0)

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.

addition of two ruby floats gives unexpected result [duplicate]

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.

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