43

It looks like bc doesn't support float operations, when I do echo 1/8 | bc it get me a zero.

I checked bc (1), but it doesn't even mention float, so I wonder if it's supported?

56

bc doesn't do floating point but it does do fixed precision decimal numbers. The -l flag Hauke mentions loads a math library for eg. trig functions but it also means

[...] the default scale is 20

scale is one of a number of "special variables" mentioned in the man page. You can set it:

scale=4

Anytime you want (whether -l was used or not). It refers to the number of significant digits used in a decimal. In other words, subsequent solutions will be rounded to that number of digits after the decimal scale (== fixed precision).

The default scale sans -l is 0, meaning rounded to whole numbers.

|improve this answer|||||
29

man page says:

If bc is invoked with the -l option, a math library is preloaded [...]

The comprehensibility of that could be improved, indeed...

|improve this answer|||||
  • 14
    Report it upstream as a bug... – vonbrand Mar 4 '13 at 3:18
0

1) Numbers in bc have a scale. The scale of a number should not be confused with the scale factor. The same world 'scale' is used as a function to query the scale of a number or as a parameter to set the scale factor.

echo "scale=scale(1.1);11/10" | bc will return 1.1

2) The scale factor determines how many digits are kept to the right of the decimal point when doing operations. If s is the current scale factor, sa is the scale of the first operand a, sb is the scale of the second operand b, results are truncated as follow :

    scale of result
a+b     max(sa,sb)
a-b     max(sa,sb)
a*b     min(sa+sb , max(s,sa,sb))
a/b     s
a%b     so that a = b*quotient + remainder; remainder has sign of a
a^b     min(sa×|b|, max(s,sa)); b must be integer
sqrt(a) max(s,sa)

3) At the origin bc was a preprocessor of dc. Now, on many systems, bc is a standalone program.

Source : [https://plan9.io/magic/man2html/1/dc]

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.