Skip to content
C++ Better Explained
Go back
C++ float vs double: Which One Should You Use?

C++ float vs double: Which One Should You Use?

Both float and double store decimal numbers like 3.14 or 0.001, but they are not interchangeable. The short answer is simple: use double unless you have a specific reason not to. This article explains why, and what actually changes when you pick one over the other.


The Core Difference: Size and Precision

A float is a single-precision floating-point number. It uses 4 bytes (32 bits) and stores roughly 7 significant decimal digits.

A double is a double-precision floating-point number. It uses 8 bytes (64 bits) and stores roughly 15 significant decimal digits.

#include <iostream>
using namespace std;

int main() {
    cout << "float:  " << sizeof(float) << " bytes\n";
    cout << "double: " << sizeof(double) << " bytes\n";
    return 0;
}

Output:

float:  4 bytes
double: 8 bytes

The name “double” literally means it uses double the storage of a float, and gets you double the precision in exchange.


Seeing Precision Loss in Action

This is the part that trips up beginners. A float can only remember about 7 digits, so extra digits silently disappear.

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    float  f = 3.14159265358979;
    double d = 3.14159265358979;

    cout << setprecision(15);
    cout << "float:  " << f << "\n";
    cout << "double: " << d << "\n";
    return 0;
}

Output:

float:  3.14159274101257
double: 3.14159265358979

Notice the float version drifts after the 7th digit — it stored the closest value it could, not the exact number you typed. The double keeps all 15 digits you gave it. This is efficient to know because those tiny errors add up across thousands of calculations.


Default Type of Decimal Literals

Here’s a subtle gotcha. When you write a plain decimal like 3.14 in your code, C++ treats it as a double by default, not a float.

float x = 3.14;    // 3.14 is a double, then narrowed to float
float y = 3.14f;   // the f suffix makes it a float literal

Adding the f suffix tells the compiler “this is a float.” Without it, the value starts life as a double and gets converted. Most compilers will warn you about this narrowing, which is another quiet hint that double is the expected default in C++.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

When to Actually Use float

Double is the default, but float earns its place in a few situations:

For everyday programs — calculators, physics for a school project, financial-style math, general algorithms — reach for double.


Why double Is the Safe Default

Beyond precision, there’s a practical reason: the C++ standard library math functions in <cmath> are written for double.

#include <cmath>

double r = sqrt(2.0);   // sqrt returns double
double a = sin(1.5);    // trig functions return double

If you feed these functions a float, the value gets promoted to double anyway, the calculation runs in double, and then you’d narrow it back. Starting with double avoids the round trip and the precision loss that comes with it.


Quick Comparison

Featurefloatdouble
Size4 bytes8 bytes
Precision~7 digits~15 digits
Literal suffix3.14f3.14 (default)
Best forgraphics, huge arrays, embeddedalmost everything else
Standard math functionspromoted to doublenative

The One-Line Rule

If you remember nothing else: default to double, and only switch to float when memory or a specific API demands it. You’ll avoid an entire class of subtle precision bugs that beginners spend hours debugging.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
C++ enum class vs enum: Scoped Enums Explained
Next Post
C++ new vs malloc: What's the Difference and Which to Use

Keep Learning