Division in script and floating-point

You could use the bc calculator. It will do arbitrary precision math using decimals (not binary floating point) if you set increease scale from its default of 0:

$ m=34
$ bc <<< "scale = 10; 1 - (($m - 20) / 34)"
.5882352942

The -l option will load the standard math library and default the scale to 20:

$ bc -l <<< "1 - (($m - 20) / 34)"
.58823529411764705883

You can then use printf to format the output, if you so choose:

printf "%.3f\n" "$(bc -l ...)"

Leave a Comment