0.1.12 Fraction

This commit is contained in:
rob tillaart 2021-11-01 15:56:22 +01:00
parent 10ab4b2c4c
commit 40458c32e1
13 changed files with 146 additions and 56 deletions

View File

@ -2,6 +2,10 @@ compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
- leonardo
- due
- zero
# - due
# - zero
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560

View File

@ -4,10 +4,14 @@ name: Arduino CI
on: [push, pull_request]
jobs:
arduino_ci:
runTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: Arduino-CI/action@master
# Arduino-CI/action@v0.1.1
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb

View File

@ -1,23 +1,26 @@
//
// FILE: FractionFindSum.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// VERSION: 0.1.2
// PURPOSE: demo
// DATE: 13-feb-2015
// URL: https://github.com/RobTillaart/Fraction
//
#include "fraction.h"
uint32_t start;
uint32_t stop;
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(115200);
Serial.print("\n\nStart F: ");
Serial.println(FRACTIONLIBVERSION);
Serial.println(FRACTION_LIB_VERSION);
Serial.println();
randomSeed(analogRead(A0) * 256 + analogRead(A1));
@ -32,6 +35,7 @@ void setup()
}
}
void findSum(Fraction f)
{
Fraction z(0, 1);
@ -63,4 +67,6 @@ void loop()
{
}
// -- END OF FILE --

View File

@ -9,10 +9,13 @@
// this method is not that fast but it shows a nice application for
// the mediant.
#include "fraction.h"
uint32_t start, stop;
void setup()
{
Serial.begin(115200);
@ -39,7 +42,6 @@ void setup()
Serial.println();
Serial.println("done...\n");
}
@ -104,5 +106,5 @@ Fraction fractionize(float f)
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -1,24 +1,27 @@
//
// FILE: fractionExerciser.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// VERSION: 0.1.2
// PURPOSE: demo sketch for fraction math
// DATE: 2015-03-29
// URL: https://github.com/RobTillaart/Fraction
//
#include "fraction.h"
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(115200);
Serial.print("\n\nStart fractionExcerciser: ");
// Serial.println(FRACTIONLIBVERSION);
// Serial.println(FRACTION_LIB_VERSION);
Serial.println();
randomSeed(analogRead(A0) * 256 + analogRead(A1));
}
Fraction readFraction()
{
int a = 0;
@ -45,6 +48,7 @@ Fraction readFraction()
return fr;
}
char choice()
{
while (Serial.available()) Serial.read();
@ -52,6 +56,7 @@ char choice()
return Serial.read();
}
void loop()
{
Serial.println("\n\n\which fractions exercise:");
@ -75,6 +80,7 @@ void loop()
Serial.println(count);
}
int add(int n)
{
int count = 0;
@ -97,6 +103,7 @@ int add(int n)
return count;
}
int sub(int n)
{
int count = 0;
@ -119,6 +126,7 @@ int sub(int n)
return count;
}
int mul(int n)
{
int count = 0;
@ -141,6 +149,7 @@ int mul(int n)
return count;
}
int div(int n)
{
int count = 0;
@ -163,6 +172,7 @@ int div(int n)
return count;
}
int equ(int n)
{
int count = 0;
@ -186,3 +196,5 @@ int equ(int n)
return count;
}
// -- END OF FILE --

View File

@ -1,14 +1,16 @@
//
// FILE: fractionTest01.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// VERSION: 0.1.2
// PURPOSE: test sketch for fraction math
// DATE: 2015-01-25
// URL: https://github.com/RobTillaart/Fraction
//
#include "fraction.h"
Fraction q(0.42);
Fraction a(1, 3);
Fraction aa(3, 9);
@ -18,11 +20,12 @@ Fraction p(5, 1);
Fraction pi(PI);
Fraction e(EULER);
void setup()
{
Serial.begin(115200);
Serial.print("Start fractionTest: ");
Serial.println(FRACTIONLIBVERSION);
Serial.println(FRACTION_LIB_VERSION);
Serial.println();
Serial.println(a);
@ -50,10 +53,12 @@ void setup()
testGE();
}
void loop()
{
}
void testPlus()
{
Serial.println("testPlus");
@ -67,6 +72,7 @@ void testPlus()
Serial.println();
}
void testMin()
{
Serial.println("testMin");
@ -80,6 +86,7 @@ void testMin()
Serial.println();
}
void testMul()
{
Serial.println("testMul");
@ -93,6 +100,7 @@ void testMul()
Serial.println();
}
void testDiv()
{
Serial.println("testDiv");
@ -117,6 +125,7 @@ void testEQ()
Serial.println();
}
void testNEQ()
{
Serial.println("testNEQ 1 0 0");
@ -127,6 +136,7 @@ void testNEQ()
Serial.println();
}
void testLS()
{
Serial.println("testLS 0 0");
@ -136,6 +146,7 @@ void testLS()
Serial.println();
}
void testLE()
{
Serial.println("testLE 0 1");
@ -145,6 +156,7 @@ void testLE()
Serial.println();
}
void testGR()
{
Serial.println("testGR 1 0");
@ -154,6 +166,7 @@ void testGR()
Serial.println();
}
void testGE()
{
Serial.println("testGE 1 1");
@ -163,3 +176,6 @@ void testGE()
Serial.println();
}
// -- END OF FILE --

View File

@ -1,31 +1,32 @@
//
// FILE: fraction.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.11
// VERSION: 0.1.12
// PURPOSE: Arduino library to implement a Fraction datatype
// URL: https://github.com/RobTillaart/Fraction
//
//
// TODO
// - divide by zero errors
// - test extensively
//
// 0.1.11 2020-12-23 arduino-CI + unit tests
// 0.1.10 2020-06-10 fix library.json
// 0.1.9 refactor
// 0.1.8 refactor made constructors explicit; fix issue #33 double --> float
// 0.1.07 major refactoring by Chris-A
// 0.1.06 added proper(), mediant(), angle();
// 0.1.05 tested negative Fractions math, added constructors,
// HISTORY
// 0.1.12 2021-11-01 update Arduino-CI, badges,
// refactor
// 0.1.11 2020-12-23 arduino-CI + unit tests
// 0.1.10 2020-06-10 fix library.json
// 0.1.9 refactor
// 0.1.8 refactor made constructors explicit; fix issue #33 double --> float
// 0.1.07 major refactoring by Chris-A
// 0.1.06 added proper(), mediant(), angle();
// 0.1.05 tested negative Fractions math, added constructors,
// minor refactoring,
// 0.1.04 stabilizing code, add simplify() for some code paths.
// 0.1.03 added toDouble(), tested several fractionize() codes, bug fixes.
// 0.1.02 faster fractionize code
// 0.1.01 some fixes
// 0.1.00 initial version
// 0.1.04 stabilizing code, add simplify() for some code paths.
// 0.1.03 added toDouble(), tested several fractionize() codes, bug fixes.
// 0.1.02 faster fractionize code
// 0.1.01 some fixes
// 0.1.00 initial version
#include "fraction.h"
//////////////////////////////////////
//
// CONSTRUCTORS
@ -35,11 +36,13 @@ Fraction::Fraction(double d)
Fraction::split(float(d));
}
Fraction::Fraction(float f)
{
Fraction::split(f);
}
void Fraction::split(float f)
{
// handle special cases?
@ -91,6 +94,7 @@ void Fraction::split(float f)
}
}
Fraction::Fraction(int32_t p, int32_t q) : n(p), d(q)
{
simplify();
@ -119,6 +123,7 @@ size_t Fraction::printTo(Print& p) const
return s;
};
//////////////////////////////////////
//
// EQUALITIES
@ -134,31 +139,37 @@ bool Fraction::operator == (const Fraction &c)
// return (n * c.d) == (d * c.n);
// }
bool Fraction::operator != (const Fraction &c)
{
return (n * c.d) != (d * c.n);
}
bool Fraction::operator > (const Fraction &c)
{
return (n * c.d) > (d * c.n);
}
bool Fraction::operator >= (const Fraction &c)
{
return (n * c.d) >= (d * c.n);
}
bool Fraction::operator < (const Fraction &c)
{
return (n * c.d) < (d * c.n);
}
bool Fraction::operator <= (const Fraction &c)
{
return (n * c.d) <= (d * c.n);
}
//////////////////////////////////////
//
// NEGATE
@ -168,6 +179,7 @@ Fraction Fraction::operator - ()
return Fraction(-n, d);
}
//////////////////////////////////////
//
// BASIC MATH
@ -181,6 +193,7 @@ Fraction Fraction::operator + (const Fraction &c)
return Fraction(n*c.d + c.n*d, d * c.d);
}
Fraction Fraction::operator - (const Fraction &c)
{
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);
}
Fraction Fraction::operator * (const Fraction &c)
{
return Fraction(n * c.n, d * c.d);
}
Fraction Fraction::operator / (const Fraction &c)
{
// division by zero returns 0
return Fraction(n * c.d, d * c.n);
}
Fraction& Fraction::operator += (const Fraction &c)
{
if (d == c.d)
@ -216,6 +232,7 @@ Fraction& Fraction::operator += (const Fraction &c)
return *this;
}
Fraction& Fraction::operator -= (const Fraction &c)
{
if (d == c.d)
@ -231,6 +248,7 @@ Fraction& Fraction::operator -= (const Fraction &c)
return *this;
}
Fraction& Fraction::operator *= (const Fraction &c)
{
n *= c.n;
@ -239,6 +257,7 @@ Fraction& Fraction::operator *= (const Fraction &c)
return *this;
}
Fraction& Fraction::operator /= (const Fraction &c)
{
// division by zero returns 0
@ -248,17 +267,20 @@ Fraction& Fraction::operator /= (const Fraction &c)
return *this;
}
float Fraction::toDouble()
{
return (1.0 * n) / d;
}
// fraction is proper if abs(fraction) < 1
bool Fraction::isProper()
{
return abs(n) < abs(d);
}
// visualize fraction as an angle in degrees
float Fraction::toAngle()
{
@ -280,19 +302,21 @@ float Fraction::toAngle()
//
// 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)
{
return Fraction(a.n + b.n, a.d + b.d);
}
// 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)
{
return Fraction(a.n*b.d + b.n*a.d, 2 * a.d * b.d);
}
// approximate a fraction with defined denominator
// sort of setDenominator(uint16_t den);
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;
}
// not that simple ...
void Fraction::simplify()
{
@ -349,6 +374,7 @@ void Fraction::simplify()
d = q;
}
//////////////////////////////////////////////////////////////////////////////
//
// fractionize() - finds the fraction representation of a float
@ -414,4 +440,6 @@ void Fraction::fractionize(float val)
d = high.d;
}
// -- END OF FILE --

View File

@ -2,14 +2,16 @@
//
// FILE: fraction.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.11
// VERSION: 0.1.12
// PURPOSE: Arduino library to implement a Fraction datatype
// URL: https://github.com/RobTillaart/Fraction
//
#include "Arduino.h"
#define FRACTIONLIBVERSION "0.1.11"
#define FRACTION_LIB_VERSION (F("0.1.12"))
class Fraction: public Printable
{
@ -17,6 +19,7 @@ public:
explicit Fraction(double);
explicit Fraction(float);
Fraction(int32_t, int32_t);
explicit Fraction(int32_t p) : n(p), d(1) {}
explicit Fraction(int16_t p) : n(p), d(1) {}
explicit Fraction(int8_t p) : n(p), d(1) {}
@ -39,7 +42,7 @@ public:
// negation
Fraction operator - ();
// basic math
// basic maths
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&);
float toDouble();
float toFloat() { return toDouble(); };
bool isProper(); // abs(f) < 1
float toAngle();
int32_t nominator() { return n; };
int32_t denominator() { return d; };
static Fraction mediant(const Fraction&, const Fraction&);
static Fraction middle(const Fraction&, const Fraction&);
// approximate a fraction with defined denominator
static Fraction setDenominator(const Fraction&, uint16_t);
@ -75,4 +82,5 @@ protected:
int32_t d;
};
// -- END OF FILE --

View File

@ -1,8 +1,10 @@
# Syntax Coloring Map For Fraction
# Syntax Colouring Map For Fraction
# Datatypes (KEYWORD1)
# Data types (KEYWORD1)
Fraction KEYWORD1
# Methods and Functions (KEYWORD2)
toDouble KEYWORD2
toFloat KEYWORD2
@ -14,5 +16,8 @@ mediant KEYWORD2
middle KEYWORD2
setDenominator KEYWORD2
# Constants (LITERAL1)
# Constants (LITERAL1)
FRACTION_LIB_VERSION LITERAL1

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/Fraction.git"
},
"version":"0.1.11",
"version":"0.1.12",
"frameworks": "arduino",
"platforms": "*"
}

View File

@ -1,5 +1,5 @@
name=Fraction
version=0.1.11
version=0.1.12
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library to implement a Fraction datatype

View File

@ -1,11 +1,15 @@
[![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)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/Fraction.svg?maxAge=3600)](https://github.com/RobTillaart/Fraction/releases)
# Fraction
Arduino library to implement a Fraction datatype (experimental)
Arduino library to implement a Fraction data type (experimental)
## Description
@ -20,16 +24,28 @@ The code is working with a number of limitations among others:
- code is experimental still.
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.
In short, use fractions with care otherwise your sketch might get broken ;)
## Operations
See examples
## Use with care
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) ??
-

View File

@ -21,6 +21,7 @@
// assertNull(actual)
// assertNotNull(actual)
#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)
{
fprintf(stderr, "VERSION:\t%s\n", FRACTIONLIBVERSION);
fprintf(stderr, "VERSION:\t%s\n", (char*) FRACTION_LIB_VERSION);
Fraction pi(PI);
assertEqual(355, pi.nominator());