Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 08-10-2004, 01:05 PM   #1
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Rep: Reputation: 30
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

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.
Old 08-10-2004, 02:35 PM   #2
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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)}'

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 ...
echo "5.083E-5" | awk -F"E" 'BEGIN{OFMT="%10.10f"} {print $1 * (10 ^ $2)}'  


Last edited by Tinkster; 08-11-2004 at 02:08 AM.
Old 08-06-2008, 04:25 PM   #3
LQ Newbie
Registered: Aug 2008
Posts: 1

Rep: Reputation: 0
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.
Old 09-14-2008, 10:36 PM   #4
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Original Poster
Rep: Reputation: 30

can you post the rest of your script? something to test out, and fiddle to get working?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer notation vs array notation? pablowablo Programming 5 03-14-2005 12:34 PM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 09:18 AM
Scientific Software linux_fresher Linux - Software 3 08-18-2003 07:46 AM
scientific calculator mtint Linux - Software 0 10-20-2002 06:05 PM
Scientific notation for C Chijtska Programming 4 02-19-2002 09:24 AM

All times are GMT -5. The time now is 07:38 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration