Fwd: [MLton-user] IEEEReal rounding not working

Matthew Fluet fluet at tti-c.org
Mon Aug 6 10:05:15 PDT 2007


On Mon, 6 Aug 2007, Sean McLaughlin wrote:
>  Thanks again for the fix.  I found another quirk though:
> Seems like fromString is going the wrong way for negative numbers.
>
> On 7/29/07, Matthew Fluet <fluet at tti-c.org> wrote:
>> I think the Real.{to,from}Decimal are the closest, though the Basis
>> Library explicitly states that the conversions occur under TO_NEAREST.
>> I believe that the Real.fromString function works according to the current
>> rounding mode.

MLton's current implementation of Real.{scan,fromString} is essentially 
the following:

   fun scan reader state =
     case IEEEReal.scan reader state of
        NONE => NONE
      | SOME (da, state) => SOME (valOf (fromDecimal da), state)

   fun fromString = StringCvt.scanString scan

So, we essentially scan the string as an IEEEReal.decimal_approx and then 
use Real.fromDecimal to convert to a Real.real value.  As noted above, the 
Real.fromDecimal conversion is specified to use the IEEEReal.TO_NEAREST 
rounding mode.

But, given that the Basis Library states:
  "All arithmetic functions involve the rounding mode.  The standard
   requires that calculations be performed to arbitrary precision and then
   be rounded according tot he roudning mode to fit the relevant
   precision."

I would expect that any function which doesn't explicitly state that it 
occurs under a specific rounding mode would occur under the current 
rounding mode.  David Gay's binary <-> decimal conversion library (which 
is what MLton uses) provides functions for string to real that obey the 
current rounding mode, we were just defaulting to the one that follows the 
TO_NEAREST mode.  Shouldn't be too difficult to use the more general 
function for the Real.{scan,fromString} conversions.




More information about the MLton-user mailing list