For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > May 2004 > bad type of variable?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author bad type of variable?
Aben Siatris

2004-05-22, 11:32 am


is this normal?
what's wrong?


my $number=(0.75-0.54)/0.03;
print "$number\n";
for (0..$number)
{
print "$_ / $number\n";
}

my $number=(75-54)/3;
print "$number\n";
for (0..$number)
{
print "$_ / $number\n";
}



output:
7
0 / 7
1 / 7
2 / 7
3 / 7
4 / 7
5 / 7
6 / 7
7
0 / 7
1 / 7
2 / 7
3 / 7
4 / 7
5 / 7
6 / 7
7 / 7
Charles K. Clarkson

2004-05-22, 11:32 am

Aben Siatris <mailto:aben@webcom.sk> wrote:
:
: is this normal?

Is what normal? The output looks okay.

: what's wrong?

The script declares $number twice and your output
indicates you may not have warnings turned on.

What were you expecting that didn't happen?

Or what happened you didn't expect?


: my $number=(0.75-0.54)/0.03;
: print "$number\n";
: for (0..$number)
: {
: print "$_ / $number\n";
: }
:
: my $number=(75-54)/3;
: print "$number\n";
: for (0..$number)
: {
: print "$_ / $number\n";
: }
:
: output:
: 7
: 0 / 7
: 1 / 7
: 2 / 7
: 3 / 7
: 4 / 7
: 5 / 7
: 6 / 7
: 7
: 0 / 7
: 1 / 7
: 2 / 7
: 3 / 7
: 4 / 7
: 5 / 7
: 6 / 7
: 7 / 7


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328


Aben Siatris

2004-05-22, 11:32 am

D=C5=88a Friday 14 May 2004 13:23 Charles K. Clarkson nap=C3=ADsal:
> Aben Siatris <mailto:aben@webcom.sk> wrote:
> :=20
> : is this normal?
>=20
> Is what normal? The output looks okay.
>=20
> : what's wrong?
>=20
> The script declares $number twice and your output
> indicates you may not have warnings turned on.
>=20
> What were you expecting that didn't happen?
>=20
> Or what happened you didn't expect?
>=20
>=20
> : my $number=3D(0.75-0.54)/0.03;
> : print "$number\n";
> : for (0..$number)
> : {
> : print "$_ / $number\n";
> : }
> :=20
> : my $number=3D(75-54)/3;
> : print "$number\n";
> : for (0..$number)
> : {
> : print "$_ / $number\n";
> : }
> :=20
> : output:
> : 7
> : 0 / 7
> : 1 / 7
> : 2 / 7
> : 3 / 7
> : 4 / 7
> : 5 / 7
> : 6 / 7
> : 7
> : 0 / 7
> : 1 / 7
> : 2 / 7
> : 3 / 7
> : 4 / 7
> : 5 / 7
> : 6 / 7
> : 7 / 7


at first is 'FOR' from 0..7 and output prints 0..6
at second is 'FOR' from 0..7 and output prins 0..7

it is normal?
Paul Archer

2004-05-22, 11:32 am

3:03pm, Aben Siatris wrote:

>
> at first is 'FOR' from 0..7 and output prints 0..6
> at second is 'FOR' from 0..7 and output prins 0..7
>
> it is normal?
>

The first time through, Perl is working with floating-point numbers, but the
second time, it is dealing with integers, so there are no rounding errors.


You can see the difference in this simpler example:

# perl -e'$number=(0.75-0.54)/0.03;$number=int($number);printf("%f\n",$number);'
6.000000

# perl -e'$number=(75-54)/3;$number=int($number);printf("%f\n",$number);'
7.000000


To see the root of the problem:

# perl -e'$number=(0.75-0.54)/0.03;;printf("%.20g\n",$number);'
6.9999999999999991118

# perl -e'$number=(75-54)/3;;printf("%.20g\n",$number);'
7



The rounding errors caused by using floating point math are causing the
difference.
In a strongly-typed language, this would be more obvious ('cause you would
have had to declare your variable as a float in the first place), but you
would still have the same problem.



-------------------------------------------------------------
"The lawgiver, of all beings, most owes the law allegiance.
He of all men should behave as though the law compelled him.
But it is the universal weakness of mankind that what we are
given to administer we presently imagine we own."
---------------------- H.G. Wells ---------------------------

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com