Question
Float vs Double: Difference in Precision, Range, and Memory
Question
I have read about the difference between single precision and double precision floating-point numbers. However, in many cases, float and double seem interchangeable, because using one or the other does not appear to change the result.
Is that actually true? When are float and double interchangeable, and what are the real differences between them?
Short Answer
By the end of this page, you will understand what float and double are, how they store decimal numbers, why they differ in precision and range, and when choosing one over the other matters. You will also see practical examples of where they behave similarly and where they produce different results.
Concept
float and double are data types used to store floating-point numbers, which are numbers with fractional parts such as 3.14, 0.1, or -12.75.
The main difference is this:
floatusually uses 32 bits of storagedoubleusually uses 64 bits of storage
Because double has more bits available, it can usually store:
- more precise values
- larger numbers
- smaller non-zero numbers
In most modern languages and systems, both follow the IEEE 754 floating-point standard:
floatis typically single precisiondoubleis typically double precision
Why this matters
A floating-point value does not store decimal numbers exactly in many cases. For example, values like 0.1 cannot usually be represented perfectly in binary floating-point.
That means two things:
Mental Model
Think of float and double as two measuring tools:
floatis like a ruler with fewer markingsdoubleis like a ruler with many finer markings
Both can measure length, and for large, rough measurements they may give results that look the same. But if you need careful, detailed measurement, the ruler with finer markings gives a better answer.
Another way to think about it:
floatis a notebook with fewer pages for storing number detailsdoubleis a notebook with more pages
If the number needs more detail than the smaller notebook can hold, some information gets rounded away.
Syntax and Examples
In C-like languages, the syntax often looks like this:
float price = 19.99f;
double total = 19.99;
Notice the f suffix on the float literal. Without it, a decimal literal is often treated as a double by default.
Basic example
#include <iostream>
#include <iomanip>
int main() {
float f = 0.1f;
double d = 0.1;
std::cout << std::fixed << std::setprecision(20);
std::cout << "float: " << f << "\n";
std::cout << "double: " << d << "\n";
}
Possible output:
float: 0.10000000149011611938
double: 0.10000000000000000555
What this shows
Both values are approximations, not exact decimal .
Step by Step Execution
Consider this example:
#include <iostream>
#include <iomanip>
int main() {
float f = 1.0f / 3.0f;
double d = 1.0 / 3.0;
std::cout << std::fixed << std::setprecision(20);
std::cout << f << "\n";
std::cout << d << "\n";
}
Step by step:
1.0f / 3.0fis computed usingfloatprecision.- The result cannot be represented exactly, so it is rounded to the nearest
floatvalue. 1.0 / 3.0is computed usingdoubleprecision.- This also cannot be represented exactly, but it is rounded to a more precise value because
doublehas more bits. - When both values are printed with 20 decimal places, the
doubleoutput usually shows more correct digits.
Typical output:
Real World Use Cases
float and double are both used in real software, but the choice depends on the problem.
Common uses for double
- General scientific or engineering calculations
- Business logic where fractional values are used but exact decimal arithmetic is not required
- Statistical calculations
- Simulation code
- Most everyday application code
Common uses for float
- Graphics programming, such as 3D coordinates, colors, and shaders
- Large arrays of numeric data where memory matters
- Embedded systems with tighter memory budgets
- Machine learning tensors in some contexts
- Game development, where millions of values may be stored and processed
Example scenarios
- A physics engine may use
floatfor position vectors to reduce memory bandwidth - A reporting tool may use
doublefor averages and calculations to reduce precision loss - A file format or GPU API may require 32-bit floating-point values, making
floatthe correct choice
Real Codebase Usage
In real codebases, developers often follow a few practical rules.
1. Use double as the default for general calculations
This is common because:
- it reduces accidental precision problems
- modern CPUs handle
doubleefficiently - it usually makes code more robust
2. Use float when there is a clear reason
Typical reasons include:
- memory savings in large datasets
- compatibility with graphics libraries or hardware
- network or file formats that specify 32-bit floats
- performance tuning on specific platforms
3. Convert at boundaries
A common pattern is to calculate internally with double, then convert to float only when sending data to an API.
double internalValue = 12.345678901234;
float gpuValue = static_cast<float>(internalValue);
4. Validate assumptions when comparing values
Real projects avoid direct equality checks for most floating-point results.
#include <cmath>
{
std::(a - b) < epsilon;
}
Common Mistakes
1. Assuming float and double always give the same result
Broken assumption:
float f = 0.1f;
double d = 0.1;
// These are not the same internal value
Avoid this by remembering that both are approximations, but double is usually more precise.
2. Comparing floating-point values with ==
Broken code:
#include <iostream>
int main() {
double a = 0.1 + 0.2;
if (a == 0.3) {
std::cout << "Equal\n";
}
}
This may fail because of representation error.
Better:
#include <cmath>
bool nearlyEqual {
std::(a - b) < epsilon;
}
Comparisons
| Feature | float | double |
|---|---|---|
| Typical size | 32 bits | 64 bits |
| Precision | About 6–7 decimal digits | About 15–16 decimal digits |
| Range | Smaller | Larger |
| Memory usage | Lower | Higher |
| Default decimal literal type in C/C++ | No | Yes |
| Common use | Graphics, large numeric arrays | General-purpose calculations |
| Risk of precision loss | Higher | Lower |
float vs double in practice
Cheat Sheet
floatusually = 32-bit IEEE 754 single precisiondoubleusually = 64-bit IEEE 754 double precisionfloatprecision: about 6–7 significant decimal digitsdoubleprecision: about 15–16 significant decimal digitsdoubleis usually the safer defaultfloatuses less memory- Many decimal values like
0.1are not exactly representable in either type - Avoid
==for computed floating-point values - Use an epsilon comparison for approximate equality
- In C/C++, decimal literals like
3.14are usuallydouble - Use
3.14ffor afloatliteral floatmay lose small changes when numbers are already large- For money, prefer fixed-point or decimal approaches when exactness is required
Quick syntax
float f = 1.23f;
double d = 1.23;
Approximate comparison
FAQ
Is double always better than float?
Not always. double is usually better for precision, but float is useful when memory, bandwidth, or API compatibility matters.
Why do float and double sometimes print the same value?
Because the difference may be too small to show with default formatting, or the number may be exactly representable in both types.
Can float and double both have rounding errors?
Yes. Both are floating-point types, so many decimal values are stored approximately.
Should I use float or double in C++?
Use double by default unless you have a specific reason to use float, such as memory limits or a graphics API requirement.
Why is 0.1 + 0.2 != 0.3 sometimes true?
Because numbers like 0.1 and 0.2 are usually not represented exactly in binary floating-point, so the result is only approximately .
Mini Project
Description
Build a small C++ program that compares how float and double store and calculate decimal values. This project helps you see precision differences directly instead of only reading about them. It is useful because many bugs involving numeric types are caused by assumptions that small decimal values are stored exactly.
Goal
Create a program that demonstrates where float and double behave similarly and where their precision starts to differ.
Requirements
- Declare one
floatand onedoubleusing the value0.1. - Print both values with high precision.
- Compute
1.0 / 3.0using both types and print the results. - Add a small value to a large number using both types.
- Print clear labels so the output is easy to compare.
Keep learning
Related questions
Array-to-Pointer Conversion in C and C++ Explained
Learn what array-to-pointer conversion means in C and C++, how array decay works, and how it differs from a pointer to an array.
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.
C Pointer to Array vs Array of Pointers: How to Read Complex Declarations
Learn the difference between pointer-to-array and array-of-pointers in C, plus a simple rule for reading complex declarations correctly.