C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Semantics of numeric_limits<double>::min()

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jonas Latt
Guest





PostPosted: Mon Aug 15, 2005 6:29 pm    Post subject: Semantics of numeric_limits<double>::min() Reply with 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

[ 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





PostPosted: Tue Aug 16, 2005 11:08 am    Post subject: Re: Semantics of numeric_limits<double>::min() Reply with quote



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





PostPosted: Tue Aug 16, 2005 11:09 am    Post subject: Re: Semantics of numeric_limits<double>::min() Reply with quote



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





PostPosted: Tue Aug 16, 2005 11:11 am    Post subject: Re: Semantics of numeric_limits<double>::min() Reply with quote

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





PostPosted: Tue Aug 16, 2005 11:39 am    Post subject: Re: Semantics of numeric_limits<double>::min() Reply with quote

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





PostPosted: Tue Aug 16, 2005 2:42 pm    Post subject: Re: Semantics of numeric_limits<double>::min() Reply with quote

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





PostPosted: Tue Aug 16, 2005 6:01 pm    Post subject: Re: Semantics of numeric_limits<double>::min() Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.