views:

77

answers:

1

Table 2 in section 5.6 on the bottom of page 11 of the IEEE 754 specification lists the ranges of decimal values for which decimal to binary floating-point conversion must be performed. The ranges of exponents don't make sense to me. For example, for double-precision, the table says the maximum decimal value eligible to be converted is (1017-1)*10999. That's way bigger then DBL_MAX, which is approximately 1.8*10308. Obviously I'm missing something -- can someone explain this table to me? Thanks.

+2  A: 

[Side note: technically, the document you link to is no longer a standard; "IEEE 754" should really only be used to refer to the updated edition of the standard published in 2008.]

My understanding is that, as you say, the left-hand column of that table describes the range of valid inputs to any decimal string to binary float conversion that's provided. So for example, a decimal string that's something like '1.234e+879' represents the value 1234*10^876 (M = 1234, N = 876), so is within the table limits, and is required to be accepted by the conversion functionality. Though note that exactly what form the decimal strings are allowed to take is outside the scope of IEEE 754; it's only the value that's relevant here.

I don't think it's a problem that some of the allowed inputs can be outside the representable range of a double; the usual rules for overflow should be followed in this case; see section 7.3 of the document. That is, the overflow exception should be signaled, and assuming that it's not trapped the result of the conversion (for a positive out-of-range value, say) is positive infinity if the rounding mode is round to nearest or round towards positive infinity, and the largest finite representable value if the rounding mode is round towards negative infinity or round towards zero.

Slightly more subtly, from my reading of this document, a decimal string like '1e+1000' should also be accepted by the conversion function, since the value it represents is expressible in the form 10 * 10^999, or even 10000000000000000 * 10^984. See the sentence that starts 'On input, trailing zeros shall be appended to or stripped from M ...' in section 5.6.

The current version of IEEE 754 seems to be a bit different in this respect, judging by the publicly available draft version (version 1.2.5): it just requires each implementation to specify bounds [-η, η] on the exponent of the decimal string, with η large enough to accomodate the decimal strings corresponding to finite binary values in the largest supported binary format; so if the binary64 format is the largest supported format, it looks to me as though η = 400 would be plenty big enough, for example.

Mark Dickinson
OK, so at least I wasn't making a mistake reading the table. Those exponent limits seem arbitrary -- it would have been nice if the spec gave the rationale. Why not 9999 or 99999? Or better yet, something closer to the max exponent? It's hard to imagine that a user would want the literal 1.234e879 to round to infinity instead of getting an error. Visual C++ and java both give "constant too big" for this case (Python, not surprisingly, gives 'inf' :)).
Rick Regan
I guess the idea is that an implementation can safely restrict itself to handing <= 3-digit exponents, which might help to simplify parsing of decimal strings. (Though as I mention above, a close reading seems to suggest that at least some cases of 4-digit exponents might have to be handled, depending on the exact string format; but I might well be misreading that bit.) I agree it's arbitrary, though; it's telling that current IEEE 754 pushes the decision of what exponents to handle onto the implementation.Where's @Stephen Canon when you need him!
Mark Dickinson
Right here, Mark, but I think you got the gist of it in your answer. I'll think about it a little and see if I can offer any further insight.
Stephen Canon
@Mark Dickinson: I found something in Jerome Coonen's "An Implementation Guide to a Proposed Standard for Floating-Point Arithmetic" (http://www.computer.org/portal/web/csdl/doi/10.1109/MC.1980.1653344): on page 79, section 2.21, it says "double: up to 3-digit exponent..." It's not rationale for allowing such a high exponent max, but at least it seems to confirm your guess.
Rick Regan
It's also curious that the range of exponents is asymmetric. If you normalize the expression M*10^N, the max positive N is greater than the max negative N (in absolute value).
Rick Regan