RE: weird stuff in incrementing for loop with Perl

2004-05-23 Thread Sui Ming Louie
Brian,

It's round-off error.

Try the following:

printf %0.1f Ghz\n, $speed;


Cheers,

Sui

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Brian Gibson
Sent: Sunday, May 23, 2004 3:49 PM
To: [EMAIL PROTECTED]
Subject: weird stuff in incrementing for loop with Perl

Can anyone explain why in this simple for loop it changes after it gets to

6.1? I am trying to create a pull down menu for a web page that lists 
processor speeds from 1.0 Ghz all the way up to 20.0 Ghz and things get
crazy 
in between. I am incrementing my counter by .1 each time thru the loop.

$starting_number = 1.0;
$ending_number = 20.1;

for ($speed = $starting_number; $speed  $ending_number; $speed = $speed +
.1) 
{

   print $speed Ghz\n;

} ## End of for 1.0 Ghz to the total possible speed of the processors.




___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs



___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: weird stuff in incrementing for loop with Perl

2004-05-23 Thread Keith C. Ivey
Brian Gibson [EMAIL PROTECTED] wrote:

 $starting_number = 1.0;
 $ending_number = 20.1;
 
 for ($speed = $starting_number; $speed  $ending_number; $speed
 = $speed + .1) {
 
print $speed Ghz\n;
 
 } ## End of for 1.0 Ghz to the total possible speed of the
 } ## processors.

That's how floating-point numbers work.  They're not exact.
See http://perldoc.com/perl5.8.4/pod/perlfaq4.html#Why-am-I-
getting-long-decimals-(eg%2c-19.94999)-instead-of-the-
numbers-I-should-be-getting-(eg%2c-19.95)- (I think they should 
have used simple numbered anchors on those pages rather than 
using the whole question for anchor text, but scroll down if 
you have trouble).

Try using this instead of the print:

   printf %.1f GHz\n, $speed;

-- 
Keith C. Ivey [EMAIL PROTECTED]
Washington, DC

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: weird stuff in incrementing for loop with Perl

2004-05-23 Thread Michael D Schleif
* Brian Gibson [EMAIL PROTECTED] [2004:05:23:15:48:40-0400] scribed:
 Can anyone explain why in this simple for loop it changes after it gets to 
 6.1? I am trying to create a pull down menu for a web page that lists 
 processor speeds from 1.0 Ghz all the way up to 20.0 Ghz and things get crazy 
 in between. I am incrementing my counter by .1 each time thru the loop.
 
 $starting_number = 1.0;
 $ending_number = 20.1;
 
 for ($speed = $starting_number; $speed  $ending_number; $speed = $speed + .1) 
 {
 
print $speed Ghz\n;
 
 } ## End of for 1.0 Ghz to the total possible speed of the processors.

As others have offered, perl's math always uses floating point, and this
may have undesired results for things that appear as simple as you are
doing here.

One thing to keep in mind while using the suggested [s]printf is that
this may mask undesired truncation.  In your case, it probably will not
matter; but, consider this:

my $starting_number =  1;
my $ending_number   = 20;
my $speed = 10 * $starting_number;

while ($speed = (10 * $ending_number) ) {
printf %4.1f GHz\n, $speed / 10;
$speed++;
}



hth

-- 
Best Regards,

mds
mds resource
877.596.8237
-
Dare to fix things before they break . . .
-
Our capacity for understanding is inversely proportional to how much
we think we know.  The more I know, the more I know I don't know . . .
--


signature.asc
Description: Digital signature
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs