How to get bc to handle numbers in scientific (aka exponential) notation?

awk scientific notation
unix sort numeric scientific notation
bash convert int to float
expr: not a decimal number
how to perform real number number with decimal point calculation in linux
bash multiply float
bash calculate percentage
float linux

bc doesn't like numbers expressed in scientific notation (aka exponential notation).

$ echo "3.1e1*2" | bc -l
(standard_in) 1: parse error

but I need to use it to handle a few records that are expressed in this notation. Is there a way to get bc to understand exponential notation? If not, what can I do to translate them into a format that bc will understand?

i'm trying to manipulate numbers that are in exponential notation (for example 3.8678E-5) using the gnu bc calculation tool. it doesn't seem to. How to get bc to handle numbers in scientific(aka exponential) notation? (6) Let me try to summarize the existing answers, with comments on each below: (a) If you indeed need to use bc for arbitrary-precision calculations - as the OP does - use the OP's

Hello there, I have a script that must be written in bash that has to deal with I guess you mean numbers like 1.234e29 which are not supported by bc either? then I find that this does not seem to work for numbers with exponential notation. How to get bc to handle numbers in scientific (aka exponential) notation? bash numeric floating-accuracy bc asked Oct 14 '12 at 13:19 stackoverflow.com 31 votes How do I get accented letters to actually work on bash? bash cygwin accented-strings asked Oct

One can use awk for this; for example,

awk '{ print +$1, +$2, +$3 }' <<< '12345678e-6 0.0314159e2 54321e+13'

produces (via awk's default format %.6g) output like 12.3457 3.14159 543210000000000000 while commands like the following two produce the output shown after each, given that file edata contains data as shown later.

$ awk '{for(i=1;i<=NF;++i)printf"%.13g ",+$i; printf"\n"}' < edata`
31 0.0312 314.15 0 
123000 3.1415965 7 0.04343 0 0.1 
1234567890000 -56.789 -30 

$ awk '{for(i=1;i<=NF;++i)printf"%9.13g ",+$i; printf"\n"}' < edata
       31    0.0312    314.15         0 
   123000 3.1415965         7   0.04343         0       0.1 
1234567890000   -56.789       -30 


$ cat edata 
3.1e1 3.12e-2 3.1415e+2 xyz
123e3 0.031415965e2 7 .4343e-1 0e+0 1e-1
.123456789e13 -56789e-3 -30

Also, regarding solutions using sed, it probably is better to delete the plus sign in forms like 45e+3 at the same time as the e, via regex [eE]+*, rather than in a separate sed expression. For example, on my linux machine with GNU sed version 4.2.1 and bash version 4.2.24, commands sed 's/[eE]+*/*10^/g' <<< '7.11e-2 + 323e+34' sed 's/[eE]+*/*10^/g' <<< '7.11e-2 + 323e+34' | bc -l produce output 7.11*10^-2 + 323*10^34 3230000000000000000000000000000000000.07110000000000000000

Convert the number to a decimal of less than 18 digits (for a 53 bit significant but bc has its own limitations (doesn't understand the e as exponent, it has to To have more, you need the GNU version of awk with the arbitrary  bc is ignoring scale option ; How to get bc to handle numbers in scientific(aka exponential) notation? How to get a decimal number when dividing in bc? How do I get bc(1) to print the leading zero? How do I calculate the log of a number using bc?

You can also define a bash function which calls awk (a good name would be the equal sign "="):

= ()
{
    local in="$(echo "$@" | sed -e 's/\[/(/g' -e 's/\]/)/g')";
    awk 'BEGIN {print '"$in"'}' < /dev/null
}

Then you can use all type of floating point math in the shell. Note that square brackets are used here instead of round brackets, since the latter would have to be protected from the bash by quotes.

> = 1+sin[3.14159] + log[1.5] - atan2[1,2] - 1e5 + 3e-10
0.94182

Or in a script to assign the result

a=$(= 1+sin[4])
echo $a   # 0.243198

Rewriting a number in its expanded form with exponents helps you to better understand Duration: 2:31 Posted: Feb 18, 2011 40 How to get bc to handle numbers in scientific (aka exponential) notation? Oct 14 '12 33 How to get bc to handle numbers in scientific (aka exponential) notation?

Luckily there is printf, which does the formatting job:

The above example:

printf "%.12f * 2\n" 3.1e1 | bc -l

Or a float comparison:

n=8.1457413437133669e-02
m=8.1456839223809765e-02

n2=`printf "%.12f" $n`
m2=`printf "%.12f" $m`

if [ $(echo "$n2 > $m2" | bc -l) == 1  ]; then 
   echo "n is bigger"
else
   echo "m is bigger"
fi

bc is a language that supports arbitrary precision numbers with interactive execution of statements. After all files have been processed, bc reads from the standard input. of the exponent and the maximum of scale and the scale of the first expression. which is not part of a number as a short hand notation for for last . The Scientific format displays a number in exponential notation, replacing part of the number with E+n, in which E (exponent) multiplies the preceding number by 10 to the nth power. For example, a 2-decimal scientific format displays 12345678901 as 1.23E+10, which is 1.23 times 10 to the 10th power. A number format does not affect the actual cell value that Excel uses to perform calculations

Have you ever bought a tent or pack that flopped miserably in the field? bag Q sleeping pad Number of days/nights spent testing: Testing locales and Seattle, WA, USA 98134; (800) 531-9531; 2,000 dealers; catalog Coleman Exponent 3*v ideal long-weekend pack, and felt it could handle 5 days or judicious packing. Bash Scientific Notation. Hello there, I have a script that must be written in bash that has to deal with reading in values from a file (in scientific notation), and requires executing some mathematical operations with them. What is the easiest way to go

For example, type man bc on your linux terminal. All you Convert a scientific decimal format number to time format in hh:mm:ss, hours:minutes:seconds. JavaScript Calculate 2 Numbers Form Input Text - Addition, 10:49. Hi How to get in textbox values into two decimal point in asp. No exponent, mandatory integer. 12 How to get bc to handle numbers in scientific (aka exponential) notation? Oct 14 '12 11 Find the largest palindrome made from the product of two 3-digit numbers Aug 25 '11

If we wanted to sum the number of neighbors to each county in Arizona, we would Learn how to convert from scientific notation to standard form by watching the In this game you can "promote" your pieces aka. Regarding the sigma notation, it seems to have been included in a tentative draft Exponents and Integers. NOTE: This function should also handle numbers in scientific notation (AKA standard form or exponential notation). For example, if you have a number represented as 6.156x10-9, you'll need to convert it to the format 6.156e-9 and then use the function above. It should recognise the number, round it and return the result in ordinary decimal

Comments
  • Two successive bash substitutions will work (i.e. v=${v/e/*10^}; v=${v/^+/^}), provided the result isn't used in an expression with higher precedence than *.
  • It may be helpful to mention that when the superscript on the exponential is negative, one has to specify the scale in bc, otherwise one may get unexpected 0.
  • uhm, so awk handles significant digits correctly. That is interesting. The only drawback I can see is that this way you have to set a maximum precision to your numbers, which if exceeded would make the script not work properly. If there was a way to force awk to use arbitrary precision it would be perfect. I like better your version of the sed command rather than my own, I forgot about the possibilities of *.
  • @Ferdinando, yes, awk has the drawbacks you mention, and its real numbers typically are doubles with 16 digit resolution; for example, awk '{printf"%.40g",+$1}' <<< 12345678901234567891234567890123456e-20 produces 123456789012.345672607421875
  • Great alternative to bc, if potentially losing precision is not a concern; Note that the portable way to force something into a number in awk is to append +0, not to prepend +. For instance, while awk '{ print +$1 }' <<<1e-1 works fine in mawk and gawk (outputs 0.1), it does not in BSD awk (as used on OS X; outputs the input unmodified). By contrast, awk '{ print $1+0 }' <<<1e-1 should work with all awk implementations.
  • I like this solution very much, provided I don't find any pitfalls. I have to do basic arithmetic with scientific notation so often and this works a charm so far. For now I have defined your function in my bash_profile and named it scmath. Using the = symbol seems a bit dangerous to me