Skip to content

Commit

Permalink
add fraction + examples + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Nov 20, 2024
1 parent e3fd211 commit b19a027
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ 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 for future.
- added define PRINTHELPERS_LIB_VERSION.
- update readme.md

## [0.4.4] - 2024-01-05
Expand Down
42 changes: 22 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ 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()** generates string for **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.
- **fraction()** generates a fraction representation of a double/float.
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.

Expand Down Expand Up @@ -63,7 +63,7 @@ The following functions are implemented:

### print64()

- **char \* print64(int64_t value, uint8_t base = 10)** converts a 64 bit integer
- **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.
Expand Down Expand Up @@ -120,8 +120,8 @@ mantissa. This is e.g. useful for temperature Celsius or percentages.
- **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"

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.
Expand All @@ -142,7 +142,7 @@ 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)
Expand Down Expand Up @@ -273,7 +273,7 @@ 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.
- **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
Expand All @@ -293,7 +293,7 @@ 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 @@ -332,6 +332,8 @@ When functions are added, the recommended minimum size might increase.
- pass char buffer as parameter (breaking)
- could be the log10 pow version?
- optimize **char \* hex(uint8_t / uint16_t ...)**
- negative ROMAN numbers (add a - sign)


#### Wont

Expand Down
1 change: 0 additions & 1 deletion printHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,6 @@ char * csi(uint16_t value)
return csi((uint32_t)value);
}


char * csi(uint8_t value)
{
return csi((uint32_t)value);
Expand Down

0 comments on commit b19a027

Please sign in to comment.