You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Datatype for floating-point numbers, a number that has a decimal point. Floating-point numbers are often used to approximate analog and continuous values because they have greater resolution than integers. Floating-point numbers can be as large as 3.4028235E+38 and as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information.
21
21
22
-
Floats have only 6-7 decimal digits of precision. That means the total number of digits, not the number to the right of the decimal point. Unlike other platforms, where you can get more precision by using a double (e.g. up to 15 digits), on the Arduino, double is the same size as float.
23
22
24
-
Floating point numbers are not exact, and may yield strange results when compared. For example 6.0 / 3.0 may not equal 2.0. Also, a general problem of truncation arises during conversion from floating point to integer math.
25
-
[float]
26
-
=== Example Code
27
-
// The example code tells the details of problem of truncation ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
28
-
[source,arduino]
29
-
----
30
-
float x = 2.9; // A float type variable
31
-
int y = x; // 2
32
-
----
33
-
You should instead check that the absolute value of the difference between the numbers is less than some small number.
34
-
35
-
Floating point math is also much slower than integer math in performing calculations, so should be avoided if, for example, a loop has to run at top speed for a critical timing function. Programmers often go to some lengths to convert floating point calculations to integer math to increase speed.
36
-
If performing the process of rounding off during the conversion process, you need to add `0.5` during the conversion process.
37
-
[float]
38
-
=== Example Code
39
-
// The example code tells the details of problem of truncation ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
40
-
[source,arduino]
41
-
----
42
-
float x = 2.9;
43
-
int y = x + 0.5; // 3
44
-
----
45
-
or use the `round()` function respectively.
46
-
47
-
[float]
48
-
=== Example Code
49
-
// The example code tells the details of problem of truncation ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
50
-
[source,arduino]
51
-
----
52
-
float x = 2.9;
53
-
int y = round(x); // 3
54
-
----
55
-
56
-
57
-
58
-
If doing math with floats, you need to add a decimal point, otherwise it will be treated as an int. See the link:../../constants/floatingpointconstants[Floating point] constants page for details.
59
23
[%hardbreaks]
60
24
61
25
[float]
@@ -105,6 +69,41 @@ z = (float)x / 2.0; // z now contains .5 (you have to use 2.0, not 2)
105
69
[#see_also]
106
70
--
107
71
72
+
[float]
73
+
=== Notes and Warnings
74
+
75
+
Floats datatype have only 6-7 decimal digits of precision. That means the total number of digits, not the number to the right of the decimal point. Unlike other platforms, where you can get more precision by using a double (e.g. up to 15 digits), on the Arduino, double is the same size as float.
76
+
77
+
Floating point numbers are not exact, and may yield strange results when compared. For example 6.0 / 3.0 may not equal 2.0.
78
+
Also, a general problem of truncation arises during conversion from floating point to integer math.
79
+
// The example code tells the details of problem of truncation ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
80
+
[source,arduino]
81
+
----
82
+
float x = 2.9; // A float type variable
83
+
int y = x; // 2
84
+
----
85
+
You should instead check that the absolute value of the difference between the numbers is less than some small number.
86
+
87
+
Floating point math is also much slower than integer math in performing calculations, so should be avoided if, for example, a loop has to run at top speed for a critical timing function. Programmers often go to some lengths to convert floating point calculations to integer math to increase speed.
88
+
If performing the process of rounding off during the conversion process, you need to add `0.5` during the conversion process.
89
+
// The example code tells the details of rounding off ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
90
+
[source,arduino]
91
+
----
92
+
float x = 2.9;
93
+
int y = x + 0.5; // 3
94
+
----
95
+
or use the `round()` function respectively.
96
+
97
+
// The example code tells the details of rounding off ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
98
+
[source,arduino]
99
+
----
100
+
float x = 2.9;
101
+
int y = round(x); // 3
102
+
----
103
+
104
+
If doing math with floats, you need to add a decimal point, otherwise it will be treated as an int. See the link:../../constants/floatingpointconstants[Floating point] constants page for details.
0 commit comments