Chapter 6 Floats and Converting
Previously, we learned how integers are stored in the computer’s memory, but what happens if we want to store numbers with digits after the decimal point, such as floats? In this chapter, we will take a look at how these floating point numbers are stored in memory.
Next, we will investigate how to switch a number of one datatype to that of another. After all, the integer 5 for example could also be written as the float 5.0. In the computer’s memory, the 5 would be stored in binary 2’s complement whereas the 5.0 would be stored in a totally different fashion (which we’ll learn about in this lesson). We have seen that a number’s datatype affects how arithmetic is performed (recall the strange division behavior in C? 5/2 = 2 but 5.0/2.0 = 1.5?) That’s why it’s necessary to know how to convert the datatype for example from an
int
to a float
in C. This process is called casting.Using floats, we can take on some simple mathematical calculations such as averages, without fear of running into the strange division behavior, such as averages.