Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Programming-Arduino (1) - Pages-78

Uploaded by

axl1994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Programming-Arduino (1) - Pages-78

Uploaded by

axl1994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

should be enclosed in parentheses as in the preceding example.

Other Variable Types


All our examples of variables so far have been int variables. This is by far the
most commonly used variable type, but there are some others that you should be
aware of.

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:

1. 17 is divided by 5, which gives 3.4, which is then truncated to 3.


2. 3 is then multiplied by 9 and 32 is added to give a result of 59—which is
quite a long way from 62.6.
For circumstances like this, we can use float s. In the following example, our
temperature conversion function is rewritten to use float s:
float centToFaren(float c)

You might also like