How to Use Multiplication and Division When Invoking the Arithmetic Expansion Operator in Bash? [duplicate] - bash

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.

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.

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.

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.

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.

Resources