Topic: Defect report: <cmath> float functions cannot return HUGE_VAL
Author: Ray Lischner <dontspamme@spam.you>
Date: 25 Feb 02 23:17:38 GMT Raw View
[Moderator's note: this defect report has been
forwarded to the C++ committee. -moderator.]
The float versions of the math functions have no meaningful value to return
for a range error. The long double versions have a value they can return,
but it isn't necessarily the most reasonable value.
Section 26.5 [lib.c.math], paragraph 5, says that C++ "adds float and long
double overloaded versions of these functions, with the same semantics,"
referring to the math functions from the C90 standard.
The C90 standard, in section 7.5.1, paragraph 3, says that functions return
"the value of the macro HUGE_VAL" when they encounter a range error.
Section 7.5, paragraph 2, defines HUGE_VAL as a macro that "expands to a
positive double expression, not necessarily representable as a float."
Therefore, the float versions of the math functions have no way to signal a
range error. The semantics require that they return HUGE_VAL, but they
cannot because HUGE_VAL might not be representable as a float.
The problem with long double functions is less severe because HUGE_VAL is
representable as a long double. On the other hand, it might not be a "huge"
long double value, and might fall well within the range of normal return
values for a long double function. Therefore, it does not make sense for a
long double function to return a double (HUGE_VAL) for a range error.
--
Ray Lischner, author of C++ in a Nutshell (forthcoming, Q4 2002)
http://www.tempest-sw.com/cpp/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Wed, 27 Feb 2002 03:46:20 GMT Raw View
Ray Lischner wrote:
>
> [Moderator's note: this defect report has been
> forwarded to the C++ committee. -moderator.]
>
> The float versions of the math functions have no meaningful value to return
> for a range error. The long double versions have a value they can return,
> but it isn't necessarily the most reasonable value.
>
> Section 26.5 [lib.c.math], paragraph 5, says that C++ "adds float and long
> double overloaded versions of these functions, with the same semantics,"
> referring to the math functions from the C90 standard.
>
> The C90 standard, in section 7.5.1, paragraph 3, says that functions return
> "the value of the macro HUGE_VAL" when they encounter a range error.
> Section 7.5, paragraph 2, defines HUGE_VAL as a macro that "expands to a
> positive double expression, not necessarily representable as a float."
The C99 standard modifies that, to specify that HUGE_VALF should be used
for the new C99 float functions, and that HUGE_VALL should be used for
the new C99 long double functions. I'd suggest that C++ make similar
choices, but referring to a std::numeric_limits<T>::max() rather than a
macro. I don't think C++ should adopt the new C99 functions themselves;
overloading is a far better way to handle it.
> Therefore, the float versions of the math functions have no way to signal a
> range error. The semantics require that they return HUGE_VAL, but they
Well, actually, they're free to signal an error by setting errno to the
value ERANGE, or by raising a floating point exception. In C99 it's
actually possible to determine which option an implementation has
chosen, by examining the value of the expressions (math_errhandling &
MATH_ERRNO) or (math_errhandling & MATH_ERREXCEPT).
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]