Skip to content

Commit

Permalink
feat: add print(float) to save space (vs double)
Browse files Browse the repository at this point in the history
Signed-off-by: Cedric Honnet <honnet@telecom-paristech.org>
Co-Authored-By: Frederic Pillon <frederic.pillon@st.com>
  • Loading branch information
honnet and fpistm committed Jun 26, 2023
1 parent 9fb5081 commit 840725a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
19 changes: 16 additions & 3 deletions cores/arduino/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ size_t Print::print(unsigned long long n, int base)
}
}

size_t Print::print(float n, int digits)
{
return printFloat(n, digits);
}

size_t Print::print(double n, int digits)
{
return printFloat(n, digits);
Expand Down Expand Up @@ -221,6 +226,13 @@ size_t Print::println(unsigned long long num, int base)
return n;
}

size_t Print::println(float num, int digits)
{
size_t n = print(num, digits);
n += println();
return n;
}

size_t Print::println(double num, int digits)
{
size_t n = print(num, digits);
Expand Down Expand Up @@ -406,7 +418,8 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
return bytes;
}

size_t Print::printFloat(double number, uint8_t digits)
template <class T>
size_t Print::printFloat(T number, uint8_t digits)
{
size_t n = 0;

Expand All @@ -430,7 +443,7 @@ size_t Print::printFloat(double number, uint8_t digits)
}

// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
T rounding = 0.5;
for (uint8_t i = 0; i < digits; ++i) {
rounding /= 10.0;
}
Expand All @@ -439,7 +452,7 @@ size_t Print::printFloat(double number, uint8_t digits)

// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
T remainder = number - (T)int_part;
n += print(int_part);

// Print the decimal point, but only if there are digits beyond
Expand Down
5 changes: 4 additions & 1 deletion cores/arduino/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class Print {
int write_error;
size_t printNumber(unsigned long, uint8_t);
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, uint8_t);
template <class T>
size_t printFloat(T, uint8_t);
protected:
void setWriteError(int err = 1)
{
Expand Down Expand Up @@ -86,6 +87,7 @@ class Print {
size_t print(unsigned long, int = DEC);
size_t print(long long, int = DEC);
size_t print(unsigned long long, int = DEC);
size_t print(float, int = 2);
size_t print(double, int = 2);
size_t print(const Printable &);

Expand All @@ -100,6 +102,7 @@ class Print {
size_t println(unsigned long, int = DEC);
size_t println(long long, int = DEC);
size_t println(unsigned long long, int = DEC);
size_t println(float, int = 2);
size_t println(double, int = 2);
size_t println(const Printable &);
size_t println(void);
Expand Down

0 comments on commit 840725a

Please sign in to comment.