Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

how to evaluate exponential notations and floating points

by BhariD (Sexton)
on Dec 15, 2009 at 20:07 UTC ( #812926=perlquestion: print w/ replies, xml ) Need Help??
BhariD has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code:

while($evalue = <DATA>){ chomp; if($evalue < 0.001){ print OUTPUT $evalue, "\n"; } } close DATA;

DATA ----------- 1e-005 2e-090 0.00 0.67 1.0 4e-065

For some reason the above code does not seem to do what I want. It prints out the input in the DATA as it is without evaluating the if statement above.

OUTPUT (wrong) ----------- 1e-005 2e-090 0.00 0.67 1.0 4e-065

The output should look like this: (the evalue of 0.00 means 1e-180, so should be counted).

OUTPUT (correct) ----------- 1e-005 2e-090 0.00 4e-065

Anyone knows what I am doing wrong here?

Comment on how to evaluate exponential notations and floating points
Select or Download Code
Re: how to evaluate exponential notations and floating points
by kennethk (Monsignor) on Dec 15, 2009 at 20:15 UTC
    I cannot replicate your problem with the posted code. The following code, taken almost entirely from your OP, works for me:

    #!/usr/bin/perl use strict; use warnings; while(my $evalue = <DATA>){ chomp $evalue; if($evalue < 0.001){ print $evalue, "\n"; } } __DATA__ 1e-005 2e-090 0.00 0.67 1.0 4e-065

    The main changes are the file handles. Note that your chomp was not given a variable, and hence was operating on $_ instead of $evalue.

Re: how to evaluate exponential notations and floating points
by bichonfrise74 (Vicar) on Dec 15, 2009 at 20:15 UTC
    Take a look at this.
    #!/usr/bin/perl use strict; while( my $evalue = <DATA> ){ chomp $evalue; if($evalue < 0.001){ print "$evalue\n"; } } __DATA__ 1e-005 2e-090 0.00 0.67 1.0 4e-065
Re: how to evaluate exponential notations and floating points
by ikegami (Pope) on Dec 15, 2009 at 20:19 UTC

    Anyone knows what I am doing wrong here?

    Asking questions about one piece of code while posting another.

    It doesn't produce the results you describe, and you would have noticed you're chomping the wrong variable.

    while($evalue = <DATA>){ chomp; # Should be chomp( $evalue ); if($evalue < 0.001){ print $evalue, "\n"; } } close DATA; __DATA__ 1e-005 2e-090 0.00 0.67 1.0 4e-065
    1e-005 2e-090 0.00 4e-065
Re: how to evaluate exponential notations and floating points
by GrandFather (Cardinal) on Dec 15, 2009 at 22:44 UTC

    Always use strictures (use strict; use warnings; - see The strictures, according to Seuss).

    Providing a stand alone script that reproduces the error helps a lot too. In this case something like the following (although this sample doesn't demonstrate your issue):

    use strict; use warnings; while (my $evalue = <DATA>) { chomp $evalue; if ($evalue < 0.001) { print $evalue, "\n"; } } __DATA__ 1e-005 2e-090 0.00 0.67 1.0 4e-065

    True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://812926]
Approved by sweetblood
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-09-17 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (62 votes), past polls