mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.12 Fraction
This commit is contained in:
parent
10ab4b2c4c
commit
40458c32e1
@ -2,6 +2,10 @@ compile:
|
|||||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||||
platforms:
|
platforms:
|
||||||
- uno
|
- uno
|
||||||
- leonardo
|
# - due
|
||||||
- due
|
# - zero
|
||||||
- zero
|
# - leonardo
|
||||||
|
- m4
|
||||||
|
- esp32
|
||||||
|
# - esp8266
|
||||||
|
# - mega2560
|
@ -4,10 +4,14 @@ name: Arduino CI
|
|||||||
on: [push, pull_request]
|
on: [push, pull_request]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
arduino_ci:
|
runTest:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- uses: Arduino-CI/action@master
|
- uses: ruby/setup-ruby@v1
|
||||||
# Arduino-CI/action@v0.1.1
|
with:
|
||||||
|
ruby-version: 2.6
|
||||||
|
- run: |
|
||||||
|
gem install arduino_ci
|
||||||
|
arduino_ci.rb
|
||||||
|
@ -1,23 +1,26 @@
|
|||||||
//
|
//
|
||||||
// FILE: FractionFindSum.ino
|
// FILE: FractionFindSum.ino
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.1.1
|
// VERSION: 0.1.2
|
||||||
// PURPOSE: demo
|
// PURPOSE: demo
|
||||||
// DATE: 13-feb-2015
|
// DATE: 13-feb-2015
|
||||||
// URL: https://github.com/RobTillaart/Fraction
|
// URL: https://github.com/RobTillaart/Fraction
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
#include "fraction.h"
|
#include "fraction.h"
|
||||||
|
|
||||||
|
|
||||||
uint32_t start;
|
uint32_t start;
|
||||||
uint32_t stop;
|
uint32_t stop;
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
pinMode(13, OUTPUT);
|
pinMode(13, OUTPUT);
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.print("\n\nStart F: ");
|
Serial.print("\n\nStart F: ");
|
||||||
Serial.println(FRACTIONLIBVERSION);
|
Serial.println(FRACTION_LIB_VERSION);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
randomSeed(analogRead(A0) * 256 + analogRead(A1));
|
randomSeed(analogRead(A0) * 256 + analogRead(A1));
|
||||||
|
|
||||||
@ -32,6 +35,7 @@ void setup()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void findSum(Fraction f)
|
void findSum(Fraction f)
|
||||||
{
|
{
|
||||||
Fraction z(0, 1);
|
Fraction z(0, 1);
|
||||||
@ -63,4 +67,6 @@ void loop()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// -- END OF FILE --
|
// -- END OF FILE --
|
||||||
|
|
||||||
|
@ -9,10 +9,13 @@
|
|||||||
// this method is not that fast but it shows a nice application for
|
// this method is not that fast but it shows a nice application for
|
||||||
// the mediant.
|
// the mediant.
|
||||||
|
|
||||||
|
|
||||||
#include "fraction.h"
|
#include "fraction.h"
|
||||||
|
|
||||||
|
|
||||||
uint32_t start, stop;
|
uint32_t start, stop;
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
@ -39,7 +42,6 @@ void setup()
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
Serial.println("done...\n");
|
Serial.println("done...\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -104,5 +106,5 @@ Fraction fractionize(float f)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -- END OF FILE --
|
||||||
|
|
||||||
// -- END OF FILE --
|
|
@ -1,24 +1,27 @@
|
|||||||
//
|
//
|
||||||
// FILE: fractionExerciser.ino
|
// FILE: fractionExerciser.ino
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.1.1
|
// VERSION: 0.1.2
|
||||||
// PURPOSE: demo sketch for fraction math
|
// PURPOSE: demo sketch for fraction math
|
||||||
// DATE: 2015-03-29
|
// DATE: 2015-03-29
|
||||||
// URL: https://github.com/RobTillaart/Fraction
|
// URL: https://github.com/RobTillaart/Fraction
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
#include "fraction.h"
|
#include "fraction.h"
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
pinMode(13, OUTPUT);
|
pinMode(13, OUTPUT);
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.print("\n\nStart fractionExcerciser: ");
|
Serial.print("\n\nStart fractionExcerciser: ");
|
||||||
// Serial.println(FRACTIONLIBVERSION);
|
// Serial.println(FRACTION_LIB_VERSION);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
randomSeed(analogRead(A0) * 256 + analogRead(A1));
|
randomSeed(analogRead(A0) * 256 + analogRead(A1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Fraction readFraction()
|
Fraction readFraction()
|
||||||
{
|
{
|
||||||
int a = 0;
|
int a = 0;
|
||||||
@ -45,6 +48,7 @@ Fraction readFraction()
|
|||||||
return fr;
|
return fr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char choice()
|
char choice()
|
||||||
{
|
{
|
||||||
while (Serial.available()) Serial.read();
|
while (Serial.available()) Serial.read();
|
||||||
@ -52,6 +56,7 @@ char choice()
|
|||||||
return Serial.read();
|
return Serial.read();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
Serial.println("\n\n\which fractions exercise:");
|
Serial.println("\n\n\which fractions exercise:");
|
||||||
@ -75,6 +80,7 @@ void loop()
|
|||||||
Serial.println(count);
|
Serial.println(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int add(int n)
|
int add(int n)
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@ -97,6 +103,7 @@ int add(int n)
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int sub(int n)
|
int sub(int n)
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@ -119,6 +126,7 @@ int sub(int n)
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int mul(int n)
|
int mul(int n)
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@ -141,6 +149,7 @@ int mul(int n)
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int div(int n)
|
int div(int n)
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@ -163,6 +172,7 @@ int div(int n)
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int equ(int n)
|
int equ(int n)
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@ -186,3 +196,5 @@ int equ(int n)
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -- END OF FILE --
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
//
|
//
|
||||||
// FILE: fractionTest01.ino
|
// FILE: fractionTest01.ino
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.1.1
|
// VERSION: 0.1.2
|
||||||
// PURPOSE: test sketch for fraction math
|
// PURPOSE: test sketch for fraction math
|
||||||
// DATE: 2015-01-25
|
// DATE: 2015-01-25
|
||||||
// URL: https://github.com/RobTillaart/Fraction
|
// URL: https://github.com/RobTillaart/Fraction
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
#include "fraction.h"
|
#include "fraction.h"
|
||||||
|
|
||||||
|
|
||||||
Fraction q(0.42);
|
Fraction q(0.42);
|
||||||
Fraction a(1, 3);
|
Fraction a(1, 3);
|
||||||
Fraction aa(3, 9);
|
Fraction aa(3, 9);
|
||||||
@ -18,11 +20,12 @@ Fraction p(5, 1);
|
|||||||
Fraction pi(PI);
|
Fraction pi(PI);
|
||||||
Fraction e(EULER);
|
Fraction e(EULER);
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.print("Start fractionTest: ");
|
Serial.print("Start fractionTest: ");
|
||||||
Serial.println(FRACTIONLIBVERSION);
|
Serial.println(FRACTION_LIB_VERSION);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
Serial.println(a);
|
Serial.println(a);
|
||||||
@ -50,10 +53,12 @@ void setup()
|
|||||||
testGE();
|
testGE();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void testPlus()
|
void testPlus()
|
||||||
{
|
{
|
||||||
Serial.println("testPlus");
|
Serial.println("testPlus");
|
||||||
@ -67,6 +72,7 @@ void testPlus()
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void testMin()
|
void testMin()
|
||||||
{
|
{
|
||||||
Serial.println("testMin");
|
Serial.println("testMin");
|
||||||
@ -80,6 +86,7 @@ void testMin()
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void testMul()
|
void testMul()
|
||||||
{
|
{
|
||||||
Serial.println("testMul");
|
Serial.println("testMul");
|
||||||
@ -93,6 +100,7 @@ void testMul()
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void testDiv()
|
void testDiv()
|
||||||
{
|
{
|
||||||
Serial.println("testDiv");
|
Serial.println("testDiv");
|
||||||
@ -117,6 +125,7 @@ void testEQ()
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void testNEQ()
|
void testNEQ()
|
||||||
{
|
{
|
||||||
Serial.println("testNEQ 1 0 0");
|
Serial.println("testNEQ 1 0 0");
|
||||||
@ -127,6 +136,7 @@ void testNEQ()
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void testLS()
|
void testLS()
|
||||||
{
|
{
|
||||||
Serial.println("testLS 0 0");
|
Serial.println("testLS 0 0");
|
||||||
@ -136,6 +146,7 @@ void testLS()
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void testLE()
|
void testLE()
|
||||||
{
|
{
|
||||||
Serial.println("testLE 0 1");
|
Serial.println("testLE 0 1");
|
||||||
@ -145,6 +156,7 @@ void testLE()
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void testGR()
|
void testGR()
|
||||||
{
|
{
|
||||||
Serial.println("testGR 1 0");
|
Serial.println("testGR 1 0");
|
||||||
@ -154,6 +166,7 @@ void testGR()
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void testGE()
|
void testGE()
|
||||||
{
|
{
|
||||||
Serial.println("testGE 1 1");
|
Serial.println("testGE 1 1");
|
||||||
@ -163,3 +176,6 @@ void testGE()
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -- END OF FILE --
|
||||||
|
|
||||||
|
@ -1,31 +1,32 @@
|
|||||||
//
|
//
|
||||||
// FILE: fraction.cpp
|
// FILE: fraction.cpp
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.1.11
|
// VERSION: 0.1.12
|
||||||
// PURPOSE: Arduino library to implement a Fraction datatype
|
// PURPOSE: Arduino library to implement a Fraction datatype
|
||||||
// URL: https://github.com/RobTillaart/Fraction
|
// URL: https://github.com/RobTillaart/Fraction
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// TODO
|
// HISTORY
|
||||||
// - divide by zero errors
|
// 0.1.12 2021-11-01 update Arduino-CI, badges,
|
||||||
// - test extensively
|
// refactor
|
||||||
//
|
// 0.1.11 2020-12-23 arduino-CI + unit tests
|
||||||
// 0.1.11 2020-12-23 arduino-CI + unit tests
|
// 0.1.10 2020-06-10 fix library.json
|
||||||
// 0.1.10 2020-06-10 fix library.json
|
// 0.1.9 refactor
|
||||||
// 0.1.9 refactor
|
// 0.1.8 refactor made constructors explicit; fix issue #33 double --> float
|
||||||
// 0.1.8 refactor made constructors explicit; fix issue #33 double --> float
|
// 0.1.07 major refactoring by Chris-A
|
||||||
// 0.1.07 major refactoring by Chris-A
|
// 0.1.06 added proper(), mediant(), angle();
|
||||||
// 0.1.06 added proper(), mediant(), angle();
|
// 0.1.05 tested negative Fractions math, added constructors,
|
||||||
// 0.1.05 tested negative Fractions math, added constructors,
|
|
||||||
// minor refactoring,
|
// minor refactoring,
|
||||||
// 0.1.04 stabilizing code, add simplify() for some code paths.
|
// 0.1.04 stabilizing code, add simplify() for some code paths.
|
||||||
// 0.1.03 added toDouble(), tested several fractionize() codes, bug fixes.
|
// 0.1.03 added toDouble(), tested several fractionize() codes, bug fixes.
|
||||||
// 0.1.02 faster fractionize code
|
// 0.1.02 faster fractionize code
|
||||||
// 0.1.01 some fixes
|
// 0.1.01 some fixes
|
||||||
// 0.1.00 initial version
|
// 0.1.00 initial version
|
||||||
|
|
||||||
|
|
||||||
#include "fraction.h"
|
#include "fraction.h"
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
//
|
//
|
||||||
// CONSTRUCTORS
|
// CONSTRUCTORS
|
||||||
@ -35,11 +36,13 @@ Fraction::Fraction(double d)
|
|||||||
Fraction::split(float(d));
|
Fraction::split(float(d));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Fraction::Fraction(float f)
|
Fraction::Fraction(float f)
|
||||||
{
|
{
|
||||||
Fraction::split(f);
|
Fraction::split(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Fraction::split(float f)
|
void Fraction::split(float f)
|
||||||
{
|
{
|
||||||
// handle special cases?
|
// handle special cases?
|
||||||
@ -91,6 +94,7 @@ void Fraction::split(float f)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Fraction::Fraction(int32_t p, int32_t q) : n(p), d(q)
|
Fraction::Fraction(int32_t p, int32_t q) : n(p), d(q)
|
||||||
{
|
{
|
||||||
simplify();
|
simplify();
|
||||||
@ -119,6 +123,7 @@ size_t Fraction::printTo(Print& p) const
|
|||||||
return s;
|
return s;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
//
|
//
|
||||||
// EQUALITIES
|
// EQUALITIES
|
||||||
@ -134,31 +139,37 @@ bool Fraction::operator == (const Fraction &c)
|
|||||||
// return (n * c.d) == (d * c.n);
|
// return (n * c.d) == (d * c.n);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
bool Fraction::operator != (const Fraction &c)
|
bool Fraction::operator != (const Fraction &c)
|
||||||
{
|
{
|
||||||
return (n * c.d) != (d * c.n);
|
return (n * c.d) != (d * c.n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Fraction::operator > (const Fraction &c)
|
bool Fraction::operator > (const Fraction &c)
|
||||||
{
|
{
|
||||||
return (n * c.d) > (d * c.n);
|
return (n * c.d) > (d * c.n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Fraction::operator >= (const Fraction &c)
|
bool Fraction::operator >= (const Fraction &c)
|
||||||
{
|
{
|
||||||
return (n * c.d) >= (d * c.n);
|
return (n * c.d) >= (d * c.n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Fraction::operator < (const Fraction &c)
|
bool Fraction::operator < (const Fraction &c)
|
||||||
{
|
{
|
||||||
return (n * c.d) < (d * c.n);
|
return (n * c.d) < (d * c.n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Fraction::operator <= (const Fraction &c)
|
bool Fraction::operator <= (const Fraction &c)
|
||||||
{
|
{
|
||||||
return (n * c.d) <= (d * c.n);
|
return (n * c.d) <= (d * c.n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
//
|
//
|
||||||
// NEGATE
|
// NEGATE
|
||||||
@ -168,6 +179,7 @@ Fraction Fraction::operator - ()
|
|||||||
return Fraction(-n, d);
|
return Fraction(-n, d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
//
|
//
|
||||||
// BASIC MATH
|
// BASIC MATH
|
||||||
@ -181,6 +193,7 @@ Fraction Fraction::operator + (const Fraction &c)
|
|||||||
return Fraction(n*c.d + c.n*d, d * c.d);
|
return Fraction(n*c.d + c.n*d, d * c.d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Fraction Fraction::operator - (const Fraction &c)
|
Fraction Fraction::operator - (const Fraction &c)
|
||||||
{
|
{
|
||||||
if (d == c.d)
|
if (d == c.d)
|
||||||
@ -190,17 +203,20 @@ Fraction Fraction::operator - (const Fraction &c)
|
|||||||
return Fraction(n*c.d - c.n*d, d * c.d);
|
return Fraction(n*c.d - c.n*d, d * c.d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Fraction Fraction::operator * (const Fraction &c)
|
Fraction Fraction::operator * (const Fraction &c)
|
||||||
{
|
{
|
||||||
return Fraction(n * c.n, d * c.d);
|
return Fraction(n * c.n, d * c.d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Fraction Fraction::operator / (const Fraction &c)
|
Fraction Fraction::operator / (const Fraction &c)
|
||||||
{
|
{
|
||||||
// division by zero returns 0
|
// division by zero returns 0
|
||||||
return Fraction(n * c.d, d * c.n);
|
return Fraction(n * c.d, d * c.n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Fraction& Fraction::operator += (const Fraction &c)
|
Fraction& Fraction::operator += (const Fraction &c)
|
||||||
{
|
{
|
||||||
if (d == c.d)
|
if (d == c.d)
|
||||||
@ -216,6 +232,7 @@ Fraction& Fraction::operator += (const Fraction &c)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Fraction& Fraction::operator -= (const Fraction &c)
|
Fraction& Fraction::operator -= (const Fraction &c)
|
||||||
{
|
{
|
||||||
if (d == c.d)
|
if (d == c.d)
|
||||||
@ -231,6 +248,7 @@ Fraction& Fraction::operator -= (const Fraction &c)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Fraction& Fraction::operator *= (const Fraction &c)
|
Fraction& Fraction::operator *= (const Fraction &c)
|
||||||
{
|
{
|
||||||
n *= c.n;
|
n *= c.n;
|
||||||
@ -239,6 +257,7 @@ Fraction& Fraction::operator *= (const Fraction &c)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Fraction& Fraction::operator /= (const Fraction &c)
|
Fraction& Fraction::operator /= (const Fraction &c)
|
||||||
{
|
{
|
||||||
// division by zero returns 0
|
// division by zero returns 0
|
||||||
@ -248,17 +267,20 @@ Fraction& Fraction::operator /= (const Fraction &c)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
float Fraction::toDouble()
|
float Fraction::toDouble()
|
||||||
{
|
{
|
||||||
return (1.0 * n) / d;
|
return (1.0 * n) / d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// fraction is proper if abs(fraction) < 1
|
// fraction is proper if abs(fraction) < 1
|
||||||
bool Fraction::isProper()
|
bool Fraction::isProper()
|
||||||
{
|
{
|
||||||
return abs(n) < abs(d);
|
return abs(n) < abs(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// visualize fraction as an angle in degrees
|
// visualize fraction as an angle in degrees
|
||||||
float Fraction::toAngle()
|
float Fraction::toAngle()
|
||||||
{
|
{
|
||||||
@ -280,19 +302,21 @@ float Fraction::toAngle()
|
|||||||
//
|
//
|
||||||
|
|
||||||
// the mediant is a fraction that is always between 2 fractions
|
// the mediant is a fraction that is always between 2 fractions
|
||||||
// at least if within precission.
|
// at least if within precision.
|
||||||
Fraction Fraction::mediant(const Fraction &a, const Fraction &b)
|
Fraction Fraction::mediant(const Fraction &a, const Fraction &b)
|
||||||
{
|
{
|
||||||
return Fraction(a.n + b.n, a.d + b.d);
|
return Fraction(a.n + b.n, a.d + b.d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// the middle is a fraction that is between 2 fractions
|
// the middle is a fraction that is between 2 fractions
|
||||||
// at least if within precission.
|
// at least if within precision.
|
||||||
Fraction Fraction::middle(const Fraction &a, const Fraction &b)
|
Fraction Fraction::middle(const Fraction &a, const Fraction &b)
|
||||||
{
|
{
|
||||||
return Fraction(a.n*b.d + b.n*a.d, 2 * a.d * b.d);
|
return Fraction(a.n*b.d + b.n*a.d, 2 * a.d * b.d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// approximate a fraction with defined denominator
|
// approximate a fraction with defined denominator
|
||||||
// sort of setDenominator(uint16_t den);
|
// sort of setDenominator(uint16_t den);
|
||||||
Fraction Fraction::setDenominator(const Fraction &a, uint16_t b)
|
Fraction Fraction::setDenominator(const Fraction &a, uint16_t b)
|
||||||
@ -319,6 +343,7 @@ int32_t Fraction::gcd(int32_t a , int32_t b)
|
|||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// not that simple ...
|
// not that simple ...
|
||||||
void Fraction::simplify()
|
void Fraction::simplify()
|
||||||
{
|
{
|
||||||
@ -349,6 +374,7 @@ void Fraction::simplify()
|
|||||||
d = q;
|
d = q;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// fractionize() - finds the fraction representation of a float
|
// fractionize() - finds the fraction representation of a float
|
||||||
@ -414,4 +440,6 @@ void Fraction::fractionize(float val)
|
|||||||
d = high.d;
|
d = high.d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// -- END OF FILE --
|
// -- END OF FILE --
|
||||||
|
|
||||||
|
@ -2,14 +2,16 @@
|
|||||||
//
|
//
|
||||||
// FILE: fraction.h
|
// FILE: fraction.h
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.1.11
|
// VERSION: 0.1.12
|
||||||
// PURPOSE: Arduino library to implement a Fraction datatype
|
// PURPOSE: Arduino library to implement a Fraction datatype
|
||||||
// URL: https://github.com/RobTillaart/Fraction
|
// URL: https://github.com/RobTillaart/Fraction
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
|
|
||||||
#define FRACTIONLIBVERSION "0.1.11"
|
#define FRACTION_LIB_VERSION (F("0.1.12"))
|
||||||
|
|
||||||
|
|
||||||
class Fraction: public Printable
|
class Fraction: public Printable
|
||||||
{
|
{
|
||||||
@ -17,6 +19,7 @@ public:
|
|||||||
explicit Fraction(double);
|
explicit Fraction(double);
|
||||||
explicit Fraction(float);
|
explicit Fraction(float);
|
||||||
Fraction(int32_t, int32_t);
|
Fraction(int32_t, int32_t);
|
||||||
|
|
||||||
explicit Fraction(int32_t p) : n(p), d(1) {}
|
explicit Fraction(int32_t p) : n(p), d(1) {}
|
||||||
explicit Fraction(int16_t p) : n(p), d(1) {}
|
explicit Fraction(int16_t p) : n(p), d(1) {}
|
||||||
explicit Fraction(int8_t p) : n(p), d(1) {}
|
explicit Fraction(int8_t p) : n(p), d(1) {}
|
||||||
@ -39,7 +42,7 @@ public:
|
|||||||
// negation
|
// negation
|
||||||
Fraction operator - ();
|
Fraction operator - ();
|
||||||
|
|
||||||
// basic math
|
// basic maths
|
||||||
Fraction operator + (const Fraction&);
|
Fraction operator + (const Fraction&);
|
||||||
Fraction operator - (const Fraction&);
|
Fraction operator - (const Fraction&);
|
||||||
Fraction operator * (const Fraction&);
|
Fraction operator * (const Fraction&);
|
||||||
@ -50,17 +53,21 @@ public:
|
|||||||
Fraction& operator *= (const Fraction&);
|
Fraction& operator *= (const Fraction&);
|
||||||
Fraction& operator /= (const Fraction&);
|
Fraction& operator /= (const Fraction&);
|
||||||
|
|
||||||
|
|
||||||
float toDouble();
|
float toDouble();
|
||||||
float toFloat() { return toDouble(); };
|
float toFloat() { return toDouble(); };
|
||||||
bool isProper(); // abs(f) < 1
|
bool isProper(); // abs(f) < 1
|
||||||
float toAngle();
|
float toAngle();
|
||||||
|
|
||||||
|
|
||||||
int32_t nominator() { return n; };
|
int32_t nominator() { return n; };
|
||||||
int32_t denominator() { return d; };
|
int32_t denominator() { return d; };
|
||||||
|
|
||||||
|
|
||||||
static Fraction mediant(const Fraction&, const Fraction&);
|
static Fraction mediant(const Fraction&, const Fraction&);
|
||||||
static Fraction middle(const Fraction&, const Fraction&);
|
static Fraction middle(const Fraction&, const Fraction&);
|
||||||
|
|
||||||
|
|
||||||
// approximate a fraction with defined denominator
|
// approximate a fraction with defined denominator
|
||||||
static Fraction setDenominator(const Fraction&, uint16_t);
|
static Fraction setDenominator(const Fraction&, uint16_t);
|
||||||
|
|
||||||
@ -75,4 +82,5 @@ protected:
|
|||||||
int32_t d;
|
int32_t d;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// -- END OF FILE --
|
// -- END OF FILE --
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
# Syntax Coloring Map For Fraction
|
# Syntax Colouring Map For Fraction
|
||||||
|
|
||||||
# Datatypes (KEYWORD1)
|
|
||||||
|
# Data types (KEYWORD1)
|
||||||
Fraction KEYWORD1
|
Fraction KEYWORD1
|
||||||
|
|
||||||
|
|
||||||
# Methods and Functions (KEYWORD2)
|
# Methods and Functions (KEYWORD2)
|
||||||
toDouble KEYWORD2
|
toDouble KEYWORD2
|
||||||
toFloat KEYWORD2
|
toFloat KEYWORD2
|
||||||
@ -14,5 +16,8 @@ mediant KEYWORD2
|
|||||||
middle KEYWORD2
|
middle KEYWORD2
|
||||||
setDenominator KEYWORD2
|
setDenominator KEYWORD2
|
||||||
|
|
||||||
# Constants (LITERAL1)
|
|
||||||
|
# Constants (LITERAL1)
|
||||||
|
FRACTION_LIB_VERSION LITERAL1
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/RobTillaart/Fraction.git"
|
"url": "https://github.com/RobTillaart/Fraction.git"
|
||||||
},
|
},
|
||||||
"version":"0.1.11",
|
"version":"0.1.12",
|
||||||
"frameworks": "arduino",
|
"frameworks": "arduino",
|
||||||
"platforms": "*"
|
"platforms": "*"
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name=Fraction
|
name=Fraction
|
||||||
version=0.1.11
|
version=0.1.12
|
||||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
sentence=Arduino library to implement a Fraction datatype
|
sentence=Arduino library to implement a Fraction datatype
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
|
|
||||||
[![Arduino CI](https://github.com/RobTillaart/Fraction/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
[![Arduino CI](https://github.com/RobTillaart/Fraction/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
||||||
|
[![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)
|
||||||
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/Fraction/blob/master/LICENSE)
|
[![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)
|
[![GitHub release](https://img.shields.io/github/release/RobTillaart/Fraction.svg?maxAge=3600)](https://github.com/RobTillaart/Fraction/releases)
|
||||||
|
|
||||||
|
|
||||||
# Fraction
|
# Fraction
|
||||||
|
|
||||||
Arduino library to implement a Fraction datatype (experimental)
|
Arduino library to implement a Fraction data type (experimental)
|
||||||
|
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
@ -20,16 +24,28 @@ The code is working with a number of limitations among others:
|
|||||||
- code is experimental still.
|
- code is experimental still.
|
||||||
|
|
||||||
That said, the library is useful e.g. to display float numbers as a fraction.
|
That said, the library is useful e.g. to display float numbers as a fraction.
|
||||||
From programming point of view the fractionize function, converting a double
|
From programming point of view the **fractionize()** function, converting a double
|
||||||
into a fraction is a nice programming problem, fast with a minimal error.
|
into a fraction is a nice programming problem, fast with a minimal error.
|
||||||
|
|
||||||
In short, use fractions with care otherwise your sketch might get broken ;)
|
In short, use fractions with care otherwise your sketch might get broken ;)
|
||||||
|
|
||||||
|
|
||||||
## Operations
|
## Operations
|
||||||
|
|
||||||
See examples
|
See examples
|
||||||
|
|
||||||
|
|
||||||
## Use with care
|
## Use with care
|
||||||
|
|
||||||
The library is reasonably tested, and if problems arise please let me know.
|
The library is reasonably tested, and if problems arise please let me know.
|
||||||
|
|
||||||
|
|
||||||
|
## Future
|
||||||
|
|
||||||
|
- experiment with bigger nominator/denominator using all of 32767 possibilities ?
|
||||||
|
- investigate divide by zero errors
|
||||||
|
- investigate better fractionize() - depends on nom/denom size
|
||||||
|
- test test test ...
|
||||||
|
- add famous constants as Fraction e.g FRAC_PI = (355, 113) ??
|
||||||
|
-
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
// assertNull(actual)
|
// assertNull(actual)
|
||||||
// assertNotNull(actual)
|
// assertNotNull(actual)
|
||||||
|
|
||||||
|
|
||||||
#include <ArduinoUnitTests.h>
|
#include <ArduinoUnitTests.h>
|
||||||
|
|
||||||
|
|
||||||
@ -37,22 +38,10 @@ unittest_teardown()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
unittest(test_new_operator)
|
|
||||||
{
|
|
||||||
assertEqualINF(exp(800));
|
|
||||||
assertEqualINF(0.0/0.0);
|
|
||||||
assertEqualINF(42);
|
|
||||||
|
|
||||||
assertEqualNAN(INFINITY - INFINITY);
|
|
||||||
assertEqualNAN(0.0/0.0);
|
|
||||||
assertEqualNAN(42);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
unittest(test_constructor)
|
unittest(test_constructor)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "VERSION:\t%s\n", FRACTIONLIBVERSION);
|
fprintf(stderr, "VERSION:\t%s\n", (char*) FRACTION_LIB_VERSION);
|
||||||
|
|
||||||
Fraction pi(PI);
|
Fraction pi(PI);
|
||||||
assertEqual(355, pi.nominator());
|
assertEqual(355, pi.nominator());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user