| View previous topic :: View next topic |
| Author |
Message |
kev
Joined: Tue Feb 16, 2010 4:40 pm Posts: 12
|
Posted: Tue Feb 16, 2010 4:46 pm Post subject: Virtually endless precision (C++ query) |
|
|
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 (
// 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 |
|
 |
M-Saunders Moderator

Joined: Mon Apr 11, 2005 1:14 pm Posts: 2881
|
Posted: Tue Feb 16, 2010 4:57 pm Post subject: Re: Virtually endless precision (C++ query) |
|
|
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 |
|
 |
Bazza LXF regular

Joined: Sat Mar 21, 2009 11:16 am Posts: 1381 Location: Loughborough
|
|
| Back to top |
|
 |
kev
Joined: Tue Feb 16, 2010 4:40 pm Posts: 12
|
Posted: Thu Feb 18, 2010 11:05 am Post subject: |
|
|
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 |
|
 |
CJLL LXF regular
Joined: Sat Jul 09, 2005 10:22 pm Posts: 193
|
Posted: Wed Feb 24, 2010 9:58 pm Post subject: |
|
|
| 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 |
|
 |
kev
Joined: Tue Feb 16, 2010 4:40 pm Posts: 12
|
Posted: Fri Feb 26, 2010 2:29 pm Post subject: |
|
|
| 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 |
|
 |
kev
Joined: Tue Feb 16, 2010 4:40 pm Posts: 12
|
Posted: Mon Mar 15, 2010 12:29 pm Post subject: |
|
|
| The answer is here |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|