Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org how to use bc to handle numbers in scientific/exponential notation?
 Linux - General This Linux forum is for general Linux questions and discussion. If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

 08-10-2004, 02:05 PM #1 zero79 Member   Registered: Nov 2003 Location: Ohio Distribution: Debian Unstable Posts: 460 Rep: how to use bc to handle numbers in scientific/exponential notation? 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 support the above notation. what i want to do is comare two numbers to determine which is larger. this is my idea of what the script would look like if bc supported exponential notation Code: ```!/bin/bash a=5.083E-5 b=7.0134 echo "if (\$a > \$b); then echo \$a; else echo \$b; fi" | bc``` \$a and \$b are arbitrary values written in scientific notation. i've tried parsing the values into a decimal and a power using 'awk,' but this won't work if i end up with a number that does not need to be represented in scientific notation (such as \$b in the above script). i really appreciate any thoughts on this. thanks.
 08-10-2004, 03:35 PM #2 Tinkster Moderator   Registered: Apr 2002 Location: in a fallen world Distribution: slackware by choice, others too :} ... android. Posts: 23,004 Blog Entries: 11 Rep: awk -F"E" '{print \$1 * ( 10 ^ \$2)}' would work, but the problem is that awk also uses scientific notation once the number exceeds a given level ... Taking the number from your example this is what happens: [tink@diggn:~]\$ echo "5.083E-5" | awk -F"E" '{print \$1 * ( 10 ^ \$2)}' 5.083e-05  Btw, I'm not quite sure what you're trying to achieve by echoing a single number into bc? Having toyed around a bit more I came up with this one ... Code: ```echo "5.083E-5" | awk -F"E" 'BEGIN{OFMT="%10.10f"} {print \$1 * (10 ^ \$2)}' 0.0000508300``` [/edit] Cheers, Tink Last edited by Tinkster; 08-11-2004 at 03:08 AM.
 08-06-2008, 05:25 PM #3 rahul.garg LQ Newbie   Registered: Aug 2008 Posts: 1 Rep: Hi Guys, I'm stuck with a similar problem. I'm trying to add two numbers using bc. Both the numbers are in exponent form. THe command is FDRAGAVG[\${index}]=`echo "scale=16 ; (\${FDRAGAVG[\${index}]}+\${fdrag})" | bc` Since the bc does not recognize the exponent precision, all that information is being lost. Can someone please suggest me a better way to add up numbers in exponent notation in bash script Thanks for the help. Rahul
 09-14-2008, 11:36 PM #4 zero79 Member   Registered: Nov 2003 Location: Ohio Distribution: Debian Unstable Posts: 460 Original Poster Rep: can you post the rest of your script? something to test out, and fiddle to get working?