Data and precision loss caused by operations

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. The basics

    /

  4. Variables

    /

  5. Operations

    /

  6. Data and precision loss

Values over the limits

Positive value that is too big for the variable to hold

If a variable of (signed or unsigned) integer primitive type is assigned with a bigger value than it can hold, the value will be subtracted by the number of values the primitive can represent (2n, where n is the size of the primitive in bits), until the variable can hold the value.

Let us say a variable of unsigned integer primitive type with a size of 8 bits (So the range of value it can represent is [0, 255]) is assigned with the number 500. It will actually hold the number 244 (500-256).

Let us say a variable of integer primitive type with a size of 8 bits (So the range of value it can represent is [-128, 127]) is assigned with the value 128. It will actually hold the number -128 (128-256).

Negative value that is too big for the variable to hold

Similarly, if a (signed or unsigned) integer primitive type is assigned with a bigger negative value than what it can hold, the value will be added with the number of values the primitive can represent, until the variable can hold the value.

Let us say a variable of unsigned integer primitive type with a size of 8 bits is assigned with the number -1. It will actually hold the number 255 (-1+256).

Let us say a variable of integer primitive type with a size of 8 bits is assigned with the value -200. It will actually hold the number 56 (-200+256).

Example

On machines with a byte size of 8 bits, the type char can hold values in the range [-128, 127] and the type unsigned char [0, 255]. In the following lines of code:

unsigned char n1 = 1000; unsigned char n2 = -300; char n3 = -220;

The value n1 will actually hold is 232, for n2 it is 212 and for n3 it is -36.

Type conversion

The conversions between primitive types are done automatically.

Here, the value of the variable n2 of type unsigned is converted to int:

unsigned n = 5; int n2 = n; // n2 equals 5.

Both the variable n and n2 equals 5.

After the following lines:

float n3 = 5.6; int n4 = n3;

What will be the value of the variable n4? 5. Why? Because when values of floating point type are converted to integer types, the digits after the decimal points are discarded.

Note that data loss can happen if a number of a certain type gets converted to an other (smaller) type that is not able to hold the value.

We will learn more about type conversion (Also called type casting) later.

Division with integer operands

When numbers do not contain decimal points, they are considered to be of integer type. So the result of the following operation:

5 / 2;

Is 2. The reason is that because the two operands are of integer type, the result also is.

Therefore, in the line below, even though the variable n2 is of floating point type, it assigned with the value 1:

float n2 = 5/2;

How to resolve that? By adding decimal points to the numbers. When a number contains a decimal point, it is considered a floating point number.

float n2 = 5.0/2.0;

In the line above, n2 is assigned with the value 2.5.

It is not an obligation to write digits after the decimal points:

float n2 = 5./2.;