in reply to time() function

This is because time is using a 32 bit integer and you are getting an overflow truncation like you do in say C. Here is a demo of what happens:

$time = time(); $odd = time * 1000; print "$time|$odd\n"; print "2**31 is ", 2**31, "\n"; $time1000 = $time *1000; $time1000 %= 2**31; print "Here is our number: $time1000\n"; print "End of *nix epoch is at: ", scalar gmtime (2**31-1)

The maximum integer value that time will hold is 2**31 or 2147483648. As $time1000 is much bigger than this we get overflow tructation. We simulate this with modulus % and lo and behold produce the same number.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: time() function
by IraTarball (Monk) on Sep 28, 2001 at 22:07 UTC
    So, I understand this is true. I'm O.K. with overflows. I can handle overflows. But why does
    $var = time * 1000;
    overflow and
    $var = time; $var *= 1000;
    not overflow?

    I believe that on my system (Win2k, perl 5.6.1) integers are 32 bit, just like returned by time() I verified this with print ($var&(~0)) which gives me (2**32)-1.

    Does perl automatically decide (not) to use BigInts? Any ideas?

    Ira,

    "So... What do all these little arrows mean?"
    ~unknown

      time() is a Perl function, implemented in C with 32 bit (long int) math relevant to the unix epoch.

      A perl scalar is an interesting beast (also implemented in C) that can hold a string or a number or a reference so is not directly comparable to a short, int, long, float, double, char in C. It is in fact a struct. Have a look at this code and perlman:perlguts. On the Win95 box in front of me Perl will represent up to 2**49 (562,949,953,421,312) as an integer before resorting to scientific notation.

      for ( 0..64 ) { $var = 2**$_; print "Perl can do 2**$_ : $var\n"; }

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print