Philip Potter wrote:
I would guess that these numbers are being stored in floats, and that
these floats are 64-bit double precision, with 53 bits of mantissa.
That means that there are just under 16 decimal digits of precision in
these numbers. print and friends seem to automatically print no more
than 15 digits of precision:
[...]
You probably want to use bignums instead. If you are dealing with
large integers, where precision down to the units place is important,
you definitely want bignums and not floats.
H:\>perl -e "use bignum; my $i = 999999999999997; print $i, ' ',$i+4,qq[\n];"
999999999999997 1000000000000001
the bignum pragma makes $i into a Math::BigInt object rather than a
floating-point value.
Your solution (a non-obvious printf format) is a bad one because while
it solves the problem of output, it doesn't solve the problem of
representation. As soon as you try to store an integer bigger than
2^53 (approximately 9e15) you will lose significance:
[...]
In answer to your question, the behaviour of print is probably the
correct behaviour, since there's no point printing more precision than
you store. So it's a self bug.
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".
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.)
Thanks.
--
: David Lee
: ECMWF (Data Handling System)
: Shinfield Park
: Reading RG2 9AX
: Berkshire
:
: tel: +44-118-9499 362
: email: david....@ecmwf.int
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/