A message for Linux.com registered users: We are in the process of making changes to the Linux forums. Starting Monday, 8/13/18 at 6:00 PM PT, you will be unable to access the forums. They will re-launch as soon as possible on Wednesday, 8/15/18 with new features and improved functionality. Thank you for your patience and stay tuned for the new improved forums.

February 17, 2010

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 () {
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 d\n\n";
return 0;}

[BTW:- how would i have inserted that as an image?]

Click Here!