Skip to main content
GameDev.net gamedev.net
🔒 Locked

Perl and scientific notation

Started by _Sigma Jun 10, 2009 at 4:03 PM 4 replies 5.2k views
Original Post
_Sigma
_Sigma
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.
fastcall22
fastcall22
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?
_Sigma
_Sigma
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!!
_Sigma
_Sigma
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!
Sneftel
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.
_Sigma
_Sigma
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....

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.