Calculating percentage returns integer [duplicate] - ruby

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)

Related

Generating a random number between 0.5 and 1.5 in bash [duplicate]

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.

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.

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.

Does grouping numbers with parentheses in Ruby not work? [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.
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.

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

Resources