Is this safe?

M

Mark P

Is the following safe:

double min_value = -numeric_limits<double>::max();

Basically, I'm trying to get a negative number with a very large magnitude.

Thanks,
Mark
 
J

James Dennett

Mark said:
Is the following safe:

double min_value = -numeric_limits<double>::max();

Basically, I'm trying to get a negative number with a very large magnitude.

I believe that's safe; all floating point formats in
use on any machine I've seen have the same range for
positive and negative numbers. (I'm not sure if the
C or C++ standards explicitly guarantee it, but in
this case that's fairly academic). This is maybe why
numeric_limits<X>::min() doesn't give a very negative
value for floating point types: the construction you
show above gives the real minimum value. (It's a shame
that min() does two different things depending on
whether the type is fp or integral, but that's another
issue.)

-- James
 
I

Ian Collins

James said:
I believe that's safe; all floating point formats in
use on any machine I've seen have the same range for
positive and negative numbers. (I'm not sure if the
C or C++ standards explicitly guarantee it, but in
this case that's fairly academic). This is maybe why
numeric_limits<X>::min() doesn't give a very negative
value for floating point types: the construction you
show above gives the real minimum value. (It's a shame
that min() does two different things depending on
whether the type is fp or integral, but that's another
issue.)
It's a real pain when attempting to use numeric_limits in templates.
 
J

James Dennett

Ian said:
It's a real pain when attempting to use numeric_limits in templates.

It's easily hidden with is_integral<T>, which isn't hard
to write (and will be standard in the future, I think),
but I agree that it does rather go against the grain of
generic programming.

-- James
 
P

paul.joseph.davis

I believe that's safe; all floating point formats in
use on any machine I've seen have the same range for
positive and negative numbers. (I'm not sure if the
C or C++ standards explicitly guarantee it, but in
this case that's fairly academic). This is maybe why
numeric_limits<X>::min() doesn't give a very negative
value for floating point types: the construction you
show above gives the real minimum value. (It's a shame
that min() does two different things depending on
whether the type is fp or integral, but that's another
issue.)

-- James

Well, a question to pose that might illustrate the reason min does
what it does.
What else would you call the function that gets the smallest positive
number representable as a float?
std::get_really_really_small_float() ?

I know thats awfully sarcastic, but really, although floats and ints
are theoretically the same, they are two fundamentally different data
types and represent two different things. If we think about the bit
representation it even makes a little sense on why this is the
behavior.

I don't know if floating point formats are included in the actual C++
standard or not, but generally their cited as being determined by IEEE
standards. My guess is that C++ doesn't specify this and leaves it as
an implementation detail, but I could see it going either way.

Oh and,

#include <iostream>
#include <limits>
int
main( int argc, char* argv[] )
{
std::cout << -std::numeric_limits<float>::max() << std::endl ;
}

compiles and spits out a really really big negative number. So yeah.
Its safe.

HTH,
Paul Davis
 
D

dasjotre

Is the following safe:

double min_value = -numeric_limits<double>::max();

Basically, I'm trying to get a negative number with a very large magnitude.

Thanks,
Mark

looup boost::numeric::bounds
it looks like:

template<class N>
struct bounds
{
static N lowest () { return implementation_defined; }
static N highest () { return implementation_defined; }
static N smallest() { return implementation_defined; }
};
 
K

Kai-Uwe Bux

Well, a question to pose that might illustrate the reason min does
what it does.
What else would you call the function that gets the smallest positive
number representable as a float?
std::get_really_really_small_float() ?

It should have been called epsilon().

The current epsilon(), of course, should be named delta(). <g>

Since that's no longer an option, I would vote for iota().
I know thats awfully sarcastic, but really, although floats and ints
are theoretically the same, they are two fundamentally different data
types and represent two different things. If we think about the bit
representation it even makes a little sense on why this is the
behavior.

I simply don't understand the rational to redefine the semantics of min().
Like integral types, floating point types have a maximal and a minimal
representable value. Like with integral types, there is a legitimate
interest to obtain those bounds from numeric_limits. Since the standard
does not guarantee that the domain of representable values is symmetric
about 0 (or does it?, I can't find it), as per the current standard, there
is no way to obtain the minimum representable value for float.
I don't know if floating point formats are included in the actual C++
standard or not, but generally their cited as being determined by IEEE
standards. My guess is that C++ doesn't specify this and leaves it as
an implementation detail, but I could see it going either way.

Oh and,

#include <iostream>
#include <limits>
int
main( int argc, char* argv[] )
{
std::cout << -std::numeric_limits<float>::max() << std::endl ;
}

compiles and spits out a really really big negative number. So yeah.
Its safe.

Maybe that works on your platform. Maybe, on you platform the domain for
floats extents farther to the negative side than to the positive side.


Best

Kai-Uwe Bux
 
P

Pete Becker

Kai-Uwe Bux said:
It should have been called epsilon().

That's not what epsilon means in floating-point circles. It's the
smallest value that can be added to 1.0 to yield a different value. So
it's a measure of the number of bits in the fraction, while the smallest
normalized value is more a measure of the number of bits in the exponent.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
P

Pete Becker

Well, a question to pose that might illustrate the reason min does
what it does.
What else would you call the function that gets the smallest positive
number representable as a float?
std::get_really_really_small_float() ?

Note, however, that that's not what min() is defined to do.min gives you
back the smallest normalized value. If an implementation supports
denormals, the smallest representable value is far smaller than that.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
K

Kai-Uwe Bux

Pete said:
That's not what epsilon means in floating-point circles. It's the
smallest value that can be added to 1.0 to yield a different value. So
it's a measure of the number of bits in the fraction, while the smallest
normalized value is more a measure of the number of bits in the exponent.

Ah, that's the reason then that the standard has it wrong. <g>

Anyway, I see the point of choosing a name that is already in use within the
community interested in that particular feature.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top