I would like to do the following operation in my script:
1 - ((m - 20) / 34)
I would like to assign the result of this operation to another variable. I want my script use floating point math. For example, for m = 34:
results = 1 - ((34 - 20) / 34) == 0.588
Use this script open this file with favorite editor like:
Then paste this code:
Now chmod it to 755:
Now use it:
In your script you can use this:
I know this is an old thread, but this seemed like a fun project to tackle without using
bc
or invoking recursion. I'm sure it can be improved, but this maxed out my skill.Example output:
You could use the
bc
calculator. It will do arbitrary precision math using decimals (not binary floating point) if you set increeasescale
from its default of 0:The
-l
option will load the standard math library and default the scale to 20:You can then use printf to format the output, if you so choose:
Bash does not do floating point math. You can use awk or bc to handle this. Here is an awk example:
To assign the output to a variable:
Teach bash e.g. integer division with floating point results:
Output:
gives the result.
Example:
Enter a & b value as 10 3, you get 3.3333333333
If you want to store the value in another variable then use the code
It also gives the same result as above. Try it...