0

Why is the number of digits stored by both float as well as double variable same, isn't double supposed to store more digits than float

#include <iostream>
using namespace std;
int main(){
double a = 3.141528579238;
float b = 3.141528579238; 
cout << a << " " << b;
return 0;
}

I'm getting this as my output

3.14153 3.14153

2 Answers2

1

The problem that you are facing is because first you should know that float has 4 bytes and double has 8 bytes and 3.141528579238 maybe can be stored in both and if you want to show more digits i suggest first including: #include <iomanip> then you can use the following to print any amount of digits after the "." : cout << std::setprecision(7) << myFloat; this will display 3.1415285.

Shervin Sorouri
  • 210
  • 2
  • 8
0

It isn't about digits it's about bits!

floats are 32bit and doubles are 64bit. A floating point number can be expressed like this: a*2^x where a < 1 and a => 0. a and x will be stored in either 32- or 64-bit memory.

The reason why your program is printing the same thing is because it just doesn't print your whole number.

Shiro
  • 2,610
  • 2
  • 20
  • 36