develooper Front page | perl.perl5.porters | Postings from March 2001

Re: [ID 20010305.005] "use integer" doesn't make rand() return integers

Thread Previous | Thread Next
From:
Nicholas Clark
Date:
March 8, 2001 02:35
Subject:
Re: [ID 20010305.005] "use integer" doesn't make rand() return integers
Message ID:
20010308103544.A7731@plum.flirble.org
On Wed, Mar 07, 2001 at 07:10:17PM -0500, Bryan C . Warnock wrote:
> On Wednesday 07 March 2001 18:08, abigail@foad.org wrote:
> > Yes, but how useful is the floating point value of rand() in a "use
> integer"
> > context? As soon as you use it an arithmetic operation, including
> comparison,
> > it gets rounded.
>
> Er, truncated, I should think.  No, floored.  No, it doesn't seem to do
> either.  Or it does both.  :-(

Both arguments get truncated before any calculation
It's not the "rounded" result of a floating point calculation.

> #!/usr/bin/perl -w
> @a = (-1.9, -1.1, -.9, -.1, .1, .9, 1.1, 1.9);
> print "@a\n";
> @b = map { $_ - 1 } @a;
> print "@b\n";
> {
>     use integer;
> 	@c = map { $_ - 1 } @a;
> 	print "@c\n";
> }
>
> [bryan@homer ~]$ perl -w round
> -1.9 -1.1 -0.9 -0.1 0.1 0.9 1.1 1.9
> -2.9 -2.1 -1.9 -1.1 -0.9 -0.1 0.1 0.9
> -2 -2 -1 -1 -1 -1 0 0
>                    ^^  ^^ ?

    -1   -1    0    0   0   0   1   1
    -1   -1   -1   -1  -1  -1  -1  -1
    ==   ==   ==   ==  ==  ==  ==  ==
    -2   -2   -1   -1  -1  -1   0   0

It's doing exactly what it means by "integer arithmetic"

Maybe the pod needs an example.

Nicholas Clark

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About