Floating point numbers.
Floating point types.
Store approximations of real numbers, which in our case can be defined as any number that has a decimal
point and a fractional part such as 0.3333 or 98.6 or P.
Although it's not possible to represent an arbitrary real number exactly in computer memory, but it's
possible to store an approximation.
So if it seems hard to believe, just think of it like number P, which has infinitely many digits with
finite computer memory.
How could you possibly represent infinitely many digits?
As with all types, floating point types take up a finite amount of memory, which is called the type's
precision.
The more precision, a floating point type has, the more accurate it will be.
Approximation.
A real number Cplusplus.
Offer three levels of precision for approximations.
The first level is plot.
The second double and a three long double.
So these levels mean, of course, these are the imaginary levels, but this is the single precision.
We have the double for double precision.
Double precision.
And the next we have the long double for extended precision.
At precision.
So as integer types, each floating point representation depends on implementation.
The section won't go into detail about floating point types, but note that there is substantial nuance
involved in these implementations.
On major desktop operating system, the float level usually has four bytes of precision.
Here.
Four bytes.
Uh, the double and long hair levels usually usually have actually eight bytes.
Eight bytes and long double eight bytes.
Eight bytes.
So most users not involved in scientific computing applications can safely ignore the details of floating
point representation in such cases.
A good general rule is to use double.
For those who cannot safely ignore the details, look at the floating point specifications relevant
to your hardware platform.
And let's go to floating point literals.
Floating point literals.
They are.
Floating point literals.
Yeah.
Wires.
And yes, the floating point literals are double precision by default.
If you need a single precision, use an F or uppercase F suffix for extended precision, use L or uppercase
L, as I will show here float A, for example.
0.1.
F double B, for example, 0.2 just A to 0.2.
We don't need to add any literal or suffix.
And lastly, long double long double C equals 0.3 L.
As you can see, we used the uppercase L and uppercase F.
Actually, let's change the comment here so you can see the codes.
So.
And you can also use the scientific notation in literals here.
Here double, for example, planks.
Banks constant.
The Planck's constant.
The Planck's constant is six point.
626074004
and E -34.
So you might be wondering, what is this, though?
So no space here are permitted between the significant the base and the suffix, the potential portion
and the E for 34.
So that's why we didn't use any suffixes or like any space or literal to use for separation of these
codes, these values.
So the floating point format, specifiers, we have format specifiers for printing the these numbers
on console.
So the format specifier F displays a float with decimal digits, whereas here e.
Displays the same number in scientific notation.
You can let Printf decide which of these two to use with the formats specifier which selects the more
compact of E or F for double.
You simply depend on l.
Lowercase l here to describe desired specifier so for long double you prepend an uppercase l For example,
if you want to want a double with decimal digits, you will specify lf l or l g.
Here.
Now, let me comment here.
Double.
With.
Decimal digits here and.
New world specifier or here with decimal digits, but for a long double you will you will specify long
double double with decimal digits.
Here for long double.
You need to use the uppercase l under under case F and the same uppercase l e or.
LG.
So let's write an example code for this and start and compile and run the program, which I will show
you how this format specifiers work on tables.
So we have the main command main.
A function here.
So we can freely use this as our code.
So double.
And here, uh, 6.0.
221409E 23 which I will explain all of these codes later after compiling and print f.
Uh, for example, Avogadro's number.
Number is.
I will explain all of these codes, of course, here.
L f.
LG and new line near.
Then I will give the three three times the same number but with different format specifiers and n.
N.
Here and.
Was this code here?
And lastly, let's create the float number
9.9..
36.
Here.
Let's print.
Print f.
Hogwarts.
Platform.
Here and e f g new line.
Give this use this format, use this number with different formats specified three times HP, HP, HP
and.
Okay so.
Imprint.
And let's finally compile this code here.
Clear.
U.S.S. Maine.
The ship so called, is compiled using the E option.
Let's save this code firstly with Ctrl save and let's compile it again.
Use this code.
So.
Okay.
Arson.
Yes.
So as you can see here, let me okay.
So as you can see here, the first we did the program declares a double here, double called N the format
specifier.
The format specifier L e.
Gives you a scientific notation of this number.
So let's see what this first.
Gave us.
This is the scientific notation which we used as E.
Is the scientific notation.
The program gives us.
And the second we did here.
Gives the decimal representation of this e n number.
As you can see, this gave us the very long decimal representation of this number.
The last one.
Specifier.
The chooses the scientific notation of these, as you can see here.
This notation gave us almost the same e and number, so.
And here we as you know, we created the plot called each p this produce similar printf output using
the E and F specifiers, as you can see here.
But the format specifier here.
Format specifier Here.
Decided to provide the decimal representation of 9.76 rather than the scientific notation.
As a general, you'll use the G.
Print floating point types.
The next lecture will be about the character types.
And I'm waiting you in the next lecture.