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
Copy file name to clipboardExpand all lines: Language/Variables/Data Types/float.adoc
+32-2Lines changed: 32 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -21,9 +21,39 @@ Datatype for floating-point numbers, a number that has a decimal point. Floating
21
21
22
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
23
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. You should instead check that the absolute value of the difference between the numbers is less than some small number.
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
+
25
56
26
-
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.
27
57
28
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.
0 commit comments