SPUG: S.O.S: Sum-Of-Squares (nickname:Help)

Fred Morris m3047 at inwa.net
Wed Oct 1 21:06:32 CDT 2003


At 4:05 PM 10/1/03, Itay Furman wrote:
>The problem: for some cases I get a small, non-zero deviation.
>My question: does any one have some insight or rules-of-a-thumb as to
>the expected numerical precision in basic arithmetic operations under Perl?

I've been painfully reminded that that is the expected behavior of Perl on
several occasions. This can bite you even when trying to figure out what
day of the week it is. There is some Math:: stuff. A long time ago I
actually took Numerical Analysis, from which I learned:

Several techniques for minimizing loss of precision, which contribute to
unreadable code in most circumstances.

That zero is only zero when you say it is, theoretically speaking.. unless
you confine yourself to integer math.. which I don't believe OTS Perl is
capable of (however, see Math::BigInt).

OTOH, prime numbers are a lot of fun.


You probably want some elucidation of exactly what standards-conformant
floating point representation Perl uses; I can't help you there.


>## Smallest vector that yields the flow
>$ test_innerprod.pl 1 1
>                                sos=2   residue=2.22044604925031e-16
>## These don't have to be 1's
>$ test_innerprod.pl 0.5 0.5 0 0
>                                sos=0.5 residue=2.22044604925031e-16
>$ test_innerprod.pl 0.5 0.5
>                                sos=0.5 residue=2.22044604925031e-16
>

Interesting. Why is it always the same? Anybody got a calculator (with that
kind of precision) handy?

It does seem to me that your example points the way to a technique for
determining the precision (which was probably covered, I don't remember):
take 1, and add a value to it which decreases by O(n**x) where n is 10 or 2
and x is a decreasing series integer value until there is no effect. That
will tell you the resolution on the platform on which you're running... or
else you'll get an underflow error. :-\

Post your (readable) code and I'll run it on a few machines.

--

Fred Morris
m3047 at inwa.net





More information about the spug-list mailing list