- We just finished discussing integer points,
how much memory they take,
the range of usage and so on.
Let's have the same discussion or floating-point
and see some of the differences in size and requirements
and capability once you move from the world of integers,
to the world of decimals.
Let's start with the float.
A float already starts at four bytes
and it only goes up to eight and ten
where if you recall the long int was the largest one
at eight bytes and you had int as shortest two and one byte.
Let's just look at the stored range.
Notice that float is listed at output precision
of six decimal places.
This is only what you see on the screen.
Internally you see the range goes from
1.2 times 10 to the minus 38th
or 3.4 times 10 to the 38th.
This means 38 digits past a decimal point.
The long double is a staggering 4900 digits
past the decimal point.
Let's take a look at the code and see how this transpires.
When you write code involving floating-point calculations,
there's another thing you have to be mindful of.
The precision, it's not just memory
but it's also a usability issue.
Let's look at line 27 through 29.
I'm performing a very simple calculation,
I am dividing two by three
so we know it should be about 0.666 depending
on what precision you want to.
Let's see what is the difference
between performing those operations
in the case of using float, double, a long double numbers.
Let's run this code.
If you look at the size of the variables,
the data type float, double and long double
are four, eight, and 16 bytes respectively.
We use the same size of function that we use
to assess integers.
Now let's look at the precision at four decimal points
and then I will show you how did I come up
with this precision using the printf command.
If all I'm interested in four decimal places,
on one hand you could say it doesn't really matter
if you use float, double or long number.
However, knowing that internally,
long doubles are stored with 4900 digits
beyond a decimal place and knowing that two divided by three
never ends, it's a sequence of sixes that never end.
There is no point in using the long double number
if all you want is four decimal.
There's no need store 4900 of them.
Let's go further and look at what happens
at the precision of 10 decimal points.
Now we started to see something interesting.
The numbers are no longer the same.
Double and long are equal because
they have a higher precision in calculation to float,
so we have to make a decision.
It's not only about memory saving
but it's about how correct your results should be.
If you are dealing with prices, stock information,
trends in market and so on,
you probably don't need that many decimal points anyway,
fours are plenty.
If you're doing computational biology, physics,
science, trajectory of rockets, you really can use all
the precision you can get.
If you go further down,
let's look at what happened with the precision
at 30 decimal points.
Well float number is not even capable
of getting any kind of accuracy
beyond seven or eight digits.
Or the double and long double keep going forever
to some extent.
You know that when you divide two by three,
in theory they should be an infinite sequence of sixes
at the very end when you finally choose to round it
it should be a seven
so how come those numbers are very, very different
than those sixes?
We'll get into this probably in a different course
when we'll get into the mathematics
behind those calculations.
For now, it suffices to say that all you have to know
is the memory size each float or integer requires
and what type of memory requirements
your solution in hand meets.
We have talked a little bit about integer sizes,
the variety of integer type available to use,
so let's do the same for float.
You already see just looking at the size column
at four byte, eight byte, and 16 bytes.
Floating type take a considerably more amount
of memory than integers.
If you look at the stored range size,
just look at the long double,
a staggering number of decimal points,
4932 digits past the decimal point.
Unless you really need to calculate things
that are extremely demanding in terms of their precision,
you probably don't need to use long double.
Why would you use a long double,
if you can use a float
that still gives you six decimal places
and stores internally 38 decimal points
and only takes a quarter of the memory
than a long double has.