Sign in to follow this  
_Sigma

Perl and scientific notation

Recommended Posts

I have a perl script that reads in some numbers, which are in scientific notation such as: 0.0100E+01 However, I need to do some math on it. What is the best way to get it into a floating point format so math can be done? I've searched Google, but I haven't really found anything regarding this.

Share this post


Link to post
Share on other sites
A quick and dirty solution is to parse it, char by char, using a state machine.
I've already a chart for this parsing a string containing a floating point number into its floating point representation, would this help?

Share this post


Link to post
Share on other sites
Nah, I think I have the parsing thing cased. I actually have this thread as reference FWIW.

I was just hoping I didn't have to manually parse the number...I like to try to limit the amount of wheels I reinvent :)

I appreciate the offer!!

Share this post


Link to post
Share on other sites
I know that my numbers will always be legal scientific notation. Is it fair game to do this:


$input =~ s/[Ee]/\*10\*\*/; #replace E or e with *10**
my $parsed = eval $input;
print "$parsed\n";


I feel dirty, but it seems to work!

Share this post


Link to post
Share on other sites
Admittedly it's been a decade since I touched perl, but can't you just read it normally and use it normally? I'm pretty sure scanf and its ilk, of which Perl is descended, operate just fine on scientific notation.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Admittedly it's been a decade since I touched perl, but can't you just read it normally and use it normally? I'm pretty sure scanf and its ilk, of which Perl is descended, operate just fine on scientific notation.


And it does too...very odd, I swear it was crashing on the scientific notation I was feeding it as an example....

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this