[Pharo-project] Unexpected behavior of roundTo:
marcus.denker at inria.fr
Sat Apr 9 09:28:11 CEST 2011
On Apr 9, 2011, at 9:09 AM, Andres Valloud wrote:
> At the end of the day, we still insist on expecting there won't be loss of information just because we wrote a decimal print string for a binary floating point number. One can get offended or irritated all one wants, but the reality of the situation won't change.
> I think the real solution to these "problems" is to implement decimal floating point as per IEEE-754/2008. \\
The MPFR lib seems to me (as far as I understand) is inspired by IEEE 754
MPFR is a portable library written in C for arbitrary precision arithmetic on floating-point numbers. It is based on the GNU MP library. It aims to provide a class of floating-point numbers with precise semantics. The main characteristics of MPFR, which make it differ from most arbitrary precision floating-point software tools, are:
• the MPFR code is portable, i.e., the result of any operation does not depend on the machine word size mp_bits_per_limb (64 on most current processors);
• the precision in bits can be set exactly to any valid value for each variable (including very small precision);
• MPFR provides the four rounding modes from the IEEE 754-1985 standard, plus away-from-zero, as well as for basic operations as for other mathematical functions.
The following five rounding modes are supported:
• MPFR_RNDN: round to nearest (roundTiesToEven in IEEE 754-2008),
• MPFR_RNDZ: round toward zero (roundTowardZero in IEEE 754-2008),
• MPFR_RNDU: round toward plus infinity (roundTowardPositive in IEEE 754-2008),
• MPFR_RNDD: round toward minus infinity (roundTowardNegative in IEEE 754-2008),
• MPFR_RNDA: round away from zero (experimental).
The ‘round to nearest’ mode works as in the IEEE 754 standard: in case the number to be rounded lies exactly in the middle of two representable numbers, it is rounded to the one with the least significant bit set to zero. For example, the number 2.5, which is represented by (10.1) in binary, is rounded to (10.0)=2 with a precision of two bits, and not to (11.0)=3. This rule avoids the drift phenomenon mentioned by Knuth in volume 2 of The Art of Computer Programming (Section 4.2.2).
> Then you can write something like 1.2345 and know for a fact the number represented is *exactly* 1.2345. At least one VisualWorks platform already supports decimal floating point in hardware (IBM's POWER line). C99 extensions exist at least in draft form to extend C so that it supports decimal floating point. IBM has released a GPL library in ANSI C that implements the feature. I am not sure what is the deal with the license and if you can e.g.: interface to it without making your whole Smalltalk GPL.
MPFR is LGPL, so linking that in the VM or using it via FFI is no problem license wise.
Marcus Denker -- http://www.marcusdenker.de
INRIA Lille -- Nord Europe. Team RMoD.
More information about the Pharo-project