Skip to main content

Section 4.3 Floating-Point Division

Whole number value like 60 can be stored as either an int or a double. But even though they seem like the same number, C++ considers the integer value 60 different from the floating-point value 60.0. They belong to different data types, and thus doing math with them can produce different results. This is most obvious when we do division.
If I divide 1.3 by 2.65, it is pretty clear that I expect a decimal (floating-point) answer. Thus C++ performs floating-point division and produces a decimal value. Using that knowledge, we can now calculate decimal value, like β€œwhat part of an hour is 45 minutes?”. We just have to make sure we use floating-point values in the calculation:
Listing 4.3.1.
Recall that if we divided one integer by another, like 45 / 60, we would get 0 for our answer. C++ assumes that if you start with two integers, you want an integer answer. But if you ask to do division involving a floating-point value, like 45.0 / 60.0, it will assume you want to do floating-point division and produce a decimal answer. It will make the same assumption even if there is just one floating point value involved, like 45 / 60.0.
Identifying whether an operation carries out integer division or floating point division can get tricky when we have a mix of integers and doubles in our expression. The thing to remember is if either the divisor or the dividend is a double then the program will carry out floating point division.
Listing 4.3.2. Run the code to see what type of division occurs each time.

Insight 4.3.1.

As long as either operand is a floating-point value, the operator will use floating-point math. If both operands are integers, the operator will use integer math.
Doing integer math when you want to do floating-point math (with something like 1 / 3) is a common logic error that can be hard to catch, since your program will compile without warning but produce the wrong kind of answer.
This is true for other operations as well. If you add 4 to 2.5, the compiler assumes you want a decimal answer. However, this decision is made at each step of the calculation, without considering what might happen later. So the order of operations can matter. For example, consider the following code:
Listing 4.3.3.
You might expect value to be set to the result of 0.33333 times 3.14159. But the first step in evaluating the expression 1 / 3 * 3.14159 is dividing 1 by 3. Those are both integers, so C++ does whole number division and gets the result 0. That makes the expresion into 0 * 3.14159 It does not matter that later the math involves a double, or that the result is stored into a double. At the point of the division, the math involves two integers, so the result is an integer. When the 0 is multiplied by 3.14159 it is promoted to a double β€” 0.0 β€” but that is too late to help produce the result we would expect.
One way to solve this problem (once you figure out the logic error) is to make sure that if you want a decimal answer, that you do math with decimal values. This code works as expected:
Listing 4.3.4.

Checkpoint 4.3.1.

Checkpoint 4.3.2.

You have attempted of activities on this page.