Vonunov has asked for the wisdom of the Perl Monks concerning the following question:

(SOLVED, <s>to the extent it actually needs to be, further minor issues are still here if you care</s>)

I'm at Day 3 of one of those Learn The Bare Bones Of X Language In Y Days tutorials. If you'll hit End, you'll see the exercises I'm supposed to be doing. I'm having a bit of trouble with 1, though; this is my response to it (after much slimming-down):
#!/usr/bin/perl -w use strict; $count = .0; until ($count == .9) { $count = $count + .1; print ($count, "\n"); } print ("End!\n");
Before I added -w and use strict;, the script would go into an infinite loop of this sort:
685.700000000087
685.800000000087
685.900000000087
686.000000000087
686.100000000087
I'm sure it's something to do with the fact that I'm using floating point values. When I replace them with whole numbers, it counts one to ten then prints End! just wonderfully. I've tried zero-padding the values (as in 0.9 instead of .9) as well; this makes no difference. When I added -w and use strict;, I get this:
-bash-2.05b$ perl count
Global symbol "$count" requires explicit package name at count line 4.
{same for $count at lines 6, 8, 8, and 10}
Execution of count aborted due to compilation errors.
Am I supposed to be declaring $count as a floating point variable somehow?

I searched and found a post or two that said something about trying to compare floating point values being a Bad Idea. Am I doing something wrong, or will this just never work?

Replies are listed 'Best First'.
Re: Floating point number counting script stuck in a loop
by GrandFather (Saint) on Oct 25, 2007 at 07:42 UTC

    Testing floating point numbers for equality (== or !=) is the problem. Instead test using >=, >, < or <=.


    Perl is environmentally friendly - it saves trees
      I see, thanks. This compiles and runs properly:
      #!/usr/bin/perl $count = .0; while ($count < .9) { $count = $count + .1; print ($count, "\n"); } print ("End!\n");
      But when I use -w and use strict; I get those same compilation errors. What is an explicit package name? What I've found about this error is that it has something to do with having not declared a variable you're trying to use under -w or use strict;, but I've declared my only variable. I don't suppose it especially matters if it compiles at all, but I'm curious now.
        When using strict (which is a good thing), declaring variables happens like this:
        my $count;
        Technically, you've initialised a variable, not declared it. Doing both at the same time:
        my $count = 0.0;
        Update: This link may prove helpful.
        Declaring variables

        When you use strict; (highly recommended btw.) you need to declare your variables so you need to stick my in front of the first assignment to $count. However, I'd be inclined to use a C type for loop in this case (note the declaration inside the loop header):

        use strict; use warnings; for (my $count = 0.0; $count < 0.9; $count += 0.1) { print "$count\n"; } print "End!\n";

        Note too that print doesn't need () and that variables can be interpolated directly into double quoted strings.


        Perl is environmentally friendly - it saves trees
Re: Floating point number counting script stuck in a loop
by FunkyMonk (Bishop) on Oct 25, 2007 at 09:05 UTC
    You'll probably find diagnostics useful while you're getting used to strict and warnings. It causes perl to spit out more verbose errors and warnings.

    For example:

    use strict; use warnings; use diagnostics; $x = 4; __END__ (F) You've said "use strict vars", which indicates that all variab +les must either be lexically scoped (using "my"), declared beforehand +using "our", or explicitly qualified to say which package the global var +iable is in (using "::").