WEBVTT

00:01.320 --> 00:05.340
Hello again! In this video, we are going to look at floating-point output format.

00:05.910 --> 00:11.010
I have to admit this is something I have never used in work, but it would have been useful at university.

00:11.610 --> 00:15.690
Except that we used BASIC and FORTRAN for everything, not C++.

00:17.160 --> 00:23.280
If we have a fairly normal sized number, say, less than 1,000,000, it will be displayed to 6 significant

00:23.280 --> 00:23.700
figures.

00:24.420 --> 00:27.090
Here we have pi to 10 decimal places.

00:28.290 --> 00:33.150
The first 6 significant figures are the 3, and then the 1, 4, 1, 5, 9.

00:33.870 --> 00:37.500
So that is just going to be displayed as 3.14159.

00:38.340 --> 00:44.920
If you are dealing with bigger or smaller numbers, if you are a Physics student, for example, then

00:44.920 --> 00:46.770
scientific notation is used.

00:47.700 --> 00:49.700
In this scientific notation.

00:49.710 --> 00:55.590
We have one digit, followed by a decimal point, followed by the rest of the digits. That is known as

00:55.590 --> 00:56.550
the mantissa.

00:57.270 --> 01:01.620
And then we have this exponent, which represents a power of 10.

01:02.250 --> 01:06.990
So e + 008 means 10 to the power of eight, which is 100 million.

01:07.560 --> 01:14.300
So that means we multiply 2.99 by 100 million, so we end up with 299

01:14.400 --> 01:15.030
million.

01:16.110 --> 01:21.720
If you do not like doing mental arithmetic, you can think of this exponent as being how many times

01:21.720 --> 01:27.180
you need to move the decimal point to the right, to get the right value of the number, the right magnitude.

01:28.170 --> 01:35.820
So +8 means 1, 2, 3, 4, 5, and then we need to add some 0's to make up the rest

01:35.820 --> 01:36.330
of the number.

01:36.570 --> 01:37.620
So 6, 7, 8.

01:37.980 --> 01:39.540
So we have three 0's at the end.

01:40.500 --> 01:43.110
And that is the correct value of the number.

01:44.950 --> 01:52.150
If the exponent is negative, -6, that means divide by one 1,000,000 or move the decimal point

01:52.420 --> 01:53.680
6 places to the left.

01:54.100 --> 02:01.390
Again, we need to add 0's, this time at the front, and we end up with

02:01.480 --> 02:01.780
0.000001.

02:03.960 --> 02:07.050
e +0 means multiply by 1. Do nothing.

02:07.440 --> 02:11.280
So 3.14159 is still 3.14159.

02:14.810 --> 02:20.540
So the default is for the stream to use 6 significant figures. Or scientific notation if there is more

02:20.540 --> 02:25.430
than 6 digits, which are all before the decimal point or all after it.

02:29.890 --> 02:35.410
So we get 3.14159 and 2.99e+008.

02:38.640 --> 02:45.690
If you want to use scientific notation for all your output, to make sure that your data [looks] consistent,

02:45.690 --> 02:48.330
for example, there is a "scientific" manipulator.

02:48.900 --> 02:51.750
So this will force the stream to use scientific notation.

02:52.350 --> 02:57.480
So anything that is pushed onto the stream after you have done this will appear in scientific notation.

02:59.580 --> 03:02.040
There is also an "uppercase" manipulation.

03:02.370 --> 03:08.610
If you do not like having a lowercase 'e' in the exponent, you can use this and it will produce an uppercase

03:08.610 --> 03:09.480
'E' instead.

03:11.220 --> 03:13.680
These are both sticky manipulators, by the way.

03:14.040 --> 03:18.720
So once you push these on the stream, they will permanently change the behaviour of the stream.

03:24.790 --> 03:28.750
So here we have pi again, and we are going to display it in scientific notation.

03:34.460 --> 03:36.490
There we are, with both lower and upper case.

03:39.200 --> 03:44.600
On the other hand, if you do not want to use scientific notation at all, there is fixed-point notation.

03:45.410 --> 03:48.560
And the "fixed" manipulator will do that.

03:53.570 --> 03:56.240
So here we are using the fixed manipulator.

03:56.540 --> 04:02.600
The result of this is that the stream will always display things to 6 decimal places.

04:03.350 --> 04:10.160
So if we have "c" again, it is going to add the decimal point and then 6 decimal places. And there is

04:10.160 --> 04:10.640
no data [for the decimal places]

04:10.640 --> 04:12.860
So it is going to add 0's to make it up.

04:13.550 --> 04:19.760
So this is going to padded out to be 299792458.000000

04:19.760 --> 04:20.120
zero.

04:21.920 --> 04:23.000
It is possibly a bit pointless!

04:23.990 --> 04:29.330
And you will also find out the reason why scientific notation was invented: so you can display numbers

04:29.330 --> 04:33.200
which are too big or too small to be stored in the format.

04:34.010 --> 04:40.130
If we have the charge on the electron, which is 10 to the minus 19, so we need to shift the decimal

04:40.130 --> 04:44.480
points 19 places to the left. Which is probably going to go over here somewhere!

04:45.560 --> 04:46.850
So this is a very small number.

04:47.660 --> 04:53.940
And the problem is that a double can only store 15 decimal places. To store

04:53.960 --> 04:56.540
this accurately will need 19 decimal places.

04:57.260 --> 05:02.000
So in fact, all this data will be truncated and we end up with 0.

05:11.410 --> 05:17.860
So fixed is sticky. In fact, all the manipulators in this video are sticky, so they will permanently

05:17.860 --> 05:19.300
change the state of the stream.

05:19.840 --> 05:24.700
If anyone tries to do floating-point output later on in the program, not just in the same function,

05:24.700 --> 05:29.890
but anywhere in the program, after you change the stream, they are going to get whatever you set it

05:29.890 --> 05:32.320
to. And that might not be what they expect.

05:32.950 --> 05:37.930
So we should be considerate to other programmers. Or maybe to ourselves in a few months' time!

05:38.380 --> 05:44.080
So once we have finished doing this output, we use the "defaultfloat" manipulator and that will push everything

05:44.080 --> 05:45.310
back to the defaults.

05:50.830 --> 05:54.850
One final thing you can do is to change the number of digits that are displayed.

05:55.360 --> 05:59.350
The default is 6 for defaultfloat and fixed.

06:00.490 --> 06:05.110
If you want to change that, there is a setprecision manipulator.

06:05.680 --> 06:09.400
And this takes an argument, which is the number of digits you want to display.

06:11.220 --> 06:16.980
So if we set that and then send pi down the stream, it is going to display 3 significant digits,

06:16.980 --> 06:18.480
which is 3.14.

06:22.690 --> 06:28.330
So to test this out, we are going to display pi using 3 significant figures and then using 6,

06:28.330 --> 06:29.560
which is the default.

06:30.850 --> 06:34.780
So we actually set the precision, and then we set it back to the default.

06:35.110 --> 06:38.230
So we are actually being a bit more public-spirited here!

06:42.330 --> 06:48.780
So when we switched the precision to 3, that gives 3 significant figures, 3.14, and then when

06:48.780 --> 06:52.440
we go back to the default 6, we get the default output.

06:54.420 --> 06:57.900
Okay, so that is it for this video. I will see you next time.

06:57.900 --> 07:00.000
But until then, keep coding!
