Programming-Arduino (1) - Pages-78
Programming-Arduino (1) - Pages-78
floats
One such type, which is relevant to the previous temperature conversion
example, is float . This variable type represents floating point numbers—that is,
numbers that may have a decimal point in them, such as 1.23. You need this
variable type when whole numbers are just not precise enough.
Note the following formula:
f = c * 9 / 5 + 32
If you give c the value 17, then f will be 17 * 9 / 5 + 32 or 62.6. But if f is an int
, then the value will be truncated to 62.
The problem becomes even worse if we are not careful of the order in which
we evaluate things. For instance, suppose that we did the division first, as
follows:
f = (c / 5) * 9 + 32
Then in normal math terms, the result would still be 62.6, but if all the numbers
are int s, then the calculation would proceed as follows: