Questions tagged [bc]

Arbitrary-precision arithmetic language.

Filter by
Sorted by
Tagged with
9 votes
3 answers
3k 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...?
user avatar
  • 91
2 votes
0 answers
55 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(%). ...
user avatar
  • 317
8 votes
1 answer
309 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 ...
user avatar
0 votes
1 answer
34 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 ...
user avatar
  • 19
0 votes
4 answers
76 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 ...
user avatar
  • 133
0 votes
5 answers
173 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 ...
user avatar
1 vote
2 answers
561 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)"
user avatar
3 votes
3 answers
448 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: ...
user avatar
13 votes
2 answers
677 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 ...
user avatar
  • 460
0 votes
1 answer
95 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 ...
user avatar
1 vote
1 answer
691 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 ...
user avatar
0 votes
2 answers
583 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 ...
user avatar
  • 143
0 votes
1 answer
119 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 ...
user avatar
5 votes
4 answers
1k 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 ...
user avatar
8 votes
1 answer
687 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 ...
user avatar
2 votes
1 answer
151 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 ...
user avatar
  • 235
9 votes
10 answers
2k 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 ...
user avatar
0 votes
3 answers
41 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 ...
user avatar
  • 25
0 votes
2 answers
47 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 ...
user avatar
1 vote
1 answer
115 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 ...
user avatar
4 votes
7 answers
5k 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 ...
user avatar
  • 2,537
3 votes
0 answers
612 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 ...
user avatar
0 votes
3 answers
678 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?
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 ...
user avatar
  • 1,681
0 votes
1 answer
1k 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 ...
user avatar
0 votes
2 answers
2k 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 ...
user avatar
-1 votes
1 answer
408 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=$(...
user avatar
0 votes
0 answers
287 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 ...
user avatar
  • 171
1 vote
1 answer
307 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 ...
user avatar
  • 167
1 vote
1 answer
6k 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%...
user avatar
  • 191
1 vote
1 answer
1k 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
  • 38.8k
0 votes
3 answers
1k 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 > $...
user avatar
  • 1,681
0 votes
1 answer
115 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 ...
user avatar
  • 3
23 votes
1 answer
2k 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
94 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 ...
user avatar
  • 271
0 votes
2 answers
285 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 ...
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 ...
user avatar
70 votes
1 answer
7k 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 ...
user avatar
  • 882
-2 votes
1 answer
5k 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?
user avatar
  • 353
-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 - ...
user avatar
  • 353
0 votes
2 answers
556 views

bash + arithmetic calculation with bash

I need to do the follwing with bash what is the elegant way ? to get the final $sum value worker_machine=32 executors_per_node=3 executer=$worker_machine/$executors_per_node-1 spare=$executer X 0....
user avatar
  • 353
2 votes
1 answer
261 views

divide two columns, not with each other

I have a CSV file generated by a script of mine, It gets CPU time used per user, however it gets this in seconds, i need it in hours so i need to divide each line by 3600. example input file **USER,...
user avatar
  • 1,864
6 votes
6 answers
3k views

printf, awk ... How to format a number with space to the thousands

Is there a simple way to separate a very large number per thousands with printf, awk, sed ? So 10000000000000 become 10 000 000 000 000 Thanks
user avatar
  • 73
0 votes
1 answer
636 views

Difference in the result of echo and printf

Why do I see a difference in the results for the following code: read a printf "%.3f\n" $(bc -l <<< "$a") RESULT: 17.929 and read a echo "scale=3; $a" | bc -l RESULT: 17.928 My input ...
user avatar
  • 103
1 vote
1 answer
765 views

Checking that a decimal number is in a range in bc

I've written this if, that is obviously not working, and I still can't manage to get over it: #LASTEFFECTIVEHASH if (( $(echo "$LASTEFFECTIVEHASHMINVAL < $LASTEFFECTIVEHASH < $...
user avatar
  • 358
0 votes
2 answers
186 views

What is the numeral for base greater than 16 in bc?

I recently read about bc and found that it supports obase upto 999. Can anyone point me to the numeral set for bc for base greater than 16.
user avatar
5 votes
2 answers
568 views

How to avoid wrapping in bc output

I have the following example in GNU v. 1.06 (I cannot identify a limit pertaining to line length): v=$(bc -l <<<"scale=100;4*a(1)"); echo $v which returns: 3....
user avatar
27 votes
4 answers
46k views

Append to a pipe and pass on?

I have a simple bash function dividing two numbers: echo "750/12.5" | bc I'd like to take the output from bc and append /24 and pipe said result to another instance of bc. Something like: echo "...
user avatar
3 votes
2 answers
1k views

Why does bc exit 0 when dividing by 0?

I was trying to think of a quick and illustrative way to generate a non-successful exit status and thought dividing by zero with the bc would be a good idea. I was suprised to discover that although ...
user avatar
  • 2,546
1 vote
3 answers
682 views

Adding zero in front of small decimal places [duplicate]

so when I'm coding Im trying to have the output print out 0.12 instead of .12. Here is my code below echo -n "What is the total cost? "; read cents cost=$(echo "scale =2;$cost_in_cents/100" | bc); ...
user avatar