LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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

Reply
 
LinkBack Search this Thread
Old 08-10-2004, 01:05 PM   #1
zero79
Member
 
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

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

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

[edit]
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 02:08 AM.
 
Old 08-06-2008, 04:25 PM   #3
rahul.garg
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.
Rahul
 
Old 09-14-2008, 10:36 PM   #4
zero79
Member
 
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Original Poster
Rep: Reputation: 30
Question

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


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 09:40 PM.

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