Skip to content

Commit

Permalink
add fraction + examples + refactor (#21)
Browse files Browse the repository at this point in the history
- add experimental **fraction()**
- add **print_fractions.ino** example
- add **print_fractions_denum.ino** example
- update **print_sci_experimental.ino** example
- added define PRINTHELPERS_LIB_VERSION.
- update readme.md
  • Loading branch information
RobTillaart authored Nov 20, 2024
1 parent cf8af40 commit cf9b306
Show file tree
Hide file tree
Showing 20 changed files with 473 additions and 124 deletions.
13 changes: 10 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.4.5] - 2024-11-20
- add experimental **fraction()**
- add **print_fractions.ino** example
- add **print_fractions_denum.ino** example
- update **print_sci_experimental.ino** example
- added define PRINTHELPERS_LIB_VERSION.
- update readme.md

## [0.4.4] - 2024-01-05
- FIx URL in examples
- fix URL in examples
- minor edits


## [0.4.3] - 2023-11-15
- added **csi()** comma separated integers for readability e.g. 123,458,654
- update readme.md.
Expand Down Expand Up @@ -41,7 +48,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

----

## [0.3.1] - 2023-01-27
## [0.3.1] - 2023-01-27
- fix **scieng()** itoa() => sprintf() + conditional ESP32
- add leading 0 for exponents smaller than 10, to better align columns.
- update GitHub actions
Expand Down
188 changes: 109 additions & 79 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,42 @@ Arduino library to help formatting data for printing.

## Description

The printHelpers library contains a number of functions that help to print
data in a way not possible in the standard print library of the Arduino.

- **print64()** print **uint64_t** and **int64_t**
- **sci()** generates in scientific format - exponent has step 1.
- **eng()** generates in engineering format - exponent has step 3.
- **scieng()** generates in exponential format - exponent has step 1 to 9.
- **toBytes()** generates KB MB GB etc.
- **hex()** generates hexadecimal output with leading zeros up to **uint64_t**.
- **bin()** generates binary output with leading zeros up to **uint64_t**.
- **toRoman()** generates a ROMAN representation of a (positive) number.
- **printInch(float inch, uint16_t step)** experimental.
- **printFeet(float feet)** experimental.
- **csi()** generates a comma separated integer for readability.
The printHelpers library contains a number of functions that help to print
data in a way not supported in the standard print library of the Arduino.

- **char \* print64()** returns a string for **uint64_t** and **int64_t**.
- **char \* sci()** returns a string in scientific format - exponent has step 1.
- **char \* eng()** returns a string in engineering format - exponent has step 3.
- **char \* scieng()** returns a string in exponential format - exponent has step 1 to 9.
- **char \* toBytes()** returns a string in KB MB GB etc.
- **char \* hex()** returns hexadecimal output with **leading zeros** up to **uint64_t**.
- **char \* bin()** returns binary output with **leading zeros** up to **uint64_t**.
- **char \* toRoman()** returns a ROMAN representation of a (positive) number.
- **char \* printInch(float inch, uint16_t step)** returns a string e.g. 5 7/8".
- **char \* printFeet(float feet)** returns a string e.g. 7"4'
- **char \* csi()** returns a comma separated integer for readability e.g. 3,254,152.
- **char \* fraction()** returns a fraction representation of a double/float e.g. 355/113.

For the details, see sections below.

If a (generic) print format is missing, please open an issue.


#### Thread safety
### Thread safety

Note the functions of this library all share an internal buffer, so the library is
definitely **not** thread safe.
Note the functions of this library all share an internal buffer, so the library is
definitely **not** thread safe.
Therefore one should copy / print the data (returned pointer) as fast as possible.

Thread-safe versions of these print functions might be made in the future.


### Related

- https://github.com/RobTillaart/Fraction
- https://github.com/RobTillaart/lineFormatter (for tabular formatting)


## Interface

```cpp
Expand All @@ -54,50 +61,50 @@ Thread-safe versions of these print functions might be made in the future.
The following functions are implemented:


#### print64()
### print64()

- **char \* print64(int64_t value, uint8_t base = 10)** converts a 64 bit integer
number to a char array.
- **char \* print64(int64_t value, uint8_t base = 10)** converts a 64 bit integer
number to a char array.
The plus sign is not printed, neither are leading zero's.
Base 10 (DEC) and 16 (HEX) are supported and other bases up to 36 can be used.
Default base == 10 == decimal.
Note that negative numbers will always get a minus sign for any base.
Cast the number to uint64_t to suppress the sign.

- **char \* print64(uint64_t value, uint8_t base = 10)** converts a unsigned 64 bit
int number to a char array.
No sign is printed, neither are leading zero's.
- **char \* print64(uint64_t value, uint8_t base = 10)** converts a unsigned 64 bit
int number to a char array.
No sign is printed, neither are leading zero's.
Base 10 (DEC) and 16 (HEX) are supported and bases up to 36 can be used.
Default base == 10 == decimal.


#### sci() eng()
### sci() eng()

- **char \* sci(double value, uint8_t decimals)** converts a float or double to a
char array.
- **char \* sci(double value, uint8_t decimals)** converts a float or double to a
char array.
E.g. **print(sci(f, 4))** ==> results in **"6.7407E+21"**.
The existing Arduino print library only supports printing of floats and
doubles up to about 4E9 while the range of floats goes up to ~1E38.
The smallest float values will often be printed as 0.00 while floats
doubles up to about 4E9 while the range of floats goes up to ~1E38.
The smallest float values will often be printed as 0.00 while floats
support down to about 1E-38 (subnormal even to 1E-45).
Existing (AVR) library functions **dtostrf()** has no scientific notation
The existing (AVR) library function **dtostrf()** has no scientific notation
and **dtostre()** is limited to 7 decimals. These latter two are faster.
Values printed with **sci()** do look often pretty in column output.

- **size_t sci(Stream &str, double value, uint8_t decimals)** as above.
Prints directly to a stream, returning bytes printed.

- **char \* eng(double value, uint8_t decimals)** converts a float or double to a
char array.
- **char \* eng(double value, uint8_t decimals)** converts a float or double to a
char array.
E.g. print(eng(f, 4)) ==> results in "6.7407E+21".
Note the exponent created by **eng()** is always a multiple of 3.
Values printed with **eng()** do not always look pretty in column output.
This is due to the exponent power of 3. However its output translates easy to
thousands, millions, billions, and millis, micros, nano etc which are powers of 3.
thousands, millions, billions, and millis, micros, nano etc. which are powers of 3.

- **char \* scieng(double value, uint8_t decimals, uint8_t exponentMultiple)** converts a
float or double to a char array.
**sci()** and **eng()** use the same underlying function called **scieng()**
- **char \* scieng(double value, uint8_t decimals, uint8_t exponentMultiple)** converts a
float or double to a char array.
**sci()** and **eng()** use the same underlying function called **scieng()**
as the initial code for converting was almost identical.
Although not intended to be used directly, feel free to use it.
The last parameter **exponentMultiple** defines where the exponent is a multiple of.
Expand All @@ -108,15 +115,15 @@ Personally I like the multiple of 2 as I get 2 orders of magnitude in the
mantissa. This is e.g. useful for temperature Celsius or percentages.


#### toBytes()
### toBytes()

- **char \* toBytes(double value, uint8_t decimals = 2)** converts a big number
- **char \* toBytes(double value, uint8_t decimals = 2)** converts a big number
representing an amount of bytes to a shorter string usable for displaying.
The string uses official extensions.
The number of decimals is max 3:
Example 3.292.528 ==> "3.140 MB"

Value ranges supported are in steps of powers of 1024.
The number of decimals is max 3, example: 3.292.528 ==> "3.140 MB"

Value ranges supported are in steps of powers of 1024.
These will all be shown in UPPERCASE so KB, MB etc.

| Unit | abbrev. | size | Unit | abbrev. | size |
Expand All @@ -130,28 +137,28 @@ These will all be shown in UPPERCASE so KB, MB etc.
| exabytes | EB | 1024^6 | udabytes | UB | 1024^12 |


Treda Byte is shortened as "TDB" and uses 2 chars to indicate the magnitude.
That would take extra memory or slightly more complex code.
As it is very seldom used, "official" support stops with UDA.
Treda Byte is shortened as "TDB" and uses 2 chars to indicate the magnitude.
That would take extra memory or slightly more complex code.
As it is very seldom used, "official" support stops with UDA.
Should be big enough for some time.

Note: max uint64_t 2^64 is in the order of exa or zetta bytes.
Note: max uint64_t == 2^64 is in the order of exa or zetta bytes.

To have some support for the really big sizes the code uses lowercase for the next 8 levels:
treda sorta rinta quexa pepta ocha nena minga luma (1024\^13 ~~ 1024\^21)
To enable this patch the function in the **printHelpers.cpp** file.


#### hex() bin()
### hex() bin()

The default print() function of Arduino does not have leading zero's
for **HEX** and **BIN**.
This often causes a "broken" layout especially if one wants to print
The default print() function of Arduino does not have leading zero's
for **HEX** and **BIN**.
This often causes a "broken" layout especially if one wants to print
the output in columns or so.

To solve this the following functions are added that will generate a
constant length char array.
To solve this the following functions are added that will generate a
constant length char array.

- **char \* hex(uint64_t value, uint8_t digits = 16)**
- **char \* hex(uint32_t value, uint8_t digits = 8)**
- **char \* hex(uint16_t value, uint8_t digits = 4)**
Expand All @@ -163,19 +170,19 @@ constant length char array.

Note: Data types not supported, must be cast to an supported type.

Note: There is overlap between **hex(value)** and **print64(value, HEX)**.
Note: There is overlap between **hex(value)** and **print64(value, HEX)**.
The latter does not produce the leading zero's or fixed length output.

Note: hex() does not print hex indicator like "0x" or "H" in front.

Note: bin() does not print bin indicator like "b" at the end.


#### toRoman()
### toRoman()

https://en.wikipedia.org/wiki/Roman_numerals

A less used but well known print format are the Roman digits.
A less used but well known print format are the Roman digits.
The library function **toRoman()** will convert any number from 0..100 million into a Roman number.
The numbers 1..5000 ("official" range) are the well known UPPER case characters.

Expand All @@ -202,15 +209,15 @@ Notes:
- values between 5K-10K are extended with extra M chars.
- values 10K-100M are represented with lower case characters.
This is not a standard, but it sort of works well.
- values > 100M return OVF == overflow.
- values > 100M return OVF == overflow.
- There is no special 'subtract code' for 9000 to have a clear distinction between
"official" and extended numbers.
- The number 4 is often written as IIII on clocks with Roman digits,
although IV would be (more?) correct and therefore IV is used.
The reason for IIII is that it is opposite of VIII giving a visual balance.


#### Distance feet inch
### Distance feet inch

Experimental 0.4.1

Expand All @@ -221,12 +228,12 @@ The parameter step must be a power of 2 == 2, 4, 8, 16, 32, 64, 128.
``` a'b"``` e.g. 4.5 feet prints as ```4'6"```


#### Comma Separated Integer
### Comma Separated Integer

Experimental 0.4.3

When you are working with large numbers, more than lets say 6 digits.
With these numbers it is often difficult to see if it is 2 million something or 20 million something.
With these numbers it is often difficult to see if it is 2 million something or 20 million something.
A proven way to solve this is to print those large numbers in groups of 3 digits separated by comma's.
This improves the readability a lot and yes the price is more room needed on a display.
The comma is chosen as it is default thousands separator in Excel.
Expand All @@ -238,32 +245,55 @@ Like all printHelper functions it uses a shared print buffer to keep memory usag
Example 192837465 becomes 192,837,465.

signed
- **char \* csi(int64_t n)**
- **char \* csi(int32_t n)**
- **char \* csi(int16_t n)**
- **char \* csi(int64_t n)**
- **char \* csi(int32_t n)**
- **char \* csi(int16_t n)**
- **char \* csi(int8_t n)**

unsigned
- **char \* csi(uint64_t n)**
- **char \* csi(uint32_t n)**
- **char \* csi(uint16_t n)**
- **char \* csi(uint64_t n)**
- **char \* csi(uint32_t n)**
- **char \* csi(uint16_t n)**
- **char \* csi(uint8_t n)**


### Fraction

Experimental 0.4.5, based upon Fraction class.

The precision is hard set to absolute 1e-6.
The fraction will have a numerator and denumerator in the range 1..99999.
Note that as floats only have 7 significant digits the precision varies
especially for numbers above 1 (as decimal part eats up significant digits).

The algorithm is primary meant for values between 0 and 1 however any float
will be processed. The algorithm does not always come up with the best
fraction

Time is not constant, e.g. **fraction(PI)** takes about 620 us on an Arduino UNO 16 MHz.

- **char \* fraction(double value)** approach the value with a fraction like n / d.
- **char \* fraction(double value, uint16_t denom)** choose the denominator.
Note it will be reduced if possible e.g. 6/8 => 3/4

If you have a faster or more accurate algorithm or both please let me know
and open an issue.


## Shared print buffer

The implementation of the function all use a shared buffer to hold the
generated string.
This is done to reduce the memory overhead of embedding static buffers.
The implementation of the function all use a shared buffer to hold the
generated string.
This is done to reduce the memory overhead of embedding static buffers.
**Note this is not thread safe!**
In a coming release the functions will be able to pass a buffer to them
In a coming release the functions will be able to pass a buffer to them
to become more thread safe.

The size of this shared buffer is default 66 to be able to print a 64 bit
integer in base 2.
To save memory one can change this buffer size in the code or compile time
The size of this shared buffer is default 66 to be able to print a 64 bit
integer in base 2.
To save memory one can change this buffer size in the code or compile time
by changing **PRINTBUFFERSIZE** in printHelpers.h.
Be aware that **sci()** and **eng()** use the same buffer.
Be aware that **sci()** and **eng()** use the same buffer.
These functions need about 10 bytes plus one bytes for every decimal used.
So for floats one need 15-20 bytes max, for doubles one need up to 30 bytes max.
In practice a size of 22 will work for most applications.
Expand Down Expand Up @@ -293,21 +323,21 @@ When functions are added, the recommended minimum size might increase.

#### Could

- investigate **bin(float)** to dump floats?
- "sign, mantissa, exponent bits"
- like this "s0 m0111010 e100010" (right length)
- investigate separators in **hex()**
- space per 8, 4 or 2
- investigate thread safe version
- pass char buffer as parameter (breaking)
- could be the log10 pow version?
- optimize **char \* hex(uint8_t / uint16_t ...)**
- make this library a .h file only?
- PRINTHELPERS_LIB_VERSION
- is there need for Scientific or Engineering integers
- 23.457e3 only positive powers
- could be without float math
- 4 versions (un)signed 32/64 ?
- negative ROMAN numbers (add a - sign)


#### Wont

- is there need for Scientific or Engineering integers (this just works)
- add **oct()** along BIN, HEX
- add **float()** as Arduino limits floats to "MAXLONG" by code.
- use dtostrf() - is that portable?
Expand All @@ -317,7 +347,7 @@ When functions are added, the recommended minimum size might increase.
- investigate separators in **bin()**
- point or space, per 8 or 4 or 2
- ==> printBuffer too small for bin(64) ==> need 75-100 bytes.
- Investigate performance and accuracy
- Investigate performance and accuracy
- **sci()** and **eng()**.
- investigate sci() version based upon use of log()
- done => see examples.
Expand Down
4 changes: 2 additions & 2 deletions examples/print64/print64.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("PRINTHELPERS_VERSION: ");
Serial.println(PRINTHELPERS_VERSION);
Serial.print("PRINTHELPERS_LIB_VERSION: ");
Serial.println(PRINTHELPERS_LIB_VERSION);
Serial.println();

delay(100);
Expand Down
Loading

0 comments on commit cf9b306

Please sign in to comment.