2009/11/4 David Lee <david....@ecmwf.int>:
> Many thanks for the reply.
>
> Following the initial surprise, my main concern was that attempts to unearth
> a description or explanation (i.e. documentation) for the observed behaviour
> was so tricky.  For instance, there was nothing obvious in the relevant
> parts of "Programming Perl".

I have a feeling this is somewhat deliberate. Scalars just do the
Right Thing, mostly. You don't have to care about whether it's
actually stored as an integer or a float *most of the time*. You only
have to start caring when you need numbers with extreme values --
particularly anything >= 2^31. Therefore perl documentation doesn't
spend much time telling you about it.

If you know or suspect that your number needs are somewhat more
specialist, as they are here, then you shouldn't be using normal
scalars but a specific type suitable for your needs. If you are
unsure, it's better to just use such a datatype rather than check what
the exact limits are in the documentation.

I wouldn't mind a specific discussion of the issues somewhere in
perldoc, but I wouldn't want it polluting perldoc perldata. But one
problem with this is that it will always relate to system specific
issues. For example, here's exactly the same perl program run on two
different machines I have access to:

H:\>perl -e "use integer; my $i = 999999999999997; print $i, q[ ], $i+4,qq[\n];"

999999999999997 3

p...@tui:~$ perl -e 'use integer; my $i = 999999999999997; print $i, q[
], $i+4,qq[\n];'
999999999999997 1000000000000001

If you start writing code which depends on 64-bit native integers,
like the above code does, you'll write non-portable Perl code. Better
is to use bignums (or bigfloats) whenever you're in any doubt.

> So I'm happy to categorise it as "self bug", although I'd like to include an
> element of "documentation weakness" in there and I'd be happy to assist
> someone in trying to improve the latter.
>
> Anyway, your explanation was useful and gives us sufficient to decide how to
> address our local use of these numbers.  (In our case, they are
> human-oriented accumulated byte-counts, for which we don't actually need
> that significance/precision.)

Really? If it's an accumulator, you can get into trouble:

p...@tui:~$ perl -e 'my $i = 1e16; for (1..10) { printf "%.17g\n",++$i;  }'
10000000000000000
10000000000000000
10000000000000000
10000000000000000
10000000000000000
10000000000000000
10000000000000000
10000000000000000
10000000000000000
10000000000000000

remember even if you don't care about printing the right value, you
have to ask if you need to store the right value. Better hope you're
always adding large powers of two to that accumulator. And it's so
easy to use a datatype which does the Right Thing, why not use it?

Phil

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to