Skip to content

Commit 90ca8c0

Browse files
Truncation and Round Off Addition
Addressing the Issue arduino#579. Information related to Truncation and Round Off Addition is added.
1 parent 8370cea commit 90ca8c0

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

Language/Variables/Data Types/float.adoc

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,39 @@ Datatype for floating-point numbers, a number that has a decimal point. Floating
2121

2222
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.
2323

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+
2556

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.
2757

2858
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.
2959
[%hardbreaks]

0 commit comments

Comments
 (0)