double precision

Discussion in 'C++' started by Noah Roberts, Sep 16, 2009.

  1. Noah Roberts

    Noah Roberts Guest

    I'm a little confused. Reading this site:

    http://www.devx.com/cplus/10MinuteSolution/30891/1954

    I would guess that I lose precision after 15 digits. However, when I
    put PI as a constant in my code as:

    3.1415926535897932384626433832795 (copied from MS calc)

    it doesn't even cause a warning. That's well more than 15 digits.

    What's the compiler going to do with this?
    Noah Roberts, Sep 16, 2009
    #1
    1. Advertising

  2. Noah Roberts

    joseph cook Guest

    On Sep 16, 12:44 pm, Noah Roberts <> wrote:
    > I'm a little confused.  Reading this site:
    >
    > http://www.devx.com/cplus/10MinuteSolution/30891/1954
    >
    > I would guess that I lose precision after 15 digits.  However, when I
    > put PI as a constant in my code as:
    >
    > 3.1415926535897932384626433832795 (copied from MS calc)
    >
    > it doesn't even cause a warning.  That's well more than 15 digits.
    >
    > What's the compiler going to do with this?


    Compiler dependant. It will either round it up or down to the closest
    value that will fit into however many bits a double happens to take up
    on your system.
    HTH,
    Joe Cook
    joseph cook, Sep 16, 2009
    #2
    1. Advertising

  3. Noah Roberts wrote:
    > I'm a little confused. Reading this site:
    >
    > http://www.devx.com/cplus/10MinuteSolution/30891/1954
    >
    > I would guess that I lose precision after 15 digits. However, when I
    > put PI as a constant in my code as:
    >
    > 3.1415926535897932384626433832795 (copied from MS calc)
    >
    > it doesn't even cause a warning. That's well more than 15 digits.


    Why should it cause a warning? It's not like it's not valid C++.
    Juha Nieminen, Sep 16, 2009
    #3
  4. Noah Roberts

    osmium Guest

    "Juha Nieminen" wrote:

    > Noah Roberts wrote:
    >> I'm a little confused. Reading this site:
    >>
    >> http://www.devx.com/cplus/10MinuteSolution/30891/1954
    >>
    >> I would guess that I lose precision after 15 digits. However, when I
    >> put PI as a constant in my code as:
    >>
    >> 3.1415926535897932384626433832795 (copied from MS calc)
    >>
    >> it doesn't even cause a warning. That's well more than 15 digits.

    >
    > Why should it cause a warning? It's not like it's not valid C++.


    That's a good interchange to put in the FAQ. It is a warning that C++ and a
    natural language, such as English, can be very incompatible. In English, it
    deserves a warning, lost precision. In the argot of C++, warning has a
    special, non-obvious, meaning.
    osmium, Sep 16, 2009
    #4
  5. Noah Roberts

    osmium Guest

    "osmium" wrote:

    > "Juha Nieminen" wrote:
    >
    >> Noah Roberts wrote:
    >>> I'm a little confused. Reading this site:
    >>>
    >>> http://www.devx.com/cplus/10MinuteSolution/30891/1954
    >>>
    >>> I would guess that I lose precision after 15 digits. However, when I
    >>> put PI as a constant in my code as:
    >>>
    >>> 3.1415926535897932384626433832795 (copied from MS calc)
    >>>
    >>> it doesn't even cause a warning. That's well more than 15 digits.

    >>
    >> Why should it cause a warning? It's not like it's not valid C++.

    >
    > That's a good interchange to put in the FAQ. It is a warning that C++ and
    > a natural language, such as English, can be very incompatible. In English,
    > it deserves a warning, lost precision. In the argot of C++, warning has a
    > special, non-obvious, meaning.


    Well, that didn't turn out too good. I should have said "The interchange
    cautions the reader that C++ and a...".
    osmium, Sep 16, 2009
    #5
  6. Noah Roberts

    Jerry Coffin Guest

    In article <hsasm.181$>, lid
    says...

    [ ... ]

    > Why should it cause a warning? It's not like it's not valid C++.


    A warning should only every apply to valid code. If the code isn't
    valid, what you get should be an error message instead. A warning
    should normally be saying that the code is valid (for some definition
    of "valid") but still contains something that seems questionable.

    Of course, "questionable" is a judgment call. Different people
    question different things...

    --
    Later,
    Jerry.
    Jerry Coffin, Sep 16, 2009
    #6
  7. Noah Roberts

    Fred Zwarts Guest

    "Noah Roberts" <> wrote in message news:4ab115b1$0$11372$...
    > I'm a little confused. Reading this site:
    >
    > http://www.devx.com/cplus/10MinuteSolution/30891/1954
    >
    > I would guess that I lose precision after 15 digits. However, when I
    > put PI as a constant in my code as:
    >
    > 3.1415926535897932384626433832795 (copied from MS calc)
    >
    > it doesn't even cause a warning. That's well more than 15 digits.
    >
    > What's the compiler going to do with this?


    The compiler will probably round up or down to the closest value that can be represented in a double.
    Why should there be a warning? Even if you would specify exactly 15 digits (or even 10 digits),
    the compiler still needs to round the value to the closest representable binary value.
    Probably in the majority of double precision operations values will be rounded.
    Should the compiler warn everywhere?
    Fred Zwarts, Sep 17, 2009
    #7
  8. Fred Zwarts wrote:
    > "Noah Roberts" <> wrote in message news:4ab115b1$0$11372$...
    >> I'm a little confused. Reading this site:
    >>
    >> http://www.devx.com/cplus/10MinuteSolution/30891/1954
    >>
    >> I would guess that I lose precision after 15 digits. However, when I
    >> put PI as a constant in my code as:
    >>
    >> 3.1415926535897932384626433832795 (copied from MS calc)
    >>
    >> it doesn't even cause a warning. That's well more than 15 digits.
    >>
    >> What's the compiler going to do with this?

    >
    > The compiler will probably round up or down to the closest value that can be represented in a double.
    > Why should there be a warning? Even if you would specify exactly 15 digits (or even 10 digits),
    > the compiler still needs to round the value to the closest representable binary value.
    > Probably in the majority of double precision operations values will be rounded.
    > Should the compiler warn everywhere?


    That's a good point. If you wrote "double d = 0.1;" should the
    compiler give a warning because 0.1 cannot be represented in base-2
    floating point? That might be informative the *first* time someone sees
    it, but quickly becomes annoying and useless.
    Juha Nieminen, Sep 17, 2009
    #8
  9. Noah Roberts

    Rune Allnor Guest

    On 16 Sep, 18:44, Noah Roberts <> wrote:
    > I'm a little confused.  Reading this site:
    >
    > http://www.devx.com/cplus/10MinuteSolution/30891/1954
    >
    > I would guess that I lose precision after 15 digits.  However, when I
    > put PI as a constant in my code as:
    >
    > 3.1415926535897932384626433832795 (copied from MS calc)
    >
    > it doesn't even cause a warning.  That's well more than 15 digits.
    >
    > What's the compiler going to do with this?


    It's one of those things you as programmer just have to know.
    Like "excessive consumption of alcohol might cause intoxication."

    The phenomena (and side effects) might come as a surprise the
    first couple of times, but then one learns and act accordingly.
    Whatever that means.

    Rune
    Rune Allnor, Sep 17, 2009
    #9
    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. Daniel Geisenhoff

    Bug in double precision computing?

    Daniel Geisenhoff, Jul 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    333
    Shan Plourde
    Jul 29, 2004
  2. Wolfgang
    Replies:
    4
    Views:
    5,128
    Andrew Hobbs
    Feb 24, 2004
  3. Replies:
    5
    Views:
    204,498
  4. Andersen

    double precision vs. integers

    Andersen, Dec 18, 2005, in forum: Java
    Replies:
    11
    Views:
    986
    Andersen
    Dec 20, 2005
  5. Sydex
    Replies:
    12
    Views:
    6,486
    Victor Bazarov
    Feb 17, 2005
Loading...

Share This Page