- [Instructor] C++ provides some common
basic floating point types.
This is float.type.cpp from chapter three
of the exercise files.
C++ provides these floating point types,
we have float, double and long double.
And if we build and run this,
we can see that float is 32 bits,
double is 64 bits
and long double it says it's 128 bits.
It's really only using 80 bits of that
which is the IEEE 80 bit long double type.
On many systems including Microsoft Visual C++,
long double is simply a synonym for double
the precision of this type varies.
You can indicate a floating point literal
by using a decimal point.
So I can say F equals 500.0
and that will assign the floating point value 500.
If I don't put in the .0
the literal is actually an integer
but it will be automatically promoted in the assignment.
But you can specify a literal floating point number
by using a decimal point
and if I go ahead and print this,
(typing)
you'll see that the value is indeed 500.
Or alternately I can use scientific notation,
I can say 5e2
and I get the same result.
So in discussing floating point numbers
it's important to understand the distinction
between scale and precision.
Scale is how large or how small a number is,
precision is the accuracy of the representation
to a number of significant digits.
What this means is when you use floating point numbers
you may get rounding errors,
obviously the larger floating point numbers
the double precision,
the long double precision
are going to have more digits of precision,
but all of them are limited.
So if I do this, if I say F equals
.1 + .1 + .1
you would expect the result to be .3.
And if I go ahead and run this,
you'll see that indeed it says that the result is .3.
And yet if I come down here and I say,
if
F equals
0.3
(typing)
you'll notice that this is not true.
You see, I get the result false
and the reason for that we can see this
if we display the full precision.
I'm going to use a modifier here to display more digits
of precision in the print F
and you'll see that the value is not actually .3
it's 0.30119.
So you can see that the precision is a little bit off,
the scale is what the scale is
because we're using a floating point number.
We can use a lot of scale
but we're sacrificing precision for scale.
So with floating point numbers
it's possible to represent very large
and very small values
with the precision of these types is always compromised.
Now I could use a double float instead
and I believe I need to put an L here,
and when I build and run this,
you say, okay well now I'm getting more precision here
because I've got a larger size floating point numbers.
So we'll look at 20 digits instead of 10 digits
and when I build and run this,
you see, well we out there towards the 20 digits,
we still got a rounding error.
And so that equality is still false
even though a .1, .1 and .1 should equal .3
it's still not quite there.
So if precision is important
for an application like accounting,
you don't want to use floating point numbers,
you want to use an integer instead.
C++ provides these standard floating point types
and while it's possible represent very large,
and very small numbers with these types.
It is at the expense of precision,
if precision is important as in the case of accounting
you want to use an integer type instead.