| View previous topic :: View next topic |
| Author |
Message |
Jonas Latt Guest
|
Posted: Mon Aug 15, 2005 6:29 pm Post subject: Semantics of numeric_limits<double>::min() |
|
|
Hi
From its name, I expected numeric_limits<double>::min() to return a
negative value as close as possible to -infinity. It does not. It
actually returns a positive value very close to 0.
The semantics for int are different. On my workstation, I get
numeric_limits<int>::int() = -2147483648 (as tested on g++ and intel icc).
This strikes me as odd. Does anybody know the actual definition of
numeric_limits<double>::min() ?
What do I do if I want to get the value of the datatype double that is
closest to -infinity?
Many thanks for your help,
Jonas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
E. Mark Ping Guest
|
Posted: Tue Aug 16, 2005 11:08 am Post subject: Re: Semantics of numeric_limits<double>::min() |
|
|
In article <4300bad9$1 (AT) nntp (DOT) unige.ch>,
Jonas Latt <Jonas.Latt (AT) cui (DOT) unige.ch> wrote:
| Quote: | Hi
From its name, I expected numeric_limits<double>::min() to return a
negative value as close as possible to -infinity. It does not. It
actually returns a positive value very close to 0.
|
Yes, that's why it's best not to guess what a function does. From the
standard, 18.2.1.2/2:
"For floating types with denormalization, returns the minimum positive
normalized value."
| Quote: | What do I do if I want to get the value of the datatype double that is
closest to -infinity?
|
that would be -std::numeric_limits<double>::max().
--
Mark Ping
[email]emarkp (AT) soda (DOT) CSUA.Berkeley.EDU[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Guest
|
Posted: Tue Aug 16, 2005 11:09 am Post subject: Re: Semantics of numeric_limits<double>::min() |
|
|
Jonas Latt wrote:
| Quote: | Hi
From its name, I expected numeric_limits<double>::min() to return a
negative value as close as possible to -infinity. It does not. It
actually returns a positive value very close to 0.
The semantics for int are different. On my workstation, I get
numeric_limits<int>::int() = -2147483648 (as tested on g++ and intel icc).
This strikes me as odd. Does anybody know the actual definition of
numeric_limits<double>::min() ?
What do I do if I want to get the value of the datatype double that is
closest to -infinity?
Many thanks for your help,
Jonas
|
For floating point types the values in numeric_limits are always
positive. To find a numeric limit for a floating point type within the
range of negative numbers, simply negate the appropriate limit in the
positive range. For example the value closest to negative infinity that
a double can represent is its representable value closest to positive
infinity, negated:
-numeric_limits<double>::max();
Now it may seem asymmetric that the floating point limits are expressed
as positive values, while the integral limits range from positive to
negative. The difference I believe derives from the two different ways
each manages their signedness. Integral types use a twos-complement
format, while floating types have a single sign bit. In fact it is the
presence of the sign bit for floating types that assures us that
negating the positive limit as done above will produce the correct
limit in the negative range.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Abhishek Pamecha Guest
|
Posted: Tue Aug 16, 2005 11:11 am Post subject: Re: Semantics of numeric_limits<double>::min() |
|
|
a sly trick would be to
possibly use (- numeric_limits<double>::max() )
hth
abhishek
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kevin.hall@motioneng.com Guest
|
Posted: Tue Aug 16, 2005 11:39 am Post subject: Re: Semantics of numeric_limits<double>::min() |
|
|
Floating point numbers have a single bit that denotes sign. The
largest negative number is just the largest positive number * -1. So
you may use:
-numeric_limits<double>::max()
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Rob Guest
|
Posted: Tue Aug 16, 2005 2:42 pm Post subject: Re: Semantics of numeric_limits<double>::min() |
|
|
Jonas Latt wrote:
| Quote: | Hi
From its name, I expected numeric_limits<double>::min() to return a
negative value as close as possible to -infinity. It does not. It
actually returns a positive value very close to 0.
The semantics for int are different. On my workstation, I get
numeric_limits<int>::int() = -2147483648 (as tested on g++ and intel
icc).
This strikes me as odd. Does anybody know the actual definition of
numeric_limits<double>::min() ?
|
It is try positive value closest to zero that can be represented by a
double.
| Quote: |
What do I do if I want to get the value of the datatype double that
is closest to -infinity?
|
I would probably use -std::numeric_limits<double>::max()
There is a complication that floting point types can sometimes include
a representation of an infinite value (i.e. there is a reserved value
that means "infinity"). Not all floating point types support that, and
I assume it is not what you're looking for in any case.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Tue Aug 16, 2005 6:01 pm Post subject: Re: Semantics of numeric_limits<double>::min() |
|
|
Jonas Latt <Jonas.Latt (AT) cui (DOT) unige.ch> wrote:
| Quote: | Hi
From its name, I expected numeric_limits<double>::min() to return a
negative value as close as possible to -infinity. It does not. It
actually returns a positive value very close to 0.
The semantics for int are different. On my workstation, I get
numeric_limits<int>::int() = -2147483648 (as tested on g++ and intel icc).
This strikes me as odd. Does anybody know the actual definition of
numeric_limits<double>::min() ?
|
It's the smallest strictly positive double.
| Quote: | What do I do if I want to get the value of the datatype double that is
closest to -infinity?
|
Negate numeric_limits<double>::max().
A recent discussion of this oddity is archived at
<http://groups.google.com/group/comp.lang.c++.moderated/browse_frm/thread/aea18fab432eee69>
--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|