Bash Scientific Notation | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Bash Scientific Notation

Shell Programming and Scripting


Tags
bash scientific notation

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-21-2008
amit_57 amit_57 is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 18 August 2008, 6:41 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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 about doing this/converting it to float to use | bc, etc.?

Thanks!
Sponsored Links
    #2  
Old 07-22-2008
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 12 October 2014, 10:11 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 117 Times in 110 Posts
This should be a basic example:

Code:
a=65; b=1.11; c=30 ; echo "scale= $a; $b ^ $c" | bc

For anything else try to elaborate your request.
Sponsored Links
    #3  
Old 07-22-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
I think the main problem here is the "scientific notation". I guess you mean numbers like 1.234e29 which are not supported by bc either?

Maybe something like this ...?


Code:
echo "1.234e23 9.876e14" | sed -e 's/ /*/' | perl -nle 'print eval $_'

Concur with danmero, an example of the input and expected output would help.
    #4  
Old 07-22-2008
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 12 October 2014, 10:11 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 117 Times in 110 Posts
bc can do the job

Code:
echo "1.234e23 9.876e14" | sed 's/e/*10^/g;s/ /*/' | bc

Sponsored Links
    #5  
Old 07-22-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
Good catch; thanks for the correction.
Sponsored Links
    #6  
Old 07-22-2008
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 12 October 2014, 10:11 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 117 Times in 110 Posts
Or just awk:

Code:
awk -v a="1.234e23" -v b="9.876e14" 'BEGIN{print (a * b)}'

Sponsored Links
    #7  
Old 08-18-2008
amit_57 amit_57 is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 18 August 2008, 6:41 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks all, in case you are interested -- I ended up using sed:

floatVar1=$(echo $Var1 | sed 's/\([0-9]*\(\.[0-9]*\)\?\)[eE]+\?\(-\?[0-9]*\)/(\1*10^\3)/g;s/^/scale=30;/'| bc)

There are some difficulties with the specific scientific notation my data files were using. (having an extra + after the E, 1.00e+1 = 10), so I had trouble getting bc to work, but I may have just messed up. However, the idea behind the question was supposed to be specifically how to convert something from scientific notation to float. Sorry for the confusion.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading Scientific notation from file and storing in array Filter500 Programming 3 12-16-2011 03:45 PM
Rounding scientific notation ppat7046 Shell Programming and Scripting 4 06-10-2009 03:20 PM
Conversion of scientific notation gingburg UNIX for Dummies Questions & Answers 4 02-24-2009 02:45 PM
How to add/multiply numbers with scientific notation (2.343e-5) chugger06 UNIX for Dummies Questions & Answers 1 01-26-2006 10:03 PM
How to Convert scientific notation to normal ? maheshsri Shell Programming and Scripting 2 01-05-2006 01:33 PM



All times are GMT -4. The time now is 10:58 PM.