64

It looks like bc doesn't support float operations. When I do echo 1/8 | bc it gets me a zero. I checked the manual page bc (1), but it doesn't even mention float, so I wonder if it's supported?

Mike Pierce
  • 737
  • 1
  • 6
  • 23
daisy
  • 53,527
  • 78
  • 236
  • 383
  • This [question](http://unix.stackexchange.com/questions/66609/round-truncate-digit-in-string-in-zsh-or-with-external-tool) and in particular the accepted [answer](http://unix.stackexchange.com/a/66755/13136) might be interesting to you. – Emanuel Berg Mar 06 '13 at 01:21

4 Answers4

84

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 down to that number of digits after the decimal scale (== fixed precision).

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

goldilocks
  • 86,451
  • 30
  • 200
  • 258
  • Here's an example of how to use scale: `echo "scale=4;22/7" | bc` - Result: 3.1428, a number marginally better than pi. – SurpriseDog May 25 '23 at 23:19
37

man page says:

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

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

Hauke Laging
  • 88,146
  • 18
  • 125
  • 174
1
  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 (value of the scale variable/register) 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 follows:

        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,as)
    
  3. Originally, bc was a preprocessor of dc. Now, on many systems, bc is a standalone program.

See also the POSIX specification of bc.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
cd9
  • 11
  • 2
0

Alternative path on MacOS (for short) is using -e option with -l option.

  -e  expr  --expression=expr

      Run "expr" and quit. If multiple expressions or files (see below) are
      given, they are all run before executing from stdin.

Example: bc -e 1/8 -l

  • 2
    The `-l` option to load the math functions and to set `scale` to 20 has already been mentioned in other answers. The `-e` option seems to be an extension found only in some implementations of `bc` (not on Busybox systems, for example). – Kusalananda Aug 22 '23 at 05:52
  • 1. This path shorter, than path with `echo`; 2. This path for MacOS version of `bc`. – Volkov Maxim Aug 29 '23 at 11:38