Virtually endless precision (C++ query)

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Virtually endless precision (C++ query)

Postby kev » Tue Feb 16, 2010 4:46 pm

I am new to C++ using GCC but the following program (print-out) gives very much more precision than the number of bytes allocated to a variable of (eg type float) 4 bytes ought to give and errno is always 0. Can anyone enlighten me as to why, please?

using namespace std;
#include <iostream>
#include <errno.h>
int main () {
cout.precision(9999);
float d=1; // 4bytes type - set i to 129 - result is 39 digits (:-o
// or try :-
//double d=1; // 8bytes type - set i to 1025 - 308 digits (:-O
// or try :-
//long double d=1; // 12bytes type - set i to 16385, - fails at i=16384 - 4932 digits? =(:-O
for (int i=1; i<129; i++) { d=d*2; cout<<"err="<<errno<<"ti="<<i<<"t"<<d<<"n"; }
cout<<"n"<< sizeof(d) <<" bytes is sizeof dnn";
return 0;}

[BTW:- how would i have inserted that as an image?]
kev
 
Posts: 12
Joined: Tue Feb 16, 2010 4:40 pm

Re: Virtually endless precision (C++ query)

Postby M-Saunders » Tue Feb 16, 2010 4:57 pm

You don't need to use images -- there's a way to format code using the [code] and [/code] tags as described in the BBCode link when posting and here:

http://www.linuxformat.com/forums/faq.php?mode=bbcode

M
User avatar
M-Saunders
LXF regular
 
Posts: 2893
Joined: Mon Apr 11, 2005 12:14 pm

Postby Bazza » Tue Feb 16, 2010 6:50 pm

Hi kev...

This should answer it far better than I...

http://www.cplusplus.com/reference/iostream/
The second paragraph is the clincher really...

And while you are about it take a look at:-

http://www.java2s.com/Tutorial/Cpp/0100 ... __cout.htm

And apply it to your "iostream"... ;o)

Hope that helps...
73...

Bazza, G0LCU...

Team AMIGA...
User avatar
Bazza
LXF regular
 
Posts: 1483
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby kev » Thu Feb 18, 2010 11:05 am

Thanks for your replies.
Bazza, unfortunately I'm still not able to see how the iostream functions can affect the precision of the core code of my program:- d=d*2; with d being only 4 bytes long.
kev
 
Posts: 12
Joined: Tue Feb 16, 2010 4:40 pm

Postby CJLL » Wed Feb 24, 2010 9:58 pm

kev wrote:Thanks for your replies.
Bazza, unfortunately I'm still not able to see how the iostream functions can affect the precision of the core code of my program:- d=d*2; with d being only 4 bytes long.


It doesn't. It only affects what you see in the iostream.

Code: Select all
     cout<<"err="<<errno<<"ti="<<i<<"\t"<<d<<"\tsize: "<<sizeof(d)<<"\n";
 
--
The reward for self love is sticky hands
CJLL
LXF regular
 
Posts: 193
Joined: Sat Jul 09, 2005 9:22 pm

Postby kev » Fri Feb 26, 2010 2:29 pm

Thanks, CJLL. Given that the print-out digits are the correct values (as far as I can make out), can you explain why the compiler appears to be assigning 128 bits to d instead of 32 bits? Could it be a bug, do you think?
kev
 
Posts: 12
Joined: Tue Feb 16, 2010 4:40 pm

Postby kev » Mon Mar 15, 2010 12:29 pm

The answer is here
kev
 
Posts: 12
Joined: Tue Feb 16, 2010 4:40 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests