Go Down

Topic: Fraction library for Arduino (Read 112 times) previous topic - next topic

robtillaart


Some time ago I needed a number as a fraction 0.42 = 21/50 so I wrote a small sketch to represent a float as a fraction. Then the thought popped up to make a Class to represent fractions on an Arduino, so after some coding a 0.1.00 version of the Fraction library was "born".

The scope of the fraction library is to have fractions with a denominator of max 4 digits. For many applications this is adequate.

There are several todo's
- negative fractions.
- rewrite to support larger numbers by extracting the integral part (5/4 ==> 1+1/4)
- more tests


As always remarks and comments are welcome
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

+ updated to version 0.1.06

code can be found on github - https://github.com/RobTillaart/Arduino/tree/master/libraries/Fraction
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

kowalski

Nice work!

Have you looked at the GCC AVR Fixed Point Support? That might be of interest at least to compare performance with.

Cheers!


pYro_65

Just added a pull-request with a few mods to possibly generate smaller code, and faster execution.

The operators the class provides have been modified to reflect the standard declaration pattern.

https://github.com/RobTillaart/Arduino/pull/17

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy