Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bc7f9af

Browse filesBrowse files
committed
Add print(float) to save space (VS double).
1 parent 9fb5081 commit bc7f9af
Copy full SHA for bc7f9af

File tree

Expand file treeCollapse file tree

2 files changed

+67
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+67
-0
lines changed

‎cores/arduino/Print.cpp

Copy file name to clipboardExpand all lines: cores/arduino/Print.cpp
+64Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ size_t Print::print(unsigned long long n, int base)
129129
}
130130
}
131131

132+
size_t Print::print(float n, int digits)
133+
{
134+
return printFloat(n, digits);
135+
}
136+
132137
size_t Print::print(double n, int digits)
133138
{
134139
return printFloat(n, digits);
@@ -221,6 +226,13 @@ size_t Print::println(unsigned long long num, int base)
221226
return n;
222227
}
223228

229+
size_t Print::println(float num, int digits)
230+
{
231+
size_t n = print(num, digits);
232+
n += println();
233+
return n;
234+
}
235+
224236
size_t Print::println(double num, int digits)
225237
{
226238
size_t n = print(num, digits);
@@ -406,6 +418,58 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
406418
return bytes;
407419
}
408420

421+
size_t Print::printFloat(float number, uint8_t digits)
422+
{
423+
size_t n = 0;
424+
425+
if (isnan(number)) {
426+
return print("nan");
427+
}
428+
if (isinf(number)) {
429+
return print("inf");
430+
}
431+
if (number > 4294967040.0) {
432+
return print("ovf"); // constant determined empirically
433+
}
434+
if (number < -4294967040.0) {
435+
return print("ovf"); // constant determined empirically
436+
}
437+
438+
// Handle negative numbers
439+
if (number < 0.0) {
440+
n += print('-');
441+
number = -number;
442+
}
443+
444+
// Round correctly so that print(1.999, 2) prints as "2.00"
445+
float rounding = 0.5;
446+
for (uint8_t i = 0; i < digits; ++i) {
447+
rounding /= 10.0;
448+
}
449+
450+
number += rounding;
451+
452+
// Extract the integer part of the number and print it
453+
unsigned long int_part = (unsigned long)number;
454+
float remainder = number - (float)int_part;
455+
n += print(int_part);
456+
457+
// Print the decimal point, but only if there are digits beyond
458+
if (digits > 0) {
459+
n += print('.');
460+
}
461+
462+
// Extract digits from the remainder one at a time
463+
while (digits-- > 0) {
464+
remainder *= 10.0;
465+
unsigned int toPrint = (unsigned int)remainder;
466+
n += print(toPrint);
467+
remainder -= toPrint;
468+
}
469+
470+
return n;
471+
}
472+
409473
size_t Print::printFloat(double number, uint8_t digits)
410474
{
411475
size_t n = 0;

‎cores/arduino/Print.h

Copy file name to clipboardExpand all lines: cores/arduino/Print.h
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Print {
3737
size_t printNumber(unsigned long, uint8_t);
3838
size_t printULLNumber(unsigned long long, uint8_t);
3939
size_t printFloat(double, uint8_t);
40+
size_t printFloat(float, uint8_t);
4041
protected:
4142
void setWriteError(int err = 1)
4243
{
@@ -86,6 +87,7 @@ class Print {
8687
size_t print(unsigned long, int = DEC);
8788
size_t print(long long, int = DEC);
8889
size_t print(unsigned long long, int = DEC);
90+
size_t print(float, int = 2);
8991
size_t print(double, int = 2);
9092
size_t print(const Printable &);
9193

@@ -100,6 +102,7 @@ class Print {
100102
size_t println(unsigned long, int = DEC);
101103
size_t println(long long, int = DEC);
102104
size_t println(unsigned long long, int = DEC);
105+
size_t println(float, int = 2);
103106
size_t println(double, int = 2);
104107
size_t println(const Printable &);
105108
size_t println(void);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.