correct round of reals?

Dennis E. Hamilton infonuovo at email.com
Sat May 13 12:40:26 EDT 2000


I think I missed the original requirement that led to this thread.
Looking at the question about VC++, it seems to me there are two
different questions here.  I find it useful to separate them.

1. Intel implements IEEE floating point, and VC++ for Win32 on Intel32
uses the hardware implementation when it is available.  When software
floating-point is required (e.g., with a 386 processor), there is an
accurate emulation of IEEE floating point that kicks in at run-time in
the usual configuration.

I assume that IEEE rounding is done during C Language calculations, and
in casting from double to float.  This doesn't help with rounding to an
integer value unless you do something clever like Fredrick Lund's
technique, which depends on the fact that intermediate and final results
are in the precision of IEEE double format.

It seems easy enough to tell if IEEE rounding is being done in this
case.  Convert some simple fractional values that have exact
representations in IEEE floating point and confirm that you get the
expected result.

Otherwise, you may need to find an interface for controlling the option
settings of the floating-point arithmetic unit of the processor.  This
is probably not a good general practice.

2. A more important question may be, what you want the integer result
for?  There is nothing sacred about the IEEE rounding rule although it
offers a kind of mathematical purity.  If your desire is to preserve
exact arithmetic, you may be better of scaling everything to integers
(even the python long integer, if you need more than about 15 digits),
only showing decimal fractions in the output.  If fractional
intermediate results are possible, you then have further questions to
answer concerning how to reduce intermediate results to integers for use
in further calculations.  This surfaces all of the questions that we
sweep under the carpet by blindly using floating-point calculations.  I
guess this is what people mean about the difference between accuracy and
precision!

The pyNum folks may have dealt with fine rounding control of the IEEE
floating-point unit, because control of the rounding mode is often
important in assuring the accuracy of numerical functions.  You may have
to look in the C Libraries underneath pyNum for ideas of what you might
be able to use from there.  I haven't looked myself so this is a wild
guess.

-- Dennis

AIIM DMware Technical Coordinator
I am traveling until June 1, 2000, and am best reached via E-mail.

Dennis E. Hamilton
----------------------------
InfoNuovo
mailto:infonuovo at email.com
http://www.infonuovo.com


-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of Peter Schneider-Kamp
Sent: Friday, 12 May 2000 07:03
To: python-list at python.org
Subject: Re: correct round of reals?


Fredrik Lundh wrote:
>
> def rint(v, m=4503599627370496.0):
>     if abs(v) > m:
>         return v
>     if v > 0:
>         return (v + m) - m
>     else:
>         return (v - m) + m
>
> (this assumes IEEE doubles)

Thanks. Do you know if e.g. VC++ uses IEEE doubles?

The platforms this code has to run on are those that
do not have an rint(3) function. So a connected
question is if these platforms use IEEE doubles?

bye
Peter
--
Peter Schneider-Kamp          ++47-7388-7331
Herman Krags veg 51-11        mailto:peter at schneider-kamp.de
N-7050 Trondheim              http://schneider-kamp.de

--
http://www.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list