Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [bc]

Arbitrary-precision arithmetic language.

Filter by
Sorted by
Tagged with
-1 votes
0 answers
29 views

Why does bc not have easy to use built in support for rounding?

I just wrote a small bash shell script and for the first time in that context I wanted to make a calculation, a division, and discovered bc. My script processes media files and I want to log the ...
d-b's user avatar
  • 1,937
-2 votes
3 answers
95 views

How to make bc handle explicitly positive numbers?

Consider the following: $ echo "-1" | bc -1 $ echo "(1)" | bc 1 $ echo "+1" | bc (standard_in) 1: syntax error Minus sign and round braces are handled correctly but ...
Anton Samokat's user avatar
1 vote
1 answer
33 views

Why does "bc" read my config file on one computer and not another?

I have two old machines, running CentOS 6.5 and CentOS 6.10. Both have bc 1.06.95-1.el6 installed. One reads $HOME/.bc.rc, one does not. Why ? (How??) strace shows near-identical output, except one ...
Andrew Daviel's user avatar
2 votes
1 answer
76 views

Use m4 interactively with bc

I would like to use m4 interactively as a preprocessor for bc. My immediate use case is to add an include capability to bc, but I can foresee other uses in the future. But there is a problem. Here is ...
user448810's user avatar
0 votes
3 answers
56 views

bash array multiplication using bc

I am trying to multiply array values with values derived from the multiplication of a loop index using bc. #!/bin/bash n=10.0 bw=(1e-3 2.5e-4 1.11e-4 6.25e-5 4.0e-5 2.78e-5 2.04e-5 1.56e-5 1.29e-5 1....
csnl's user avatar
  • 35
3 votes
1 answer
332 views

By default, bc interprets any alpha character as a 9

System: Ubuntu 22.04.3 LTS GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu) bc 1.07.1 Observation: Both ibase and obase are unset. echo "A0" | bc 90 echo "B0" | bc 90 ...
yossi-matkal's user avatar
0 votes
0 answers
35 views

is it possible to integrate bc calculator in an app (script interpreter) as a library?

is it possible to integrate bc command line calculator in an app (script interpreter) as a library? I am not asking if that can be done, I am asking if there is something ready already that wraps bc ...
Gussak Thor's user avatar
1 vote
1 answer
144 views

Why I can pipe echo into bc, but I can't do the same with printf?

I can pipe echo into bc. But I cannot do the same with "printf": it gives syntax error. ❯ echo "100-5" | bc 95 ❯ printf "%s" "100-5" | bc (standard_in) 1: ...
robertspierre's user avatar
0 votes
1 answer
3k views

(standard-in) 1: Syntax error when using bc

I have this command that works correct. However it shows a (standard_in) 1: Syntax error that I don't know how to identify. participants=0 while IFS=, read -r id name nat sex date height weight sport ...
CPG's user avatar
  • 15
1 vote
2 answers
145 views

What's the upper-bound for an array index/subscript in GNU bc?

I've found that there is an upper-bound on your subscripts/indices in an array in GNU bc. Running interactively and asking to let arr[100000000]=42 returns an error: Runtime error (func=(main), adr=...
Mike Pierce's user avatar
9 votes
3 answers
4k views

Linux bc defines PI as 99

I'm using bc in Ubuntu Linux. It has a pre-defined constant PI that's set to 99. Why does bc define PI to be 99 instead of 3.14159...?
Jim's user avatar
  • 91
2 votes
0 answers
73 views

bash variable getting some weird hex data from Command substitution calling a script containing a bc calculation

bash --version GNU bash, version 5.1.16(1)-release (x86_64-apple-darwin21.1.0) I'm calling a script (dewpoint) which contains a bc calculation that defines dewpoint based on temp(c) and humidity(%). ...
rr0ss0rr's user avatar
  • 352
9 votes
1 answer
684 views

Who decided the bc math library will define sine cosine and arctangent?

If you load the bc math library you get the trig functions s() and c() and a() which are sine, cosine, and arctangent respectively. Why these three functions? I know why it's those three from the ...
Mike Pierce's user avatar
0 votes
1 answer
424 views

Rescaling a scaled value using bc command

I am trying to find square root of 12345 and then scale it to 100 decimal places using the command shown below: val=$(bc <<< "scale=100 ; sqrt ( 12345) " ) But the problem is I ...
kuspia's user avatar
  • 164
0 votes
4 answers
477 views

Floating point comparison in shell

I am comparing the floating point values in shell script based on this reference. Following is the script contents num1=50.960 num2=6.65E+07 echo "${num1} < ${num2}" | bc When I ran the ...
Raja's user avatar
  • 133
0 votes
5 answers
828 views

BC scale rounding error

Trying to truncate the value of a bc calculation, however when I set the scale=0 it returns 0 for the entire calculation. Setting scale=1 returns a proper value of 357.3, but scale=0 will always ...
BroccoliHead's user avatar
1 vote
2 answers
3k views

How to pipe a number into bc elegantly?

Is there a more elegant way than using xargs -Ix for the following? echo "283" | xargs -Ix bc -l -e "scale=2; l( x )/l(10)"
Jona Engel's user avatar
3 votes
4 answers
4k views

I keep receiving command not found when I try to run my code in bash

I am trying to write a code that will calculate the radius and area of a circle using pi and the circumference. When I try to run my code, I receive an error message that says main.sh: line 5: bc: ...
user445702's user avatar
14 votes
2 answers
965 views

Is it possible to save `bc` command line history?

bash has a handy file .bash_history in which it saves the history of commands and on the next execution of bash the history is populated with saved commands. Is it possible to save bc command history ...
KamilCuk's user avatar
  • 860
0 votes
1 answer
182 views

How to shorten decimal output in bash?

Right now I am working on an assignment that involves advanced piping, and one of the questions involves calculating a percent and shortening it to 3 decimal places. I've managed to calculate the ...
PCRevolt's user avatar
2 votes
1 answer
4k views

Set scale for bc inside a variable

I am trying to divide two values in a loop using bc, and I have set that value as a variable. My problem is that I want that value to have 2 decimal places, but I am having trouble getting scale=2 to ...
mdem7705's user avatar
1 vote
3 answers
3k views

How to properly use bc to convert the value of percentage to floating point format?

I have 3 variables and values: totalLines=14 outsideLines=6 multiplied=600 totalLines represents the total number of lines (100%), while outsideLines represents number of lines with the timestamp ...
kamokoba's user avatar
  • 163
1 vote
1 answer
181 views

Why does GNU 'bc' have such a long copyright string?

When you run 'bc' on a GNU system, it prints out the following text: ~$ bc bc 1.07.1 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc. This is free ...
Ciaran Woodward's user avatar
5 votes
4 answers
2k views

How to get bc to print trailing zeros?

I read topics about how to get bc to print the first zero, but that is not exactly what I want. I want more... I want a function that returns floating point numbers with eight decimal digits. I am ...
Clement S's user avatar
8 votes
1 answer
743 views

bc: does POSIX prohibit standalone use of relational operators?

I want to compare two numbers with bc. Per this highly rated answer on StackOverflow, I can do it in a manner as such: printf '%s\n' '1.2 > 0.4' | bc bc sends 1 to STDOUT, indicating that the ...
Harold Fischer's user avatar
2 votes
1 answer
421 views

bc - command: not giving appropriate results

I am trying to do simple calculation using bc command in linux. Once i want the value with all the decimal places. other time i want only the integer part Want to get with full decimal places $ echo ...
Santhosh's user avatar
  • 409
9 votes
10 answers
3k views

How can I evaluate a math equation, one per line in a file?

I have a file with the following: 37 * 60 + 55.52 34 * 60 + 51.75 36 * 60 + 2.88 36 * 60 + 14.94 36 * 60 + 18.82 36 * 60 + 8.37 37 * 60 + 48.71 36 * 60 + 34.17 37 * 60 + 42.52 37 * 60 + 51.55 35 * 60 ...
void.pointer's user avatar
0 votes
3 answers
53 views

Discard integer and keep floating point

I'm trying to remove an integer from a decimal value that I currently have. current syntax: h=$(echo "scale=2; (($e/$g/$g))" | bc) echo $h The following is used to convert seconds to minutes ...
SSR's user avatar
  • 25
0 votes
2 answers
326 views

converting variables in a shell script from strings to numbers

I have a file laid out like this gender,yearsExperience,yearsSchool,wage female,9,13,6.3152956461 female,12,12,5.4797699786 female,11,11,3.6421699174 female,9,14,4.5933365997 female,8,14,2.4181574607 ...
sethparker's user avatar
1 vote
1 answer
176 views

Raise each element of an array to a power and store output to a new array in bash

I want to raise a fraction (fraction is calculated on first loop) to a decimal power (second loop), however, I always get 1 as a result. I want to store the output of the second loop in an array as ...
Maria Karypidou's user avatar
4 votes
7 answers
7k views

bash - sum numbers in a variable

I have a variable set with numbers separated with space, of which first number can also be led by space, example: VAR=" 2 1 34 3 2 " I need to sum all these numbers. Easiest way would be ...
Chris's user avatar
  • 4,151
3 votes
0 answers
1k views

Error compiling bc: cannot find libreadline.so.8 [closed]

I am following the Linux from Scratch book, Version 8.4 and am stuck on compiling bc (Chapter 6.15). I have copied and pasted the commands on that page up to the make command, but when I run make, I ...
Ryan Bester's user avatar
0 votes
3 answers
1k views

Add two doubles on bash

I'm trying to add two doubles y1=0.17580197E-01 y2=0.11979236E-02 sum=`echo $y1+$y2 | bc -l` the above script gives me sum = -2.704405652. How do i resolve this issue?
Black Heart's user avatar
1 vote
0 answers
27 views

bc command unexpected results [duplicate]

I am trying to compare a decimal and it seems like bc is ignoring it. The OS is CentOS release 6.9 (Final) and bash is bash-4.1.2-48.el6.x86_64, below is the code #!/bin/sh i=32.39 IP_LOW_2=32.0 ...
Atul's user avatar
  • 1,891
0 votes
1 answer
3k views

How display the output of calculation in decimal point using bc command

I want to do simple coding by storing the output calculation in variable and display the result in decimal point using command bc. But it only works on for minus calculation not for division ...
tembikai's user avatar
0 votes
2 answers
3k views

How to store the output of calculation to variable using bash script [duplicate]

I want to do a simple coding by storing the product of calculation using command bc into variable. answer="$varnameX % $num1" | bc echo "the result is $answer" it did not show the product of ...
tembikai's user avatar
-1 votes
1 answer
537 views

Bash Script Wind Chill using getopt and bc

I am now getting the correct output, however it is not scaling to only 3 places. Anyone know how to do that? function windchill { to=32 v=10 vel=`echo 'e(l('$v')*0.16)' | bc -l` w=$(...
Trinity Zamrzla's user avatar
0 votes
0 answers
509 views

Convert to Single line output of bc command [duplicate]

echo 'obase=2; 3402823669209384469321375 ' | bc 10110100001001001101110000110101000010010101110011010101100110001011\ 01011010011111 I would like to convert the multiple line output into a single ...
m4n07's user avatar
  • 191
1 vote
1 answer
452 views

Calculate difference in negative floating numbers with bc

I want to calculate the difference between two negative floating numbers and subsequently check if the result is below a certain threshold. The numbers are stored in the file.dat, since this ...
And's user avatar
  • 167
2 votes
1 answer
8k views

(standard_in) 1: syntax error when using bc

I have written an .awk which executes some operation on a .tr file and writes the output to a file. The END section of .awk file prints this: printf("%15.2f\n%15.5f\n%15.2f\n%15.2f\n%15.2f\n%10.2f\n%...
Robur_131's user avatar
  • 221
0 votes
2 answers
2k views

GNU BC: How is the “modulo” (%) with scale other than 0 useful?

This is a self-answered question, the research that is reasonable to ask for a question goes in the answer part, Please do not downvote because you believe I have not researched enough for an answer. ...
user avatar
0 votes
3 answers
2k views

compare two IP address using bc

I am trying to compare two IP address using bc command however the IP with lower numeric value is not being trapped in the test. Here is my code: TEST=$(echo "$i < $IP_BRD && $i > $...
Atul's user avatar
  • 1,891
0 votes
1 answer
193 views

AIX Does not support bc boolean expression [duplicate]

I ran into an issue where bc does not have boolean expressions in the AIX system. Wondering if there is a replacement command so I don't have make my code any longer? This is in a bash script. Here ...
Tags's user avatar
  • 3
23 votes
1 answer
3k views

bc: Why does `ibase=16; obase=10; FF` returns FF and not 255? [duplicate]

I've been using bc to convert numbers between binary to hex, octal to decimal and others. In the following example, I was trying to convert base 16 (hex) number to binary, octal and decimal. I don't ...
user avatar
2 votes
1 answer
138 views

how to close a while statement in bc script

I cannot find a way to close while statement.The following is my bash script code. bc << EOF a=0; while(a<10) a++; print a; EOF The ouput is not as expected, it prints all the a values ...
dcnh35's user avatar
  • 321
0 votes
2 answers
332 views

Syntax error when i use ssh with awk over bc

I basically get the memory usage from two different nodes via ssh and store this in two different variables. I then select the host with lesser memory and return a code 0 or 1. The array arr contains ...
Kiran Kumar's user avatar
0 votes
1 answer
2k views

How to script an an exponential function that decrements the value of the exponent every iteration so that the exponent does not grow?

New to Linux and having problems getting this script to run. Been working on something to take large numbers and use the remainder (modulus) to store the result ( to save memory ). Using the bash ...
Post Apocalyptic's user avatar
70 votes
1 answer
8k views

Why is 'bc' required to build the Linux kernel?

The Linux kernel minimal building requirements specifies that the calculator bc is required to build kernel v4.10, the minimal version of the tool being 1.06.95. What use is made of bc in this ...
Ikaros's user avatar
  • 882
-1 votes
1 answer
6k views

bash + divide number with round number to up or down [duplicate]

I want to convert 8874M to GIGA so I did this echo $(( 8874 / 1024 )) but actually we get 8 ( while exactly results is 8.6 ) so what need to change in order to get the 8.6?
jango's user avatar
  • 423
-3 votes
1 answer
2k views

linux + how to calculate float numbers with bash [duplicate]

how to calculate float numbers: with bash example DRIVER_MEMORY=$(( 5 * 0.6 * 0.9 )) -bash: 5 * 0.6 * 0.9 : syntax error: invalid arithmetic operator (error token is ".6 * 0.9 ") remark - ...
jango's user avatar
  • 423