Back to C++
2026-02-025 min read

Floating-point Literals (C++)

Learn Floating-point Literals (C++) step by step with clear examples and exercises.

Why This Matters

Understanding Floating-Point Literals is crucial for working with real numbers in C++ programs. Real numbers are essential in various applications such as scientific calculations, game development, machine learning, and more. Mastering Floating-Point Literals can help you avoid common mistakes, improve the efficiency of your code, and ace technical interviews.

Prerequisites

Before diving into Floating-Point Literals, make sure you have a good understanding of:

  1. C++ Basics: Variables, operators, control structures, functions, and input/output operations.
  2. Data Types: Understanding integer types (int, long, short) and character type (char).
  3. Basic Input/Output: Using cin and cout for reading from and writing to the console.
  4. Basic Math Operations: Familiarity with basic mathematical operations like addition, subtraction, multiplication, and division.
  5. Understanding of structures and classes in C++ (optional but recommended).
  6. Knowledge of conditional statements like if-else and switch-case.
  7. Familiarity with loops such as for, while, and do-while.

Core Concept

Floating-Point Literals represent real numbers with a fractional part in C++. They are used to perform calculations that require decimal points, such as calculating the area of a circle or solving mathematical equations. The basic syntax for defining Floating-Point Literals is:

float variable_name = value;
double variable_name = value;
long double variable_name = value;

Here's a breakdown of these declarations:

  1. float: A single-precision floating-point number with 32 bits, providing approximately 7 digits of precision.
  2. double: A double-precision floating-point number with 64 bits, offering approximately 15 digits of precision.
  3. long double: An extended-precision floating-point number with at least 64 bits, providing a higher level of precision compared to both float and double.

Floating-Point Notation

C++ supports several ways to write Floating-Point Literals, including:

  1. Decimal notation (default): 3.14 or 0.0078
  2. Scientific notation: 3.14e+2 (3.14 multiplied by 10 raised to the power of 2) or 0.0078e-3 (0.0078 divided by 10 raised to the power of 3)
  3. Hexadecimal notation: 0x1.fpc (where fpc is a fraction part in hexadecimal)
  4. Binary notation: 0b1.fpb (where fpb is a fraction part in binary)

Floating-Point Formats

When a floating-point number is stored in memory, it consists of three parts: sign, exponent, and mantissa (fraction). The IEEE 754 standard defines the binary representation of floating-point numbers in C++.

Subheadings under Floating-Point Formats:

  • Normalized Format
  • Denormalized Format
  • Zero and Infinity Values
  • NaN (Not a Number) Values

Worked Example

Let's create a simple program that calculates the area of a circle using a float variable and another using double:

#include <iostream>

int main() {
const float pi = 3.14f; // Using 'f' to ensure float type
const double Pi = 3.14; // Defaults to double
int radius = 5;

float circle_area_float = pi * radius * radius;
double circle_area_double = Pi * radius * radius;

std::cout << "Circle area (float): " << circle_area_float << std::endl;
std::cout << "Circle area (double): " << circle_area_double << std::endl;

return 0;
}

Subheadings under Worked Example:

  • Analyzing the Output
  • Understanding the Role of pi and Radius Variables
  • Comparing the Results between float and double

Common Mistakes

  1. Omitting the 'f' or 'F' suffix for float literals: This can lead to unexpected behavior when the compiler assumes a double value instead.
  2. Incorrectly using scientific notation: Be careful with the exponent and ensure that it is in the correct format (e.g., 3.14e+2 instead of 3.14e2).
  3. Misunderstanding the precision of different floating-point types: Using a float when double or long double would provide more accuracy can lead to inaccurate results.
  4. Neglecting to initialize floating-point variables: Uninitialized floating-point variables often contain garbage values, which may cause unexpected behavior in your code.
  5. ### Subheadings under Common Mistakes:
  • Forgetting the 'f' or 'F' suffix for float literals when initializing constants (e.g., const float pi = 3.14;)
  • Incorrectly using scientific notation in expressions (e.g., 3.14e+2 + 2 instead of (3.14 * 10) + 2)
  • Ignoring the precision differences between floating-point types when choosing a data type for a specific use case
  • Failing to initialize floating-point variables before using them in calculations

Practice Questions

  1. Write a program that calculates the square root of a number using Newton's method with a float variable and another using double.
  2. What happens when you omit the 'f' or 'F' suffix for a float literal? Explain the behavior in terms of precision and accuracy.
  3. Given the following code snippet, what is the output of the program?
#include <iostream>
float pi = 3.14;
int main() {
int radius = 5;
float circle_area = pi * radius * radius;
std::cout << "Circle area: " << circle_area << std::endl;
return 0;
}

FAQ

  1. What is the difference between float, double, and long double in C++?
  • float provides approximately 7 digits of precision, while double offers around 15 digits of precision. long double offers even higher precision compared to both float and double.
  1. Why should I use scientific notation for floating-point literals?
  • Scientific notation can help make large or small numbers easier to read and write, as well as reduce the risk of errors when typing decimal points.
  1. What is the IEEE 754 standard, and how does it affect floating-point numbers in C++?
  • The IEEE 754 standard defines the binary representation of floating-point numbers, which helps ensure compatibility between different systems and programming languages. In C++, this standard dictates how floating-point numbers are stored in memory and manipulated during calculations.
  1. What is the difference between a normalized format and denormalized format for floating-point numbers?
  • A normalized format represents a floating-point number with its mantissa having at least one non-zero digit before the binary point, while a denormalized format represents a floating-point number with its mantissa having only zeroes before the binary point. Denormalized numbers are used to represent very small values.
  1. What happens when a floating-point number is converted from one type (e.g., float) to another (e.g., double)?
  • When converting a floating-point number from one type to another, the value may be rounded or truncated depending on the new data type's precision and the original value's magnitude. It is essential to understand the potential loss of precision when performing such conversions.
Floating-point Literals (C++) | C++ | XQA Learn