Questions tagged [bc]
Arbitrary-precision arithmetic language.
109
questions
0
votes
1answer
22 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 ...
0
votes
4answers
44 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 ...
0
votes
5answers
48 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 ...
1
vote
2answers
54 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)"
116
votes
20answers
153k views
Is there a way to get the min, max, median, and average of a list of numbers in a single command?
I have a list of numbers in a file, one per line. How can I get the minimum, maximum, median and average values? I want to use the results in a bash script.
Although my immediate situation is for ...
3
votes
3answers
84 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: ...
24
votes
1answer
27k views
Does bc support hex calculations?
I'm trying to do a hex calculation directly with bc, I already specified the scale.
echo 'scale=16;c06b1000-c06a5e78' | bc
But I still get a zero. What could be wrong?
23
votes
5answers
29k views
Understand “ibase” and “obase” in case of conversions with bc?
I often use bc utility for converting hex to decimal and vice versa. However, it is always bit trial and error how ibase and obase should be configured. For example here I want to convert hex value C0 ...
18
votes
4answers
32k 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 "...
12
votes
2answers
492 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 ...
51
votes
5answers
51k views
Float operation with bc?
It looks like bc doesn't support float operations, when I do echo 1/8 | bc it get me a zero.
I checked bc (1), but it doesn't even mention float, so I wonder if it's supported?
0
votes
3answers
353 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?
0
votes
1answer
59 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 ...
1
vote
1answer
104 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 ...
7
votes
3answers
25k views
Bash limiting precision of floating point variables
In Ubuntu 14.04.1 LTS 64-bit bash I am declearing floating point variables by multiplying floating point bash variables in bc with scale set to 3; however, I cannot get the number of digits after the ...
0
votes
2answers
62 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 ...
8
votes
10answers
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 ...
0
votes
1answer
94 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 ...
5
votes
4answers
867 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
637 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
5answers
4k 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=...
2
votes
1answer
111 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 ...
0
votes
3answers
36 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
41 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
53 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
4k 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
267 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 ...
8
votes
4answers
4k 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 ...
38
votes
4answers
27k views
How is bc different from dc?
What are the differences between dc and bc calculators?
When should I use dc and when bc?
1
vote
1answer
3k views
Convert serialized Java object to human readable
I have object of class
class X {
private DateTime dt;
"constructor, set/get"
}
I have one instance of this object serialized in file.bin.
I want to show content of `file.bin on the Linux ...
1
vote
0answers
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
...
0
votes
1answer
642 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
1k 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 ...
13
votes
4answers
77k views
Calculate variable, and output it to another variable
The only calculator I know is bc. I want to add 1 to a variable, and output to another variable.
I got the nextnum variable from counting string in a file:
nextnum=`grep -o stringtocount file.tpl....
-1
votes
1answer
329 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=$(...
5
votes
2answers
321 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....
2
votes
3answers
3k views
BC - no “\\n” at the end + start with zeros?
This is a bc output, e.g.:
Input:
echo "scale=10; BLA-BLA-HERE-NOT-IMPORTANT" | bc
Output:
.3708446283953709207058828124021300754352578903651372655882743141882\
...
0
votes
0answers
187 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
245 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
4k 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%...
20
votes
3answers
39k 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
2answers
155 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.
1
vote
1answer
788 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
675 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
89 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 ...
24
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 ...
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" ...
2
votes
1answer
74 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
234 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
1k 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 ...