• Advertisement
Sign in to follow this  

Perl and scientific notation

This topic is 3148 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement