Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > double precision

Reply
Thread Tools

double precision

 
 
Noah Roberts
Guest
Posts: n/a
 
      09-16-2009
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?
 
Reply With Quote
 
 
 
 
joseph cook
Guest
Posts: n/a
 
      09-16-2009
On Sep 16, 12:44*pm, Noah Roberts <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      09-16-2009
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++.
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      09-16-2009
"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.


 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      09-16-2009
"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...".


 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      09-16-2009
In article <hsasm.181$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed)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.
 
Reply With Quote
 
Fred Zwarts
Guest
Posts: n/a
 
      09-17-2009
"Noah Roberts" <(E-Mail Removed)> wrote in message news:4ab115b1$0$11372$(E-Mail Removed)...
> 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?
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-17-2009
Fred Zwarts wrote:
> "Noah Roberts" <(E-Mail Removed)> wrote in message news:4ab115b1$0$11372$(E-Mail Removed)...
>> 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.
 
Reply With Quote
 
Rune Allnor
Guest
Posts: n/a
 
      09-17-2009
On 16 Sep, 18:44, Noah Roberts <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java double precision info@censnet.it Java 5 06-10-2009 11:10 AM
double precision vs. integers Andersen Java 11 12-20-2005 11:02 AM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Bug in double precision computing? Daniel Geisenhoff ASP .Net 1 07-29-2004 01:58 PM
high precision Math with 'double' (underflow problem) Wolfgang Java 4 02-24-2004 03:41 AM



Advertisments