0.1.13 Angle

This commit is contained in:
rob tillaart 2022-10-29 12:45:36 +02:00
parent 3ced6ab10e
commit 86307cd8cb
9 changed files with 141 additions and 64 deletions

View File

@ -1,3 +1,18 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
@ -7,5 +22,7 @@ compile:
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560
- esp8266
# - mega2560
- rpipico

View File

@ -1,26 +1,12 @@
//
// FILE: Angle.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.12
// VERSION: 0.1.13
// PURPOSE: library for Angle math for Arduino
// URL: https://github.com/RobTillaart/Angle
// http://forum.arduino.cc/index.php?topic=339402
//
// HISTORY
// 0.1.00 initial version
// 0.1.01 clean up a bit
// 0.1.02 added toRadians() + fix compare()
// 0.1.03 added URL, fromRadians [TEST]
// 0.1.04 changed thousands in tenthousands, string constructor
// 0.1.05 added AngleFormat proxy added 03/03/15 by Christoper Andrews.
// 0.1.06 fixed bug negative values.
// 0.1.7 2020-03-26 refactor #pragma once
// 0.1.8 2020-05-27 update library.json
// 0.1.9 2020-12-10 Arduino CI
// 0.1.10 2021-01-16 readme.md + minor refactor
// 0.1.11 2021-10-17 update build-CI,
// 0.1.12 2021-12-12 update library.json, license, minor edits
// HISTORY: see changelog.md
#include "Angle.h"
@ -38,23 +24,25 @@ size_t AngleFormat::printTo(Print& p) const
}
Angle::Angle(int dd, int mm, int ss, int tt) // TODO optimize
Angle::Angle(int dd, int mm, int ss, int tt)
{
neg = false;
d = dd;
m = mm;
s = ss;
t = tt;
// TODO
// normalize();
// assume only one (largest) parameter is negative at most...
// TODO
// normalize();
// assume only one (largest) parameter is negative at most...
if (d < 0) { d = -d; neg = true; }
if (m < 0) { m = -m; neg = true; }
if (s < 0) { s = -s; neg = true; }
if (t < 0) { t = -t; neg = true; }
// modulo might be faster
while (t >= 10000) { s++; t -= 10000; }
while (s >= 60) { m++; s -= 60; }
while (m >= 60) { d++; m -= 60; }
// check special case 0
if (d == 0 && m == 0 && s == 0 && t == 0) neg = false;
}
@ -67,18 +55,20 @@ Angle::Angle(const double alpha)
d = int(a);
a = a - d;
// unsigned long p = a * 3600000L;
// 3600 000 = 2^7 • 3^2 • 5^5 = 128 * 28125
// 2^7 = 128 will only affect exponent - no loss precision
// 28125 is less digits so less loss of significant digits.
// upgraded to 4 decimal seconds
// 36 000 000L = 256 * 140625
// unsigned long p = a * 3600000L;
// 3600 000 = 2^7 • 3^2 • 5^5 = 128 * 28125
// 2^7 = 128 will only affect exponent - no loss precision
// 28125 is less digits so less loss of significant digits.
// upgraded to 4 decimal seconds
// 36 000 000L = 256 * 140625
a = a * 256;
unsigned long p = round(a * 140625.0);
t = p % 10000UL;
p = p / 10000UL;
s = p % 60UL;
m = p / 60UL;
// check special case 0
if (d == 0 && m == 0 && s == 0 && t == 0) neg = false;
}
@ -87,26 +77,26 @@ Angle::Angle(const char * str)
uint32_t yy = 0;
uint8_t d_cnt = 0;
neg = false;
// parse whole degrees
// parse whole degrees
char *p = (char *) str;
d = 0;
// skip crap
// skip crap
while (!isdigit(*p) && (*p != '-')) p++;
// process sign
// process sign
if (*p == '-')
{
neg = true;
p++;
}
if (*p == '+') p++;
// parse whole part into degrees;
// parse whole part into degrees;
while (isdigit(*p))
{
d *= 10;
d += (*p - '0');
p++;
}
// parse decimal part into an uint32_t
// parse decimal part into an uint32_t
if (*p != '\0')
{
p++; // skip .
@ -118,29 +108,31 @@ Angle::Angle(const char * str)
p++;
}
}
// make sure we have 9 decimal places.
// make sure we have 9 decimal places.
while (d_cnt < 9)
{
d_cnt++;
yy *= 10;
}
// convert float to degrees. 1000000000 ~> 36000000 -> /250 * 9
// yy = yy * 4 / 125 + yy / 250; // just keeps the maths within 32 bits
// convert float to degrees. 1000000000 ~> 36000000 -> /250 * 9
// yy = yy * 4 / 125 + yy / 250; // just keeps the maths within 32 bits
yy = yy * 4 / 125;
yy = yy + (yy + 4)/ 8; // just keeps the maths within 32 bits
// split yy in m, s, tt
yy = yy + (yy + 4)/ 8; // just keeps the maths within 32 bits
// split yy in m, s, tt
t = yy % 10000UL;
yy = yy / 10000UL;
s = yy % 60;
m = yy / 60;
// check special case 0
if (d == 0 && m == 0 && s == 0 && t == 0) neg = false;
}
// PRINTING
// PRINTING
size_t Angle::printTo(Print& p, AngleFormatMode mode) const
{
unsigned char c = mode;
char separator[4] = ".\'\""; // "..."; // ALT-0176 = ° 179.59.59.9999
char separator[4] = ".\'\""; // "..."; // ALT-0176 = ° 179.59.59.9999
size_t n = 0;
if (neg) n += p.print('-');
@ -178,7 +170,7 @@ double Angle::toDouble(void)
}
// NEGATE
// NEGATE
Angle Angle::operator - ()
{
Angle temp = *this;
@ -194,7 +186,7 @@ Angle Angle::operator - ()
};
// BASIC MATH
// BASIC MATH
Angle Angle::operator + (const Angle &a) // TOCHECK
{
return addHelper(a);
@ -231,20 +223,20 @@ Angle Angle::addHelper(const Angle &a) // TOCHECK
}
Angle Angle::operator - (const Angle &a) // TOCHECK
Angle Angle::operator - (const Angle &a) // TOCHECK
{
return subHelper(a);
}
Angle& Angle::operator -= (const Angle &a) // TOCHECK
Angle& Angle::operator -= (const Angle &a) // TOCHECK
{
*this = subHelper(a);
return *this;
}
Angle Angle::subHelper(const Angle &a) // TOCHECK
Angle Angle::subHelper(const Angle &a) // TOCHECK
{
Angle temp = *this;
if (temp.neg == a.neg)
@ -292,7 +284,7 @@ Angle& Angle::operator /= (const double dd)
}
// !! can divide by zero
// !! can divide by zero
double Angle::operator / (Angle& a)
{
double f = this->toDouble();
@ -303,14 +295,14 @@ double Angle::operator / (Angle& a)
///////////////////////////////////////////////////////////
//
// PRIVATE
// PRIVATE
//
int Angle::compare(const Angle &a, const Angle &b)
{
// check sign first
// check sign first
if (!a.neg && b.neg) return 1;
if (a.neg && !b.neg) return -1;
// check abs value
// check abs value
int rv = 0;
if (a.d > b.d) rv = 1;
else if (a.d < b.d) rv = -1;
@ -326,7 +318,7 @@ int Angle::compare(const Angle &a, const Angle &b)
}
void Angle::normalize() // TODO CHECK
void Angle::normalize() // TODO CHECK
{
while (t < 0) { s--; t += 10000; }
while (t >= 10000) { s++; t -= 10000; }

View File

@ -2,7 +2,7 @@
//
// FILE: Angle.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.12
// VERSION: 0.1.13
// PURPOSE: angle library for Arduino
// HISTORY: See angle.cpp
//
@ -15,7 +15,7 @@
#include "Printable.h"
#define ANGLE_LIB_VERSION (F("0.1.12"))
#define ANGLE_LIB_VERSION (F("0.1.13"))
class Angle;
@ -57,7 +57,7 @@ public:
double toRadians() { return toDouble() * PI / 180.0; };
void fromRadians(double rad) { *this = rad * 180.0/PI; };
// EQUALITIES
// EQUALITIES
bool operator == (const Angle& a) { return compare(*this, a) == 0; };
bool operator != (const Angle& a) { return compare(*this, a) != 0; };
bool operator < (const Angle& a) { return compare(*this, a) < 0; };
@ -65,7 +65,7 @@ public:
bool operator > (const Angle& a) { return compare(*this, a) > 0; };
bool operator >= (const Angle& a) { return compare(*this, a) >= 0; };
// NEGATE
// NEGATE
Angle operator - ();
Angle operator + (const Angle&);
@ -80,7 +80,7 @@ public:
Angle operator / (const double);
Angle& operator /= (const double);
double operator / (Angle&); // ratio
double operator / (Angle&); // ratio
private:
void normalize();
@ -89,11 +89,11 @@ private:
Angle addHelper(const Angle &a);
Angle subHelper(const Angle &a);
bool neg; // angle is negative
int d; // whole degrees
int m; // minutes
int s; // seconds
int t; // ten thousands
bool neg; // angle is negative
int d; // whole degrees
int m; // minutes
int s; // seconds
int t; // ten thousands
};

View File

@ -0,0 +1,61 @@
# Change Log Angle
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.1.13] - 2022-10-12
- Add RP2040 support to build-CI
- Add CHANGELOG.md
- minor change unit test
- minor edits (comments)
- Add zero tests to two constructors (zero is not negative)
## [0.1.12] - 2021-12-12
- update library.json
- update license
- minor edits
## [0.1.11] - 2021-10-17
- update build-CI
## [0.1.10] - 2021-01-16
- update readme.md
- minor refactor
## [0.1.9] - 2020-12-10
- Add build Arduino CI
## [0.1.8] - 2020-05-27
- update library.json
## [0.1.7] - 2020-03-26
- refactor #pragma once
## [0.1.06]
- fixed bug negative values
## [0.1.05] 2015-03-03
- added AngleFormat
- proxy added 03/03/15 by Christoper Andrews
## [0.1.04]
- changed thousands in tenthousands
- add string constructor
## [0.1.03]
- added URL
- fromRadians (test)
## [0.1.02]
- added toRadians()
- fix compare()
## [0.1.01]
- clean up a bit
## [0.1.00]
- initial version

View File

@ -80,5 +80,10 @@ bugs. Especially the constructor does not check input so use it carefully.
- improve documentation
- test more
- optimize code where possible
- performance sketch
- improve code quality
- fix TODO in code
- use better variable names in code
- move all code to .cpp

View File

@ -11,6 +11,7 @@ degree KEYWORD2
minute KEYWORD2
second KEYWORD2
tenthousand KEYWORD2
toDouble KEYWORD2
toRadians KEYWORD2
fromRadians KEYWORD2

View File

@ -20,7 +20,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/Angle.git"
},
"version": "0.1.12",
"version": "0.1.13",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=Angle
version=0.1.12
version=0.1.13
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library to convert between floating point angle to minutes hours representation.

View File

@ -39,6 +39,7 @@
unittest_setup()
{
fprintf(stderr, "ANGLE_LIB_VERSION: %s\n", (char *) ANGLE_LIB_VERSION);
}
unittest_teardown()
@ -48,8 +49,6 @@ unittest_teardown()
unittest(test_constructors)
{
fprintf(stderr, "ANGLE_LIB_VERSION: %s\n", (char *) ANGLE_LIB_VERSION);
Angle a(1, 2, 3, 4);
Angle b(-45, 30);
Angle n(0);
@ -87,6 +86,7 @@ unittest(test_constructors)
assertEqual(0, s.tenthousand());
}
unittest(test_toDouble)
{
Angle a(1, 2, 3, 4);
@ -108,6 +108,7 @@ unittest(test_toDouble)
assertMoreOrEqual(38, fs);
}
unittest(test_Radians)
{
Angle a(1, 2, 3, 4);