Questions tagged [bc]

an arbitrary precision calculator language used in shell scripting. Use this tag for questions for programming-related uses of bc; general questions about bc usage and troubleshooting are a better fit at Unix & Linux Stack Exchange.

bc, for basic calculator, is "an arbitrary-precision calculator language" using algebraic infix notation. bc is typically used as either a mathematical scripting language or as an interactive mathematical shell.

Typical Usages

There are two ways bc is typically used.

  • From a Unix command prompt, then interactively entering a mathematical expressions, such as (1 + 3) * 2, which will display 8.

  • From the shell command prompt, where bc reads from standard input. For example $ echo '(1 + 3) * 2' | bc will display 8 in the shell.

While bc can perform calculations to arbitrary precision, its default behavior is to truncate calculations to whole numbers. I.e. entering the expression 2/3 displays 0. This can surprise new bc users. The number of digits that bc displays is determined by a variable scale, which may be changed. For example, running bc interactively, setting scale=7 then entering 2/3 will display .6666666. Starting bc with the -l option loads a math library, setting scale to 20 and loading these math functions

s (x)    The sine of x, x is in radians.
c (x)    The cosine of x, x is in radians.
a (x)    The arctangent of x, arctangent returns radians.
l (x)    The natural logarithm of x.
e (x)    The exponential function of raising e to the value x.
j (n,x)  The bessel function of integer order n of x.

In 1991, POSIX rigorously defined and standardized bc. Two implementations of that standard survive today:

  1. Traditional bc, on Unix and Plan 9 systems.

  2. GNU bc with numerous extensions beyond the POSIX standard, on Linux, etc.

Links & References

281 questions
-1
votes
3 answers

convert large number to spoken english

Converting 'small' numbers to English is not to troublesome. But if you handle BCMath Arbitrary Precision numbers then it can be. Using code from: http://marc.info/?l=php-general&m=99928281523866&w=2 The maximum number seems to be: two billion one…
Yehonatan
  • 1,172
  • 2
  • 11
  • 21
-1
votes
4 answers

more than one source of data to process with bc

I have this file that contains number in a column: [root@server1]# cat numbers.txt 30 25 15 I need to add them together so I do this: [root@autonoc cattests]# cat numbers.txt | paste -s -d+ | bc 70 But after I add them together I need to divide…
-1
votes
1 answer

execvp ("/usr/bin/bc", arg, env)

I have doubts about how to throw in C an execv of bc. How should the parameters be passed (for example: 3, +, 2)? Would it be for a pipe? Or by the argv despite being int? And, if this exec was thrown by the child, would the parent collect that…
ssuarez
  • 1
  • 1
-1
votes
1 answer

Piped input for `bc` division generates random numbers

I've got two files formatted in this way: File1: word token occurence File2: token occurence What I want is a third file with this output: word token occurrence1/occurence2 This is my code: while read token pos count do #get pos counts …
-1
votes
2 answers

How to calculate extra large numbers with bc in bash

I have a file with 1800 lines that look like this 600.76 600.66 700.44 566.66 Ect.. I made a bash script to calculate the mean. Now I first made a variable to count the total column lines like: Lines="$(awk 'END{print NR}' file.txt)" Then another…
theloosegoos
  • 129
  • 1
  • 10
-2
votes
1 answer

How can I use brackets while using bc (Unix basic calculator)?

While working with bc, UNIX basic calculator on my Ubuntu WSL, I found this bizarre behaviour: Prompt> echo (1+2)*3 | bc -l -bash: syntax error near unexpected token `1+2' Prompt> echo (1 + 2) * 3 | bc -l -bash: syntax error near unexpected token…
Dominique
  • 16,450
  • 15
  • 56
  • 112
-2
votes
1 answer

Pipe CURL result to BC

I have a CURL command echo "{cur_format}" | curl -w @- -s -o /dev/null https://example.com Let's say the above command outputs a string of "I waited 1 day". How can I convert "I waited 1 day" to "I waited 24 hours" (i.e. pipe "1 * 24" to the bc…
gye
  • 1,374
  • 3
  • 16
  • 27
-2
votes
3 answers

How to use bc command of perl inside perl script?

I am able to execute the below script on command line. echo "56.8 + 77.7" | bc -l and I get proper output as well but when I try to do the same in my perl/html script and run it in browser it fails. Can you please guide me what's going…
Sumit Raj
  • 113
  • 1
  • 3
  • 10
-3
votes
1 answer

how calculated in bash atan2

hello i want to calcul the distance between me and the ISS so this my code i want to print this sentence : The ISS is currently located at -30.1461, -50.7975: 10010km from us! my code : #!/bin/bash pi =…
TS2IR 2018
  • 27
  • 3
-4
votes
2 answers

Calculate the sum

Let’s imagine we have a budget document: budget.txt The exercise is to calculate the sum column for every product in the month, also to calculate totals in the last row. You may try to do it in a shell. How shall it work? a program/script shall…
Aktar
  • 11
  • 3
-4
votes
4 answers

Syntax error running command bc under bash 4.1

I have a bash script with the following line. The variables start_time and start_files[$i] are floating point numbers. I want to compare them using the command bc as follows: result1=$(bc -l <<< $start_time'>='${start_files[$i]}) When I run the…
user3102314
1 2 3
18
19