net.sf.saxon.value
Class FloatingPointConverter

java.lang.Object
  extended by net.sf.saxon.value.FloatingPointConverter

public class FloatingPointConverter
extends Object

This is a utility class that handles formatting of numbers as strings.

The algorithm for converting a floating point number to a string is taken from Guy L. Steele and Jon L. White, How to Print Floating-Point Numbers Accurately, ACM SIGPLAN 1990. It is algorithm (FPP)2 from that paper. There are three separate implementations of the algorithm:

The choice of method depends on the value of the number being formatted.

The module contains some residual code (mainly the routine for formatting integers) from the class AppenderHelper by Jack Shirazi in the O'Reilly book Java Performance Tuning. The floating point routines in that module were found to be unsuitable, since they used floating point arithmetic which introduces rounding errors.

There are several reasons for doing this conversion within Saxon, rather than leaving it all to Java. Firstly, there are differences in the required output format, notably the absence of ".0" when formatting whole numbers, and the different rules for the range of numbers where exponential notation is used. Secondly, there are bugs in some Java implementations, for example JDK outputs 0.001 as 0.0010, and IKVM/GNU gets things very wrong sometimes. Finally, this implementation is faster for "everyday" numbers, though it is slower for more extreme numbers. It would probably be reasonable to hand over formatting to the Java platform (at least when running the Sun JDK) for exponents outside the range -7 to +7.


Field Summary
static FloatingPointConverter THE_INSTANCE
           
 
Method Summary
static FastStringBuffer appendDouble(FastStringBuffer s, double value)
          Append a string representation of a double value to a string buffer
static FastStringBuffer appendDoubleExponential(FastStringBuffer s, double value)
          Append a string representation of a double value to a string buffer, forcing use of exponential notation
static FastStringBuffer appendFloat(FastStringBuffer s, float value)
          Append a string representation of a float value to a string buffer
static FastStringBuffer appendFloatExponential(FastStringBuffer s, float value)
          Append a string representation of a float value to a string buffer, forcing use of exponential notation
static FastStringBuffer appendInt(FastStringBuffer s, int i)
          Format an integer, appending the string representation of the integer to a string buffer
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

THE_INSTANCE

public static FloatingPointConverter THE_INSTANCE
Method Detail

appendInt

public static FastStringBuffer appendInt(FastStringBuffer s,
                                         int i)
Format an integer, appending the string representation of the integer to a string buffer

Parameters:
s - the string buffer
i - the integer to be formatted
Returns:
the supplied string buffer, containing the appended integer

appendDouble

public static FastStringBuffer appendDouble(FastStringBuffer s,
                                            double value)
Append a string representation of a double value to a string buffer

Parameters:
s - the string buffer to which the result will be appended
value - the double to be formatted
Returns:
the original string buffer, now containing the string representation of the supplied double

appendDoubleExponential

public static FastStringBuffer appendDoubleExponential(FastStringBuffer s,
                                                       double value)
Append a string representation of a double value to a string buffer, forcing use of exponential notation

Parameters:
s - the string buffer to which the result will be appended
value - the double to be formatted
Returns:
the original string buffer, now containing the string representation of the supplied double

appendFloat

public static FastStringBuffer appendFloat(FastStringBuffer s,
                                           float value)
Append a string representation of a float value to a string buffer

Parameters:
s - the string buffer to which the result will be appended
value - the float to be formatted
Returns:
the original string buffer, now containing the string representation of the supplied float

appendFloatExponential

public static FastStringBuffer appendFloatExponential(FastStringBuffer s,
                                                      float value)
Append a string representation of a float value to a string buffer, forcing use of exponential notation

Parameters:
s - the string buffer to which the result will be appended
value - the float to be formatted
Returns:
the original string buffer, now containing the string representation of the supplied float


Copyright (C) Michael H. Kay. All rights reserved.