Perl and scientific notation

Started by
4 comments, last by _Sigma 14 years, 10 months ago
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.
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?
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!!
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!
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.
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....

This topic is closed to new replies.

Advertisement