What is the difference between float and double in C++?
Hi! I recently started learning C++ and came across a confusing situation. The textbook says there are two types for floating-point numbers: float and double. I understand that double takes up more memory (8 bytes vs 4), but I'm not entirely clear on when to use which.
For example, I wrote a simple program to calculate the area of a circle:
```cpp
#include
#include
int main() {
float radius = 2.5f;
float area = M_PI * radius * radius;
std::cout << "Area: " << area << std::endl;
return 0;
}
```
Everything works fine. But then I tried to calculate something more complex, like the sum of a large number of fractional numbers:
```cpp
float sum = 0.0f;
for (int i = 0; i < 1000000; ++i) {
sum += 0.1f;
}
std::cout << "Sum: " << sum << s
No answers yet.
Log in to leave an answer