Topic: Floating point numeric_limits


Author: "yavor.vutov@gmail.com" <yavor.vutov@gmail.com>
Date: Thu, 13 Sep 2007 14:25:47 CST
Raw View
I have several questions regarding std::numeric_limits and floating
point types.

Why the min() member is choosen to return the smallest positive
representable value instead of
something like  (-max()).
This is a bit strange choice for me. Imagine something like this
template <class T>
T func(T a)
{
  T min = std::numeric_limits<T>::min();
  // one would expect that min <= a, for all T and all a, possibliy
without (-infinity).
}

Why there is no minus_infinity() method?

What about the signed zero?

I would like to be able to get the value (-0.), and to check if there
is a signed zero at all.

Yavor

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: =?iso-8859-1?q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Thu, 13 Sep 2007 22:02:24 CST
Raw View
On 13 Sep., 22:25, "yavor.vu...@gmail.com" <yavor.vu...@gmail.com>
wrote:
> I have several questions regarding std::numeric_limits and floating
> point types.
>
> Why the min() member is choosen to return the smallest positive
> representable value instead of
> something like  (-max()).

I think that this is for historical reasons to stay in sync with the
*_MIN constants from <limits.h> and <float.h>.

> This is a bit strange choice for me. Imagine something like this
> template <class T>
> T func(T a)
> {
>   T min = std::numeric_limits<T>::min();
>   // one would expect that min <= a, for all T and all a, possibliy
> without (-infinity).
>
> }

Yes, this is a problem, but by means of a helper template this is also
solvable (see e.g. boost's converter facilities) [I did not say
easy ;-)].

Your problem will also be solved - as a side effect - if the newly
proposed
std::numeric_limits member "lowest" will be accepted (see the recent
draft N2369 or the originating paper

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2348.pdf

)

Please note that this is actually a comfortable side effect of the
definition
of this member, because the official reason (given as a footnote in
the
upcoming standard) is that there exist otherwise C++-compatible
platforms
with floating point representations where -
std::numeric_limits<FloatType>::max()
is not the same as the most negative (finite) number. Personally I
have
always thought that such systems would violate the C(99) floating
point
model and I would appreciate if someone else could clarify why this
is
obviously *not* so.

> Why there is no minus_infinity() method?

It seems that -INF as the same as the negated form of INF on all
systems
that support floating point infinities, but this is just a guess.

> What about the signed zero?
>
> I would like to be able to get the value (-0.), and to check if there
> is a signed zero at all.

This is already possible, e.g. by using the function signbit from
math.h, see the note in the C99 standard:

"The signbit macro reports the sign of all values, including
infinities,
zeros, and NaNs. If zero is unsigned, it is treated as positive."

According to the section "Expression transformations" of the C99
standard the following should apply for *all* rounding directions:

bool has_signed_zero = signbit((-0.0)-(+0.0)) <> 0;

(No, it's not an ICE).

The same section explains that at least in the *default* rounding
direction the expression -(1.0 - 1.0) should return a -0.
All these tests work well on my x87 system by taking advantage
of signbit.


Greetings from Bremen,

Daniel Kr   gler


---
[ 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.comeaucomputing.com/csc/faq.html                      ]