Lesson 2.3. Floating point numbers

Float type

The floating type allows to store decimal numbers. Its ful lname is floating point numbers. There are 2 types for encoding decimal numbers in C. As for the integers, it is advisable to choose the appropriate type to minimize the use of memory usage:

Type Bytes Bits Minimum Maximum
float 4 32 ±10-37 ±10+37
double 8 64 ±10-307 ±10+307
long double 16 128 ±10-308 ±10+308

Note: Float types do not support the `unsigned` prefix. Therefore, the `unsigned float` or `unsigned double` types do not exist in C.

Encoding of floats

We won't go into detail about how decimal numbers are encoded in memory. However, it is always interesting to know. If you are interested in this concept, you can consult this video which explains the details of the IEEE 754 standard.

Just remember two important things:

• The precision of floats is not infinite, for example with a float, 1010 is approximated by 99999997952.
• The larger the encoded numbers are, the less accurate they are.

To go further, the video below explains the origins and consequences of these inaccuracies:

Notation

The notation of decimal numbers in C is governed by a few rules:

• A float must have a dot: `12.45` `-24.` `3.1415`
• The dot cannot be replaced by a comma.
• When the integer part is zero, it can be omitted: `0.25` can be written `.25`.
• The exponent is written `e` or `E`, e.g. `10e6` or `10E6`.

Format codes

As for integers, you must use the appropriate format code to display a float. Here are the format codes for each type:

Type Format code
float `%f`
double `%lf`
long double `%Lf`

Exercises

Exercise 1

Here is an example that display a `double` :

``````#include <stdio.h>

int main(void) {

// Declare and display the variable N
double N=10e10;
printf ("%lf", N);

return 0;
}``````

Replace the type of the variable `N` with a `float` and change the format code of the `printf`. What do you notice?

Exercise 2

Write a program that declares a variable `gold_number` of type `double` and initializes it with the value 1.6180 before displaying it.

``````#include <stdio.h>

int main(void) {
// Declare the golden number
// COMPLETE HERE

// Display the golden number
// COMPLETE HERE

return 0;
}``````

Exercise 3

Write a program that declares 2 variables `a` and `b` of types `float` and `double` respectively. The two variables are initialized with the value \(-10^{28}\) then displayed.

``````#include <stdio.h>

int main(void) {
// Declare two variables (float and double)
// COMPLETE HERE

// Display the variables
// COMPLETE HERE

return 0;
}``````

Quiz

Which types are used to store decimal numbers?

How much space does a `long double` take in memory?

Which type allows to memorize the greatest number?

The estimated number of atoms in the universe is \( 10^{80} \). Which type is best suited to store this number?

Which type will be the most precise?

To store a number between -100 and +100, what is the advantage of using a double rather than a float?