Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Virtually endless precision (C++ query)

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
kev



Joined: Tue Feb 16, 2010 4:40 pm
Posts: 12

PostPosted: Tue Feb 16, 2010 4:46 pm    Post subject: Virtually endless precision (C++ query) Reply with quote

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 (Surprised
// 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?]
Back to top
View user's profile Send private message
M-Saunders
LXF regular


Joined: Mon Apr 11, 2005 1:14 pm
Posts: 2893

PostPosted: Tue Feb 16, 2010 4:57 pm    Post subject: Re: Virtually endless precision (C++ query) Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
Bazza
LXF regular


Joined: Sat Mar 21, 2009 11:16 am
Posts: 1477
Location: Loughborough

PostPosted: Tue Feb 16, 2010 6:50 pm    Post subject: Reply with quote

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__Development/0040__cout.htm

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

Hope that helps...
_________________
73...

Bazza, G0LCU...

Team AMIGA...
Back to top
View user's profile Send private message
kev



Joined: Tue Feb 16, 2010 4:40 pm
Posts: 12

PostPosted: Thu Feb 18, 2010 11:05 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
CJLL
LXF regular


Joined: Sat Jul 09, 2005 10:22 pm
Posts: 193

PostPosted: Wed Feb 24, 2010 9:58 pm    Post subject: Reply with quote

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:

     cout<<"err="<<errno<<"ti="<<i<<"\t"<<d<<"\tsize: "<<sizeof(d)<<"\n";
 

_________________
--
The reward for self love is sticky hands
Back to top
View user's profile Send private message
kev



Joined: Tue Feb 16, 2010 4:40 pm
Posts: 12

PostPosted: Fri Feb 26, 2010 2:29 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
kev



Joined: Tue Feb 16, 2010 4:40 pm
Posts: 12

PostPosted: Mon Mar 15, 2010 12:29 pm    Post subject: Reply with quote

The answer is here
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast