Variables & Constants

Lesson 9 - Understanding double Data Type

Learn how double stores decimal numbers, how it compares with float, and when it is useful for higher-precision calculations in Arduino/ESP32 projects.

Progress indicator

Lesson 9 of 9

Learning Objectives

  • Understand that double stores decimal (fractional) numbers like float.
  • Learn double syntax, declaration, initialization, range, and memory size basics.
  • Compare double and float in beginner-friendly language.
  • Use double values for precision-focused calculations and measurements.
  • Avoid common mistakes when working with decimal precision in Arduino/ESP32 code.
  • Understand that precision and memory behavior can vary by board/platform.

Concept Explanation

What is double

double is a decimal number data type, similar to float, used for values with fractional parts such as 3.7125, 0.333333, or -0.0125.

It is often introduced when you need more precision than a basic decimal value example, especially in calculations involving calibration or detailed measurements.

In embedded programming, the exact benefit of double depends on the board. Some boards treat it similarly to float, while others provide more precision.

double Syntax

Basic syntax:

double variableName;
double voltage;
double ratio = 0.333333;
double calibration = -0.0125;

If you are unsure whether to choose float or double, start by learning the calculation goal first (range and required precision), then choose the type.

Beginner rule: choose float for simple decimal lessons, and choose double when you are specifically learning about precision or your project requires it.

Declaring double Variables

Declaring means creating a variable and choosing double as the type so it can store decimal values.

double preciseVoltage;
double calibrationValue;

Initializing double

Initializing means assigning a starting decimal value when you create the variable.

double sensorValue = 2.71828;
double correction = -0.01;

Use decimal literals like 100.0 when your math is intended to be decimal-based.

Beginner tip

Writing values like 4.2000 can improve readability in measurement code by showing intended precision, even if the stored value is approximate internally.

double Range

double can store a wide range of decimal values, but the exact range and precision depend on the board/platform implementation.

Beginner rule: think of double as a decimal type often used when precision matters more than a simple float example.

Like other floating-point types, double can still have precision limits. Very small differences may not always be represented exactly.

double Memory Size

The memory size of double is platform-dependent. On some boards it may be the same as float, while on others it may use more memory for extra precision.

This tradeoff is important in embedded systems: more precision may also mean more memory usage and sometimes slower math operations.

Why double Stores Decimal Numbers

Like float, double is built for values with fractional parts, which makes it suitable for measurements, ratios, and precision-oriented calculations.

This is why types like int are not a good fit for values such as 3.7125 V or calibration offsets like -0.0125.

When to Use double

  • When you need decimal values and may need more precision than float
  • Calibration math and engineering-style calculations
  • Scientific/measurement code where precision matters
  • Board-specific projects where double provides higher precision

If your board treats double like float, the practical behavior may be similar. Always check your board documentation.

Real-Life Example

A calibrated sensor system might measure a voltage, apply a tiny offset, and then convert the result to a percentage. Small decimal differences may matter, which is why this lesson discusses double.

double vs float (Comparison)

Featuredoublefloat
Decimal supportYesYes
PrecisionOften higher (platform-dependent)Common beginner decimal type
Memory usageMay be same as or larger than floatUsually smaller or equal
Beginner useWhen precision needs are discussedDefault decimal lessons

The exact difference depends on the board. On some microcontrollers, double and float may behave very similarly.

Use float when

You need simple decimal values for beginner sensor examples and do not require extra precision.

Use double when

Your board supports higher precision and your calculations benefit from it.

Example Code

This example stores decimal values using double and performs a calibration-style calculation.

double sensorVoltage = 3.7125;
double calibrationOffset = -0.0125;
double referenceVoltage = 4.2000;

void setup() {
  Serial.begin(115200);

  double correctedVoltage = sensorVoltage + calibrationOffset;
  double batteryPercent = (correctedVoltage / referenceVoltage) * 100.0;

  Serial.println("Double values calculated");
}

void loop() {
  // Lesson focus is decimal precision in setup()
}

What this example teaches

  • How to declare and initialize double variables
  • How to apply a calibration offset to a measured value
  • How to compute a percentage using decimal math
  • How to think about precision-oriented decimal calculations

Important reminder

Even when using double, decimal results can still be approximate. The goal is often better precision, not perfect decimal storage.

Example Code Explanation

  1. double sensorVoltage = 3.7125; stores a measured decimal voltage.
  2. double calibrationOffset = -0.0125; stores a negative decimal correction used to adjust the measurement.
  3. double referenceVoltage = 4.2000; stores a decimal reference value for percentage calculation.
  4. double correctedVoltage = sensorVoltage + calibrationOffset; applies the correction to the measurement.
  5. double batteryPercent = (correctedVoltage / referenceVoltage) * 100.0; calculates a percentage using decimal math.
  6. Serial.println(...) prints a message after calculations are done.

Beginner reading tip

Break the code into 3 parts when reading: measured values (sensorVoltage), correction values (calibrationOffset), and final calculated values (correctedVoltage and batteryPercent).

Real-life example

A calibrated measurement system may apply a tiny correction offset before showing a final value. This is a common situation where decimal precision is important.

Common Mistakes with double

  • Assuming double always has more precision than float on every board.
  • Using int when decimal precision is required.
  • Expecting all decimal values to be stored exactly with no precision limits.
  • Using double everywhere without considering memory usage in embedded projects.
  • Comparing decimal values with exact equality in advanced logic without tolerance checks.

Best Practices for double

  • Use double when your board and use case benefit from extra precision.
  • Check your board documentation to understand double size and precision.
  • Use clear names like correctedVoltage and referenceVoltage.
  • For simpler decimal examples, float may be enough and easier to teach first.
  • Use intermediate variables (like correctedVoltage) to keep formulas readable.

Practice Task

  1. Change sensorVoltage to 3.8450 and explain how correctedVoltage changes.
  2. Change calibrationOffset to 0.0050 and explain why a positive offset changes the result differently from a negative offset.
  3. Rename batteryPercent to batteryPercentEstimate for clearer meaning.
  4. Explain when double may be better than float and why board documentation matters.

Try it now

Open the simulator workspace and observe variables while stepping through examples.

Run in Simulator