Variables and Fundamental Types

Floating-Point Types

Floating point types store numbers that contain a fraction of a whole number. Examples include 3.14, negative 0.5, and 0.001.

There are two floating point types in C++ which occur most frequently:

#include <iostream>

int main()
{
    float f{3.14f};
    double d{3.14};

    std::cout << f << '\n';
    std::cout << d << '\n';
}
  • float, which is smaller and usually runs faster on some systems, but is not as accurate as the other type.

  • double, which generally uses twice as much memory but gives a much more precise result. This is the type that is recommended for use in most calculations.

There is one further floating point type, long double, which can be used to offer still higher precision; however, this is seldom needed in most programs.

#include <iostream>

int main()
{
    double price{19.99};
    double tax_rate{0.08};
    double tax{price * tax_rate};
    double total{price + tax};

    std::cout << "Price: " << price << '\n';
    std::cout << "Tax:   " << tax << '\n';
    std::cout << "Total: " << total << '\n';
}

Each variable takes a name and a starting value.

The sum is constructed so that the arithmetic being executed is easy to understand.

Floating point numbers are typically stored in binary to a limited number of bits, and not all decimal values can be accurately represented.

Hence, an expression like 0.1 + 0.2 may not evaluate to exactly 0.3 but to some value very close indeed, like 0.30000000000000004. In most programs, this small difference is of no consequence.

However, it will become obvious if you compare two floating point numbers using the equality operator, ==, or if you carry out a massive sequence of computations.

  • If possible, always use double. If memory is proved to be an issue, then reconsider.

  • Always initialize floating point variables at declaration.

  • Use descriptive names that make the purpose of the variable evident.

  • Don't compare two floating point numbers for exact equality. Rather, check that their difference is smaller than some tiny value when exactness is needed.

Exercise: Floating-Point TypesStarter code, hints and solutionOpenClose
Guided practiceBeginner · 10–15 min

Practice: Floating-Point Types

Work through the mission, reveal hints only when needed, and compare your result with the model solution.

Your challenge

Change price to 25.0 while leaving tax_rate at 0.08. Calculate tax and total before running the program.

Mission checklist

  1. Run the starter as a baseline, then predict the change requested in Your challenge.
  2. Make that change in your editor; use the lesson-specific hints if you need help.
  3. Compile and run your answer, then compare with the worked solution and explain the result.

Starter program

This is the working baseline, not the completed answer. Compile as C++20, then make the change described above.

#include <iostream>

int main()
{
    double price{19.99};
    double tax_rate{0.08};
    double tax{price * tax_rate};
    double total{price + tax};

    std::cout << "Price: " << price << '\n';
    std::cout << "Tax:   " << tax << '\n';
    std::cout << "Total: " << total << '\n';
}

Hints

Hint 1

Compute tax from price * tax_rate before computing total from price + tax. The rate 0.08 represents eight percent, not eight whole units.

Hint 2

Change price in its initializer so both later initializers use the new value. Decimal floating-point output may be rounded; it is not exact decimal-money arithmetic.

Worked solution

price is now 25.0. With the unchanged tax_rate of 0.08, tax is 2 and total is 27. The dependent variables are initialized after price, so they use its new value.

#include <iostream>

int main()
{
    double price{25.0};
    double tax_rate{0.08};
    double tax{price * tax_rate};
    double total{price + tax};

    std::cout << "Price: " << price << '\n';
    std::cout << "Tax:   " << tax << '\n';
    std::cout << "Total: " << total << '\n';
}

Example output

Price: 25
Tax:   2
Total: 27

0 comments

No comments yet. Be the first.

Add a comment

About CppLearn

CppLearn is an interactive, free reference for modern C++. In each lesson, you'll find out how the language works, rather than just lines of code that you can copy and paste. We cover concepts starting from beginner lessons on fundamentals and proceed to templates, move semantics, smart pointers, and areas that usually trap or scare C++ learners.