Monday, May 9, 2011

Floating point on bc, command line calculator

bc is a great commandline calculator on Linux and *nix like OSes. But the default setting on the Linux machine here at Tufts gives:

7/16 = 0

Obviously, this is not what I was looking for. After a little googling, I found the following.

Quoting from the bc online manual:

"There are two attributes of numbers, the length and the scale. The length is the total number of significant decimal digits in a number and the scale is the total number of decimal digits after the decimal point. For example, .000001 has a length of 6 and scale of 6, while 1935.000 has a length of 7 and a scale of 3."

To set the scale (say 4 digits), just type:

scale = 4

No comments: