GY-63_MS5611/libraries/Fraction/readme.md

134 lines
3.8 KiB
Markdown
Raw Normal View History

2021-01-29 06:31:58 -05:00
[![Arduino CI](https://github.com/RobTillaart/Fraction/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
2021-11-01 10:56:22 -04:00
[![Arduino-lint](https://github.com/RobTillaart/Fraction/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/Fraction/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/Fraction/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/Fraction/actions/workflows/jsoncheck.yml)
2023-11-02 10:10:48 -04:00
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/Fraction.svg)](https://github.com/RobTillaart/Fraction/issues)
2021-01-29 06:31:58 -05:00
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/Fraction/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/Fraction.svg?maxAge=3600)](https://github.com/RobTillaart/Fraction/releases)
2023-11-02 10:10:48 -04:00
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/Fraction.svg)](https://registry.platformio.org/libraries/robtillaart/Fraction)
2021-01-29 06:31:58 -05:00
2021-11-01 10:56:22 -04:00
2020-11-27 05:16:22 -05:00
# Fraction
2021-12-18 08:10:11 -05:00
Arduino library to implement a Fraction data type (experimental).
2021-11-01 10:56:22 -04:00
2020-11-27 05:16:22 -05:00
## Description
2023-02-03 07:14:59 -05:00
The fraction library implements fractional numbers a.k.a. Q,
(integers are Z and floats/doubles are R), and the conversion to floats.
2015-03-01 08:19:14 -05:00
2023-02-03 07:14:59 -05:00
The code is working with a number of limitations a.o.:
2015-03-01 08:19:14 -05:00
- denominator is max 4 digits to keep code for multiply and divide simple
2023-02-03 07:14:59 -05:00
- Fractions are not exact, even floats are not exact.
2015-03-01 08:19:14 -05:00
- the range of numbers supported is limited.
2020-11-27 05:16:22 -05:00
- code is experimental still.
2015-03-01 08:19:14 -05:00
That said, the library is useful e.g. to display float numbers as a fraction.
2023-02-03 07:14:59 -05:00
From programming point of view the **fractionize(float)** function, converting a double
2015-03-01 08:19:14 -05:00
into a fraction is a nice programming problem, fast with a minimal error.
2020-11-27 05:16:22 -05:00
In short, use fractions with care otherwise your sketch might get broken ;)
2021-11-01 10:56:22 -04:00
2023-02-03 07:14:59 -05:00
## Interface
2020-11-27 05:16:22 -05:00
2023-02-03 07:14:59 -05:00
```cpp
#include "fraction.h"
```
#### Constructors
- **explicit Fraction(double)**
- **explicit Fraction(float)**
- **Fraction(int32_t nominator, int32_t denominator)**
- **explicit Fraction(int32_t p)**
- **explicit Fraction(int16_t p)**
- **explicit Fraction(int8_t p)**
- **explicit Fraction(uint32_t p)**
- **explicit Fraction(uint16_t p)**
- **explicit Fraction(uint8_t p)**
- **Fraction(const Fraction &f)**
#### Printable
2023-11-02 10:10:48 -04:00
The Fraction library implements the Printable interface, so one can do.
2023-02-03 07:14:59 -05:00
```cpp
Fraction fr(PI);
Serial.print(fr); // print 355/113
```
#### Equalities
The Fraction library implements ==, !=, >=, >, <, <=
#### Basic Math
The Fraction library implements, + - * / += -= *= /= and - (negation)
#### Conversion
- **double toDouble()** idem.
- **float toFloat()** idem.
- **bool isProper()** absolute value < 1.
- **float toAngle()** returns 0..360 degrees.
- **int32_t nominator()** idem.
- **int32_t denominator()** idem.
#### Miscellaneous (static)
- **Fraction mediant(const Fraction&, const Fraction&)**
- **Fraction middle(const Fraction&, const Fraction&)**
- **Fraction setDenominator(const Fraction&, uint16_t)**
2020-11-27 05:16:22 -05:00
2021-11-01 10:56:22 -04:00
2020-11-27 05:16:22 -05:00
## Use with care
2023-02-03 07:14:59 -05:00
The library is reasonably tested. If problems arise please open an issue.
2020-11-27 05:16:22 -05:00
2021-11-01 10:56:22 -04:00
## Future
2023-02-03 07:14:59 -05:00
#### Must
2021-12-18 08:10:11 -05:00
- improve documentation
2022-11-07 09:02:34 -05:00
- test test test ...
2023-02-03 07:14:59 -05:00
#### Should
- performance testing
2021-11-01 10:56:22 -04:00
- investigate divide by zero errors
2023-02-03 07:14:59 -05:00
- NAN in fraction? => 0/0 ?
- INF in fraction? => 1/0 and -1/0?
- investigate better **fractionize()**
- depends on nom/denom size
- returns the error..
#### Could
2022-11-07 09:02:34 -05:00
- extend unit tests
- experiment with bigger nominator/denominator using all of 32767 possibilities ?
2023-02-03 07:14:59 -05:00
- add famous constants as Fraction e.g
- FRAC_PI = 355/113
- FRAC_E = 3985/1466
#### Wont
2021-11-01 10:56:22 -04:00
2023-11-02 10:10:48 -04:00
## Support
If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.
Thank you,