Is this safe?

Discussion in 'C++' started by Mark P, Feb 13, 2007.

  1. Mark P

    Mark P Guest

    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
    Mark P, Feb 13, 2007
    #1
    1. Advertising

  2. Mark P wrote:
    > 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
    James Dennett, Feb 13, 2007
    #2
    1. Advertising

  3. Mark P

    Ian Collins Guest

    James Dennett wrote:
    > Mark P wrote:
    >
    >>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.)
    >

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

    --
    Ian Collins.
    Ian Collins, Feb 13, 2007
    #3
  4. Ian Collins wrote:
    > James Dennett wrote:
    >> Mark P wrote:
    >>
    >>> 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.)
    >>

    > 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
    James Dennett, Feb 13, 2007
    #4
  5. Mark P

    Guest

    On Feb 12, 10:22 pm, James Dennett <> wrote:
    > Mark P wrote:
    > > 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


    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
    , Feb 13, 2007
    #5
  6. Mark P

    dasjotre Guest

    On 13 Feb, 02:53, Mark P <>
    wrote:
    > 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; }
    };
    dasjotre, Feb 13, 2007
    #6
  7. Mark P

    Kai-Uwe Bux Guest

    wrote:

    > On Feb 12, 10:22 pm, James Dennett <> wrote:
    >> Mark P wrote:
    >> > 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

    >
    > 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
    Kai-Uwe Bux, Feb 13, 2007
    #7
  8. Mark P

    Pete Becker Guest

    Kai-Uwe Bux wrote:
    > wrote:
    >
    >> On Feb 12, 10:22 pm, James Dennett <> wrote:
    >>> Mark P wrote:
    >>>> 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

    >> 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().
    >


    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)
    Pete Becker, Feb 13, 2007
    #8
  9. Mark P

    Pete Becker Guest

    wrote:
    >
    > 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)
    Pete Becker, Feb 13, 2007
    #9
  10. Mark P

    Kai-Uwe Bux Guest

    Pete Becker wrote:

    > Kai-Uwe Bux wrote:
    >> wrote:
    >>
    >>> 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().
    >>

    >
    > 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
    Kai-Uwe Bux, Feb 13, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gabriel Rossetti
    Replies:
    0
    Views:
    1,310
    Gabriel Rossetti
    Aug 29, 2008
  2. Replies:
    1
    Views:
    333
    Brian Candler
    Aug 12, 2003
  3. Aredridel

    Not just $SAFE, but damn $SAFE

    Aredridel, Sep 2, 2004, in forum: Ruby
    Replies:
    19
    Views:
    237
  4. Farrel Lifson

    $SAFE =4 safe enough?

    Farrel Lifson, Aug 29, 2006, in forum: Ruby
    Replies:
    7
    Views:
    100
    Eric Hodel
    Aug 31, 2006
  5. John Nagle
    Replies:
    5
    Views:
    464
    John Nagle
    Mar 12, 2012
Loading...

Share This Page