Skip to content

Commit 060bcc5

Browse files
committed
Added optional support for printing floating point numbers using the Print class with values greater/less than +/-4,294,967,295. It now supports +/-18,446,744,073,709,551,615.
1 parent cab1e2e commit 060bcc5

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

config.h

+7
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@
8181
* milliseconds). This saves about 64 bytes.
8282
*/
8383
#define NO_DELAY_HIGH_WORD
84+
85+
/* Define LONG_LONG_PRINT_FLOAT to enable the use of Unsigned Long Long variables for printing
86+
* the integer part of single or double floating point numbers using the Print class. This allows
87+
* printing integers up to +/-18,446,744,073,709,551,615, rather than the default +/-4,294,967,295.
88+
* This cost a little code space.
89+
*/
90+
//#define LONG_LONG_PRINT_FLOAT
8491
#endif
8592

8693
#endif // _CONFIG_H_

cores/arduino/Print.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
#include <stdio.h>
2121
#include <string.h>
2222
#include <math.h>
23+
#include <limits.h>
2324
#include "Arduino.h"
2425

2526
#include "Print.h"
27+
#include "../../config.h"
2628

2729
// Public Methods //////////////////////////////////////////////////////////////
2830

@@ -295,8 +297,13 @@ size_t Print::printFloat(double number, uint8_t digits)
295297
if (isnan(number)) return print("nan");
296298
if (isinf(number)) return print("inf");
297299

300+
#if defined(LONG_LONG_PRINT_FLOAT)
301+
if (number > (double)ULONG_LONG_MAX) return print ("ovf");
302+
if (number < -(double)ULONG_LONG_MAX) return print ("ovf");
303+
#else
298304
if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
299305
if (number <-4294967040.0) return print ("ovf"); // constant determined empirically
306+
#endif
300307

301308
// Handle negative numbers
302309
if (number < 0.0)
@@ -313,7 +320,11 @@ size_t Print::printFloat(double number, uint8_t digits)
313320
number += rounding;
314321

315322
// Extract the integer part of the number and print it
323+
#if defined(LONG_LONG_PRINT_FLOAT)
324+
unsigned long long int_part = (unsigned long long)number;
325+
#else
316326
unsigned long int_part = (unsigned long)number;
327+
#endif
317328
double remainder = number - (double)int_part;
318329
n += print(int_part);
319330

@@ -342,8 +353,13 @@ size_t Print::printFloat(float number, uint8_t digits)
342353
if (isnan(number)) return print("nan");
343354
if (isinf(number)) return print("inf");
344355

356+
#if defined(LONG_LONG_PRINT_FLOAT)
357+
if (number > (float)ULONG_LONG_MAX) return print ("ovf");
358+
if (number < -(float)ULONG_LONG_MAX) return print ("ovf");
359+
#else
345360
if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
346361
if (number <-4294967040.0) return print ("ovf"); // constant determined empirically
362+
#endif
347363

348364
// Handle negative numbers
349365
if (number < 0.0)
@@ -360,7 +376,11 @@ size_t Print::printFloat(float number, uint8_t digits)
360376
number += rounding;
361377

362378
// Extract the integer part of the number and print it
379+
#if defined(LONG_LONG_PRINT_FLOAT)
380+
unsigned long long int_part = (unsigned long long)number;
381+
#else
363382
unsigned long int_part = (unsigned long)number;
383+
#endif
364384
float remainder = number - (float)int_part;
365385
n += print(int_part);
366386

0 commit comments

Comments
 (0)