Working with float and double variables
In this article, let’s experiment with float and double data types. Before that, let me explain the range of these data types.
Float storage size is 4 bytes, and its precision is up to 6 decimal places after the decimal point, and the value range is 1.2×10 to the power -38 to 3.4×10 to the power +38.
The range of double is 2.3×10 to the power -308 to 1.7×10 to the power 308, and the precision is up to 15 decimal places, and since it is double-precision storage, it consumes 8 bytes in the memory.
In the program, I have a variable called number, which is initialized to the number 45.78976834578. This is a real number, which has an integer part, a decimal point, and a fractional part. And I have used the data type float here. If I want to print this, I would use %f and let me print that number. The code is shown in Figure 3.
In the program’s output, you can see that the %f prints 6 decimal places of the number after the decimal point. Now the result is correct with respect to 6 decimal places.
And if you want to know what is the status of the remaining decimal spaces, you can increase the width of the output by mentioning the width specifier here. So, I can write 0.9. This is like telling print up to 9 decimal places after the decimal point.
In the output, you can see that the result is now with respect to 9 decimal places, but up to 6 is correct, and the remaining three are wrong. So, there is a precision loss.
Now, change the data type to double; you will get better precision. You can print up to 15. Let’s print up to 14 decimal places, and give lf here. Let’s use lf for the double. The code is shown in Figure 5.
In the output, you can see that you got the better precision. The result is correct. So, the result is identical with this fractional part.
Now, we can also print this output in scientific notation. For that, you have to use %e here instead of %f. So, %e will print this result in scientific notation.
Here we can see that you got the result in scientific notation. The meaning of e+001 is 10 to the power 1.
Now you can also ask the printf to print with respect to 2 decimal places. For that, I would use 0.2 here.
You can see that the output is printed up to 2 decimal places after the decimal point. Here the 8 rounds of it’s the nearest number. That’s why it printed 45.79. And look at the scientific notation; it is 4.58 into 10 to the power 1. So, that would give you 45.8.
Now let’s modify this program to store a charge of an electron. Let’s create a variable chargeE, and the charge of an electron is this number -1.60217662 x 10-19; you can also use the minus sign here. You can also store the number in scientific notation into the float or double data type variables. But you cannot write like this 10-19. What you have to do here is remove this and write e-19. This is how you store the number with scientific notation. This is like 10 to the power -19.
Let’s print that number. Let me print using %f and %e, as shown in Figure 8.
First of all, in the first output, it printed 0. That makes sense because it’s a very small number. And look at the scientific notation, it printed almost correct, and for the fractional part, it printed with respect to 6 decimal places. That’s why this 6 became 7 here. That means you cannot use a float variable here to store the exact value of the charge of an electron because there is a precision loss here.
Here, your requirement is to show the data with respect to 8 decimal places after the decimal point. For that, you can mention 0.8(Figure 9).
You can see that the output result won’t improve. Here 602176, then it is showing 60. So, the result will not improve.
That’s why it makes sense to use a double here because a double gives you precision up to 15 decimal places. Let’s use double here and the lf and le format specifier, and we can now see better precision for this program.
In the Output, you can see that the fractional part is exactly like the input. The reason for that is in double, you can obtain the precision up to 15 decimal places after the decimal point.
When you try to print in a %lf format, you can see that it is printing all zeros because it’s a very small number.
So, if you want to get output, let’s introduce more decimal places; let me give %0.28lf.
Now in the output, it is printing the fractional part with respect to a 28 decimal place after the decimal point.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1