This question already has answers here:
Why is division in Ruby returning an integer instead of decimal value?
(7 answers)
Closed 8 years ago.
I was trying to write a small program in Ruby, and I ran into the following problem: Ruby doesn't seem to be able to group numbers in parentheses.
For example:
puts (2 - 0) / 10
# prints out 0
There is obviously a flaw in the logic here. What should be happening is that (2 - 0) gets evaluated first (according to the order of operations) and then (2 - 0) should get divided by 10.
Does grouping with parentheses in Ruby not work? By the way, I'm using 2.1.2.
You're doing integer division without realizing it. 2 / 10 does equal 0 in integer division.
Try instead running this:
puts (2 - 0) / 10.0
# prints out 0.2
You will probably get an answer more like what you're expecting. The reason is that by changing 10 to 10.0, you coerce the operation into floating point division.
Related
This question already has answers here:
How do I use floating-point arithmetic in bash?
(23 answers)
Bash Division Keeps giving 0 [duplicate]
(1 answer)
Closed 1 year ago.
I am attempting to calculate a grade based off of two variables with given values, and set it to a new variable in my shell script. For example,
x=25
y=75
Using the arithmetic expansion operator I have made many attempts to do so such as:
grade=$(($x*($y / 100)))
grade=$(($x * ($y / 100)))
y=$(($y / 100))
grade=$(($x*$y))
& many more desperate attempts, but none seem to work. Any tips as to what I am missing/not understanding? I have looked at several examples/other similar questions but they do not match my sitaution.
Thank you.
This question already has answers here:
How to generate a random decimal number from 0 to 3 with bash?
(2 answers)
Generate random float number in given specific range of numbers using Bash
(3 answers)
Closed 2 years ago.
I need random numbers to three decimal places like:
0.624, 1.035, 0.869, 1.324
What I am using is:
"0.$(($RANDOM%1000+500))"
However, in this case, all the values less than 1 are correct (i.e. 0.917,0.917,0.917,0.855), but the values greater than 1 are incorrect (i.e 0.1195,0.14340.1434) as I append 0. At the beginning of the random number produced.
Thanks.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
The universal advice to avoid floating point errors in ruby is to use BigDecimal. I must be overlooking something, because I think I've found a case where BigDecimal math is returning an error where a Float does not:
using Float gives the correct answer of 2.75:
> 50.0 * 0.6 / 360.0 * 33
=> 2.75
using BigDecimal gives the incorrect answer of 2.74999999:
> BigDecimal("50") * BigDecimal("0.6") / BigDecimal("360") * BigDecimal("33")
=> #<BigDecimal:7efe74824c80,'0.2749999999 999999989E1',27(36)>
Someone please tell me what I'm missing here?
Let's simplify your example, and use this one instead:
BigDecimal(1) / BigDecimal(3) * BigDecimal(3)
# => #<BigDecimal:19289d8,'0.9999999999 99999999E0',18(36)>
How did it get there?
BigDecimal(1) / BigDecimal(3)
# => #<BigDecimal:1921a70,'0.3333333333 33333333E0',18(36)>
BigDecimal does not provide rational numbers, so when you divide 1 by 3, you get 0, following by a lot of 3s. A lot, but not infinitely many. When you then multiply that by 3, you will get 0 followed by equally many 9s.
I believe you misread the BigDecimal's advertisement (although I am not sure it is anywhere advertised as the solution to floating point errors). It just provides arbitrary precision. It is still a floating point number. If you really want exact numbers when dividing numbers, you might take a look at Rational class:
(Rational(50) * Rational(0.6) / Rational(360) * Rational(33)).to_f
# => 2.75
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
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.