Questions tagged [bc]

Arbitrary-precision arithmetic language.

Filter by
Sorted by
Tagged with
0
votes
2answers
18 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 ...
0
votes
1answer
77 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 ...
4
votes
4answers
603 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 ...
8
votes
1answer
592 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 ...
2
votes
1answer
87 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 ...
8
votes
10answers
1k 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 ...
0
votes
3answers
32 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 ...
0
votes
2answers
30 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 ...
1
vote
1answer
34 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 ...
3
votes
7answers
2k 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 ...
2
votes
0answers
129 views

Error compiling bc: cannot find libreadline.so.8

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 ...
0
votes
2answers
187 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
24 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
301 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
679 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
246 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
108 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
166 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 ...
1
vote
1answer
2k 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
1answer
488 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
357 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
64 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
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 ...
2
votes
1answer
53 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
193 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
850 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
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 ...
-3
votes
1answer
2k 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
1k 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
401 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
253 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,...
6
votes
6answers
2k 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
510 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
585 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
129 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
182 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
68 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" ...
8
votes
3answers
22k 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
825 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
403 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); ...
3
votes
1answer
265 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
473 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
1k 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 ...
7
votes
4answers
3k 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
156 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
51 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
1k 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
961 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 ...
3
votes
2answers
4k 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
2k 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 ...