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:
Bryan C . Warnock
Date:
March 7, 2001 16:08
Subject:
Re: [ID 20010305.005] "use integer" doesn't make rand() return integers
Message ID:
01030719101701.05028@homer.idiocity.nut
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.  :-(

#!/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    
                   ^^  ^^ ?

But yes, I believe I mentioned that.

> 
> The following will never terminate for instance:
> 
>     {   use integer;
>         print ++ $i, "\n";
>         redo unless rand () > 0.1
>     }

Yes, I don't recall perl actually preventing the user from doing things 
that are perfectly legit, but don't make any sense.  However, even with 
'use integer' semantics the above wouldn't terminate, so I don't see your 
point.  Unless it returns an arbitrary range of integers.  What range 
should that be?

(I've just reread your past posts.  It seems I misinterpreted your 
position.  You want rand() of all sorts overloaded, no?  If so, I apologize 
for my misrepresentation before.)

-- 
Bryan C. Warnock
bwarnock@capita.com

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