Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [bc]

Arbitrary-precision arithmetic language.

0
votes
2answers
43 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?
1
vote
0answers
23 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 ...
0
votes
1answer
34 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 ...
0
votes
2answers
30 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 ...
-1
votes
1answer
62 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=$(...
0
votes
0answers
30 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 ...
0
votes
1answer
50 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 ...
0
votes
1answer
184 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%...
0
votes
0answers
21 views

Problem in divison with in awk ( gives only upto 6 decimal points ) [duplicate]

Say I have file o.txt A,B,47.367447754129 C,D,20.742171853178 E,F,31.890380392692 I want to divide 3rd column by 100 and want to see result as below A,B,.47367447754129 C,D,.20742171853178 E,F,....
0
votes
1answer
67 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. ...
0
votes
3answers
74 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 > $...
0
votes
1answer
37 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 ...
23
votes
1answer
1k 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 ...
2
votes
1answer
27 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 ...
0
votes
2answers
119 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 ...
0
votes
1answer
382 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 ...
66
votes
1answer
6k 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 ...
-3
votes
1answer
358 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?
-3
votes
1answer
518 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 - ...
0
votes
2answers
179 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....
2
votes
1answer
174 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,...
5
votes
6answers
1k 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
0
votes
1answer
400 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 ...
1
vote
1answer
448 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 < $...
0
votes
2answers
87 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.
3
votes
2answers
60 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....
0
votes
1answer
65 views

How change first rows in column format of next's rows, replacing * by a number and perform operations between columns?

I have the following file which has the first two rows as if they were a single-column (i.e. a continuous string). I would like to separate them into columns and replace the "*" character with an "x" ...
0
votes
3answers
8k 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 "...
3
votes
2answers
477 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 ...
1
vote
3answers
128 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); ...
2
votes
1answer
142 views

Start bc with pi defined

I would like to create an alias for bc that runs bc -l and specifies that pi=4*a(1). This way, I can start each session with pi already defined. What alias will do this?
-1
votes
1answer
390 views

Bash as float calculator

I have used this to write a function that calculates floating-point numbers. mt (){ echo "$1" | bc -l | awk '{printf "%f", $0}' echo ' ' } This works great, but I was wondering if there is a way to ...
1
vote
1answer
587 views

use loop variable for calculation bash

for i in {0..9} do T=$(bc<<<"8+$i*0.5") echo $T done I get : syntax error near unexpected token `T=$(bc<<<"8+$i*0.5")' I believe the problem is the $i. What am I doing ...
6
votes
3answers
1k views

how to make bc to show me 10 and not 10.00

#!/bin/bash q=$(bc <<< "scale=2;$p*100") head -n$q numbers.txt > secondcoordinate.txt That's just part of the script, but I think it's enough to clarify my intentions. p is a variable ...
1
vote
3answers
125 views

Unexpected backslash when converting from hex to binary number

I am trying to convert a hex line to binary in bash, but the output includes a backslash. Can someone explain why? Input: for i in 3D3D516343746D4D6D6C315669563362; do BIN=$(echo "ibase=16; obase=2;...
2
votes
1answer
47 views

wants to understanding the meaning of these symbols one by one

so this simple command: echo "Hello, World! sin($1)=$(echo "s($1)“|bc -l)" I know echo is like print or show, and "" is string , but $1 so my understanding is $ is input and 1 is first input correct?...
1
vote
0answers
651 views

Why can't I install bc on my Raspberry Pi? [duplicate]

When I try to run the command sudo apt-get install bc I get the following error message: Reading package lists... Done Building dependency tree Reading state information... Done Package munin-...
0
votes
1answer
571 views

Unix command to return maximum float value out of two after subtraction

This is similar to question about getting the minimum/maximum of two numbers, but I'm interested to do the same with float numbers. So basically I'm trying to use shell arithmetic expansion using the ...
2
votes
2answers
2k views

How do I get bc to start decimal fractions with a leading zero

How do I get bc to start decimal fractions with a leading zero? $ bc <<< 'scale=4; 1/3' .3333 I want 0.3333.
1
vote
1answer
1k views

Wrong output in comparing floats

I'm trying to compare two floats in bash and something is going wrong. Here is the code sample based on solution here num1=0.502E-01 num2=0.01 echo $num1'>'$num2 | bc -l echo $num2'>'$num1 | bc ...
4
votes
2answers
655 views

Float operations with bc not precise?

When I'm performing float operations in shell using bc, the result is not the same if I use a regular calculator. Am I doing something wrong? For example, I need to find a volume of a sphere. User ...
3
votes
2answers
312 views

How can I do basic calculations in a shell script?

I want to perform some mathematical operations in the shelll. For example: 5+50*3/20 + (19*2)/7 I tried: #!/bin/bash read equ echo "scale=3; $equ" | bc -l Expected output: 17.929 My output: ...
14
votes
3answers
26k views

How to format floating point number with exactly 2 significant digits in bash?

I want to print the floating point number with exactly two significant digits in bash (maybe using a common tool like awk, bc, dc, perl etc.). Examples: 76543 should be printed as 76000 0.0076543 ...
0
votes
1answer
90 views

Piping difference of grep -c's with different regex to bc

I need to find the number of words in a file (1 word per line) that do not contain some set of letters. To do this I want to take the difference of grep -c '.' <file> and grep -c '<...
2
votes
4answers
5k views

Shell Scripting: calculate power of a number with a real number as an exponent

I am using shell scripting and I am using the following expression: A=`echo "(( (($a / $b) ^ 0.3) -1 ))" |bc -l` I want to have a real number as an exponent. I noticed that If I place 0.3, it rounds ...
0
votes
2answers
89 views

Is there an equivalent of javascript Math.min() in bash/bc etc.? [duplicate]

I need to convert some javascript calculations to bash, and I am wondering if there is an equivalent function of javascript Math.min() in bash, bc or any other calculator in the shell?
3
votes
3answers
114 views

A bc problem about long expression

I want to calculate an expression in shell. I use the following code: pi=$(echo "scale=10; 4*a(1)" | bc -l) i=3 d=`expr (1+c($pi*($i/10)+$pi))/2 | bc -l` But it says bad pattern: (1+c(3.1415926532*(...
1
vote
4answers
55 views

How to calculate % of successful queries

I am trying to calculate what is the % of successful queries in apache log. I have two commands: cat access_log|cut -d' ' -f10|grep "2.."|wc -l and cat access_log|cut -d' ' -f10|wc -l They return ...
1
vote
2answers
91 views

Any way to customize the initialization of variables in the bc calculator?

The bc calculator appears to assign uninitialized variables the value of zero. I would like to alter this behavior so that when bc encounters an uninitialized variable, it instead flags the expression ...
1
vote
3answers
2k views

bc scale: How to avoid rounding? (Calculate small binomial probability)

Following code calculates the Binomial Probability of a success event k out of n trials: n=144 prob=$(echo "0.0139" | bc) echo -e "Enter no.:" read passedno k=$passedno nCk2() { num=1 den=...