Hello again! In this video, we are going to look at floating-point output format.
I have to admit this is something I have never used in work, but it would have been useful at university.
Except that we used BASIC and FORTRAN for everything, not C++.
If we have a fairly normal sized number, say, less than 1,000,000, it will be displayed to 6 significant
figures.
Here we have pi to 10 decimal places.
The first 6 significant figures are the 3, and then the 1, 4, 1, 5, 9.
So that is just going to be displayed as 3.14159.
If you are dealing with bigger or smaller numbers, if you are a Physics student, for example, then
scientific notation is used.
In this scientific notation.
We have one digit, followed by a decimal point, followed by the rest of the digits. That is known as
the mantissa.
And then we have this exponent, which represents a power of 10.
So e + 008 means 10 to the power of eight, which is 100 million.
So that means we multiply 2.99 by 100 million, so we end up with 299
million.
If you do not like doing mental arithmetic, you can think of this exponent as being how many times
you need to move the decimal point to the right, to get the right value of the number, the right magnitude.
So +8 means 1, 2, 3, 4, 5, and then we need to add some 0's to make up the rest
of the number.
So 6, 7, 8.
So we have three 0's at the end.
And that is the correct value of the number.
If the exponent is negative, -6, that means divide by one 1,000,000 or move the decimal point
6 places to the left.
Again, we need to add 0's, this time at the front, and we end up with
0.000001.
e +0 means multiply by 1. Do nothing.
So 3.14159 is still 3.14159.
So the default is for the stream to use 6 significant figures. Or scientific notation if there is more
than 6 digits, which are all before the decimal point or all after it.
So we get 3.14159 and 2.99e+008.
If you want to use scientific notation for all your output, to make sure that your data [looks] consistent,
for example, there is a "scientific" manipulator.
So this will force the stream to use scientific notation.
So anything that is pushed onto the stream after you have done this will appear in scientific notation.
There is also an "uppercase" manipulation.
If you do not like having a lowercase 'e' in the exponent, you can use this and it will produce an uppercase
'E' instead.
These are both sticky manipulators, by the way.
So once you push these on the stream, they will permanently change the behaviour of the stream.
So here we have pi again, and we are going to display it in scientific notation.
There we are, with both lower and upper case.
On the other hand, if you do not want to use scientific notation at all, there is fixed-point notation.
And the "fixed" manipulator will do that.
So here we are using the fixed manipulator.
The result of this is that the stream will always display things to 6 decimal places.
So if we have "c" again, it is going to add the decimal point and then 6 decimal places. And there is
no data [for the decimal places]
So it is going to add 0's to make it up.
So this is going to padded out to be 299792458.000000
zero.
It is possibly a bit pointless!
And you will also find out the reason why scientific notation was invented: so you can display numbers
which are too big or too small to be stored in the format.
If we have the charge on the electron, which is 10 to the minus 19, so we need to shift the decimal
points 19 places to the left. Which is probably going to go over here somewhere!
So this is a very small number.
And the problem is that a double can only store 15 decimal places. To store
this accurately will need 19 decimal places.
So in fact, all this data will be truncated and we end up with 0.
So fixed is sticky. In fact, all the manipulators in this video are sticky, so they will permanently
change the state of the stream.
If anyone tries to do floating-point output later on in the program, not just in the same function,
but anywhere in the program, after you change the stream, they are going to get whatever you set it
to. And that might not be what they expect.
So we should be considerate to other programmers. Or maybe to ourselves in a few months' time!
So once we have finished doing this output, we use the "defaultfloat" manipulator and that will push everything
back to the defaults.
One final thing you can do is to change the number of digits that are displayed.
The default is 6 for defaultfloat and fixed.
If you want to change that, there is a setprecision manipulator.
And this takes an argument, which is the number of digits you want to display.
So if we set that and then send pi down the stream, it is going to display 3 significant digits,
which is 3.14.
So to test this out, we are going to display pi using 3 significant figures and then using 6,
which is the default.
So we actually set the precision, and then we set it back to the default.
So we are actually being a bit more public-spirited here!
So when we switched the precision to 3, that gives 3 significant figures, 3.14, and then when
we go back to the default 6, we get the default output.
Okay, so that is it for this video. I will see you next time.
But until then, keep coding!