double precision

N

Noah Roberts

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?
 
J

joseph cook

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
 
O

osmium

Juha Nieminen said:
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.
 
O

osmium

osmium said:
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...".
 
J

Jerry Coffin

[ ... ]
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...
 
F

Fred Zwarts

Noah Roberts said:
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?
 
J

Juha Nieminen

Fred said:
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.
 
R

Rune Allnor

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
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top