Skip to content

Commit 3f61b65

Browse files
committed
stdlib_noniso.cpp: fix another rounding error in dtostrf
Rounding up the passed float/double to the specified presicion must be the *very first* thing we do, before doing anything else with that value (In this case, the issue was cause by digitsBe4Decimal() returning an incorrect value, because it was being called before passed value was rounded up)
1 parent 8aabac9 commit 3f61b65

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Diff for: cores/arduino/stdlib_noniso.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ char *dtostrf(double number, signed char width, unsigned char prec, char *s)
199199
return s;
200200
}
201201

202+
// rounding up to the precision
203+
rounding = 0.5;
204+
for (i = 0; i < prec; ++i)
205+
rounding /= 10.0;
206+
number += rounding;
207+
202208
out = s;
203209
before = digitsBe4Decimal(number);
204210

@@ -215,12 +221,6 @@ char *dtostrf(double number, signed char width, unsigned char prec, char *s)
215221
number = -number;
216222
}
217223

218-
// rounding up to the precision
219-
rounding = 0.5;
220-
for (i = 0; i < prec; ++i)
221-
rounding /= 10.0;
222-
number += rounding;
223-
224224
// seperate integral and fractional parts
225225
integer = (unsigned long long) number;
226226
fraction = (double) (number - integer);

0 commit comments

Comments
 (0)