Skip to content

Commit 25bd5df

Browse files
committed
Fixes #84: Choice of float/double
* Can now choose between using `double` and `float` internally, for floating-point work, using the `PRINTF_USE_DOUBLE_INTERNALLY` definition and corresponding CMake option. * Adjusted test suite to support the different choice of floating-point type: * Tests relevant both to `float` and `double`, but with different precisions, are adjusted with an `#if #else #endif` * Tests and test cases relevant only for `double` precision are not compiled at all when `float` is used. * Lots of new explicit conversions :-(
1 parent 53d83f4 commit 25bd5df

File tree

5 files changed

+148
-101
lines changed

5 files changed

+148
-101
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ option(SUPPORT_EXPONENTIAL_SPECIFIERS "Support exponential floating poin
2020
option(SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS "Support the I + bit size integer specifiers (%I8, %I16, %I32, %I64) as in Microsoft Visual C++" ON)
2121
option(SUPPORT_WRITEBACK_SPECIFIER "Support the length write-back specifier (%n)" ON)
2222
option(SUPPORT_LONG_LONG "Support long long integral types (allows for the ll length modifier and affects %p)" ON)
23+
option(USE_DOUBLE_INTERNALLY "Use the C `double` type - typically 64-bit in size - for internal floating-point arithmetic " ON)
2324
option(CHECK_FOR_NUL_IN_FORMAT_SPECIFIER "Be defensive in the undefined-behavior case of a format specifier not ending before the string ends" ON)
2425

2526
set(ALIASING_MODES NONE HARD SOFT)
@@ -38,6 +39,7 @@ foreach(opt
3839
SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS
3940
SUPPORT_WRITEBACK_SPECIFIER
4041
SUPPORT_LONG_LONG
42+
USE_DOUBLE_INTERNALLY
4143
ALIAS_STANDARD_FUNCTION_NAMES_SOFT
4244
ALIAS_STANDARD_FUNCTION_NAMES_HARD
4345
CHECK_FOR_NUL_IN_FORMAT_SPECIFIER

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ Options used both in CMake and in the library source code via a preprocessor def
8282
| PRINTF_ALIAS_STANDARD_FUNCTION_NAMES | NONE | Alias the standard library function names (`printf()`, `sprintf()` etc.) to the library's functions.<br>The possible values are `NONE`, `SOFT` and `HARD`. With Soft aliasing, the library's object files contain symbols which do not clash with the standard library's: `printf_`, `sprintd_` etc; and a macro in `printf.h` replaces usages of `printf()`, `sprintf()` etc. with the underscored versions. With Hard aliasing, no such macro is used, and the library's object files contain `printf`, `sprintf` etc. - and thus cannot be linked together with a full-fledged standard library. **Note:** The preprocessort definitions `#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT` and `#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD` should be defined to have the same values when using the library as when having compiled the list. |
8383
| PRINTF_INTEGER_BUFFER_SIZE | 32 | ntoa (integer) conversion buffer size. This must be big enough to hold one converted numeric number _including_ leading zeros, normally 32 is a sufficient value. Created on the stack. |
8484
| PRINTF_DECIMAL_BUFFER_SIZE | 32 | ftoa (float) conversion buffer size. This must be big enough to hold one converted float number _including_ leading zeros, normally 32 is a sufficient value. Created on the stack. |
85-
| PRINTF_DEFAULT_FLOAT_PRECISION | 6 | Define the default floating point precision|
85+
| PRINTF_DEFAULT_FLOAT_PRECISION | 6 | Define the default floating point precision digits |
8686
| PRINTF_MAX_INTEGRAL_DIGITS_FOR_DECIMAL | 9 | Maximum number of integral-part digits of a floating-point value for which printing with %f uses decimal (non-exponential) notation |
8787
| PRINTF_SUPPORT_DECIMAL_SPECIFIERS | YES | Support decimal notation floating-point conversion specifiers (%f, %F) |
8888
| PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS | YES | Support exponential floating point format conversion specifiers (%e, %E, %g, %G) |
8989
| SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS | YES | Support the 'I' + bit size integer specifiers (%I8, %I16, %I32, %I64) as in Microsoft Visual C++ |
9090
| PRINTF_SUPPORT_WRITEBACK_SPECIFIER | YES | Support the length write-back specifier (%n) |
9191
| PRINTF_SUPPORT_LONG_LONG | YES | Support long long integral types (allows for the ll length modifier and affects %p) |
92+
| PRINTF_USE_DOUBLE_INTERNALLY | YES | Use the `double` for internal floating-point calculations (rather than using the single-precision `float` type |
9293

9394
Within CMake, these options lack the `PRINTF_` prefix.
9495

printf_config.h.in

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define PRINTF_SUPPORT_WRITEBACK_SPECIFIER @PRINTF_SUPPORT_WRITEBACK_SPECIFIER@
88
#define PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS @PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS@
99
#define PRINTF_SUPPORT_LONG_LONG @PRINTF_SUPPORT_LONG_LONG@
10+
#define PRINTF_USE_DOUBLE_INTERNALLY @PRINTF_USE_DOUBLE_INTERNALLY@
1011
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT @PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT@
1112
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD @PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD@
1213

0 commit comments

Comments
 (0)