0.1.9 FastTrig

This commit is contained in:
rob tillaart 2021-12-18 13:14:58 +01:00
parent 1a62ec98e0
commit cf87923609
16 changed files with 113 additions and 40 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

@ -2,7 +2,7 @@
//
// FILE: FastTrig.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.8
// VERSION: 0.1.9
// PURPOSE: Arduino library for a faster approximation of sin() and cos()
// DATE: 2011-08-18
// URL: https://github.com/RobTillaart/FastTrig
@ -23,10 +23,15 @@
// 0.1.7 2021-04-23 fix for PlatformIO
// 0.1.8 2021-08-10 made % 180 conditional in itan() => performance gain
// added icot() cotangent.
// 0.1.9 2021-12-18 update Arduino-CI, badges,
// update library.json, minor edits
#include "Arduino.h"
#define FAST_TRIG_LIB_VERSION (F("0.1.9"))
// 91 x 2 bytes ==> 182 bytes
// use 65535.0 as divider
uint16_t isinTable16[] = {
@ -75,6 +80,7 @@ uint8_t isinTable8[] = {
255
};
///////////////////////////////////////////////////////
//
// GONIO LOOKUP
@ -124,12 +130,14 @@ float isin(float f)
return -g;
}
float icos(float x)
{
// prevent modulo math if x in 0..360
return isin(x - 270.0); // better than x + 90;
}
float itan(float f)
{
// reference
@ -175,6 +183,7 @@ float itan(float f)
return ta;
}
// some problem at 0 but at least we have a icot(x) cotangent.
float icot(float f)
{
@ -225,15 +234,18 @@ float iasin(float f)
return (lo + delta);
}
float iacos(float f)
{
return 90 - iasin(f);
}
// PLACEHOLDER
float iatan(float f)
{
return 0 * f;
}
// -- END OF FILE --

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2011-2021 Rob Tillaart
Copyright (c) 2011-2022 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -1,5 +1,7 @@
[![Arduino CI](https://github.com/RobTillaart/FastTrig/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/FastTrig/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/FastTrig/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/FastTrig/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/FastTrig/actions/workflows/jsoncheck.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/FastTrig/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/FastTrig.svg?maxAge=3600)](https://github.com/RobTillaart/FastTrig/releases)
@ -14,19 +16,32 @@ Arduino library with interpolated lookup for sin() and cos(). Trades speed for a
**Warning: The library trades speed for accuracy so use at own risk**
The library provides one lookup table that is used for
**isin(degrees)** and **icos(degrees)** and **itan(degrees)**. This lookup table is optimized for interpolation so the values for whole degrees are not optimal. Furthermore the **itan()** on AVR has almost no performance gain over the regular **tan()** so on AVR one is adviced to use **tan()**. On ESP32 the **itan(degrees)** does have a serious performance gain so use it if you need speed.
**isin(degrees)** and **icos(degrees)** and **itan(degrees)**.
This lookup table is optimized for interpolation so the values for whole degrees are not optimal.
Furthermore the **itan()** on AVR has almost no performance gain over the regular **tan()** so on AVR one is advised to use **tan()**.
On ESP32 the **itan(degrees)** does have a serious performance gain so use it if you need speed.
These functions are to be used as replacements for **sin(radians)**, **cos(radians)**and **tan(radians)**. Important to know is that they are NOT direct replaceable as the parameter differs a factor (PI/180.0) or its inverse.
These functions are to be used as replacements for **sin(radians)**, **cos(radians)** and **tan(radians)**.
Important to know is that they are NOT direct replaceable as the parameter differs a factor (PI/180.0) or its inverse.
Similar to ```cos(x) == sin(x + PI)``` it is also true that ```icos(x) == isin(x + 90)```, so **icos()** can use the very same lookup table at the cost of a single addition. In fact it uses ```icos(x) == isin(x - 270)``` as that performs better,
due to the folding.
Similar to ```cos(x) == sin(x + PI)``` it is also true that ```icos(x) == isin(x + 90)```,
so **icos()** can use the very same lookup table at the cost of a single addition.
In fact it uses ```icos(x) == isin(x - 270)``` as that performs better, due to the folding.
The **i** in the names stands for **int** and **interpolated** as the core is using integer math and lookuptable of 91 uint16_t = 182 bytes. By folding and mirroring the whole 360 degrees and beyond can be handled. When **isin(x)** is called and ```x == int(x)``` then the library will not interpolate and this will improve performance. When x is not a whole number the library will linear interpolate between **isin(int(x))** and **isin(int(x+1))**. Of course this introduces an error but it is quite fast (which was the goal).
The **i** in the names stands for **int** and **interpolated** as the core is using integer math and lookup table of 91 uint16_t = 182 bytes.
By folding and mirroring the whole 360 degrees and beyond can be handled.
When **isin(x)** is called and ```x == int(x)``` then the library will not interpolate and this will improve performance.
When x is not a whole number the library will linear interpolate between **isin(int(x))** and **isin(int(x+1))**.
Of course this introduces an error but it is quite fast (which was the goal).
#### Lookup tables
The lookup tables are optimized (sketch provided) to minimize the error when using the interpolation, this implies that the points in the table might not be optimal when you use only wholde degrees. A sketch that generates lookup tables is in the examples folder. This generator sketch can also generate tables with different resolution e.g. 24, 14, 12 or even 6, 5 or 4 bit lookup tables. So depending on the application these tables can be ideal, but verify they meet your requirements.
The lookup tables are optimized (sketch provided) to minimize the error when using the interpolation,
this implies that the points in the table might not be optimal when you use only whole degrees.
A sketch that generates lookup tables is in the examples folder.
This generator sketch can also generate tables with different resolution e.g. 24, 14, 12 or even 6, 5 or 4 bit lookup tables.
So depending on the application these tables can be ideal, but verify they meet your requirements.
The lookup tables used by **isin()** can be used directly in your program, the names are:
- **isinTable16\[\]** index 0..90, values need to be (float) divided by 65535.0
@ -70,6 +85,7 @@ values outside the 0..360 range.
Please, verify the performance to see if it meets your requirements.
## Accuracy isin icos itan
errors - based upon example sketch - lib version 0.1.5
@ -93,10 +109,11 @@ UNO calls 0.0 - 360.0 step 0.1 degree
*Note: 0.1.3 for AVR was bad: 17.41900634 , 0.02249339 , 0.02953807 for itan() *
Strange that the **itan()** on UNO and ESP32 differs (OK same order of magnitude).
Different implementation of gonio / float math?
Different implementation of goniometry / float maths?
Please, verify the performance to see if it meets your requirements.
## Performance iasin iacos iatan
(added 0.1.5)
@ -113,10 +130,11 @@ time in us - calls -1 ..+1 step 0.001 degree
| iatan | NI | NI |
- the interpolated reverse lookup is around 30% faster on UNO an 80+% on ESP32
- iatan is Not Implemented.
- **iatan()** is **Not** Implemented.
Please, verify the accuracy to see if it meets your requirements.
## Accuracy iasin iacos iatan
(added 0.1.5)
@ -131,7 +149,7 @@ ESP32 calls -1 ..+1 step 0.001 degree
| iatan | NI | NI | NI | NI |
- largest error at 0.999981 - second largest error 0.052841 at -0.999000
- iatan is Not Implemented
- **iatan()** is **Not** Implemented
@ -144,8 +162,8 @@ UNO calls -1 ..+1 step 0.001 degree
| iatan | NI | NI | NI | NI |
- largest error at 0.999981 - second largest error 0.052841 at -0.999000
- max rel error is high as it occured near zero.
- iatan is Not Implemented
- max relative error is high as it occurred near zero.
- **iatan()** is **Not** Implemented
Please, verify the accuracy to see if it meets your requirements.
@ -186,14 +204,18 @@ There is no **atan()** or **atan2()** replacement.
- Made the % 180 in the **itan()** conditional.
- added **icot(f)**
## 0.1.9
## TODO
- How to improve the accuracy of the whole degrees, as now the table is optimized for interpolation.
- update library.json, badges, version string, minor edits.
## Operation
See examples
## Future
- How to improve the accuracy of the whole degrees, as now the table is optimized for interpolation.
- version info in release_notes.md file.

View File

@ -1,7 +1,6 @@
//
// FILE: fastTrig_generate_tables.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: generate look up tables for gonio functions (and others)
// these are not optimized for interpolation.
// DATE: 2020-09-08
@ -10,6 +9,7 @@
// TODO
// tables might have some trouble at "max values" CHECK
#include "Arduino.h"
@ -38,13 +38,16 @@ void setup()
generate_bit_tan(16);
generate_bit_tan(8);
Serial.println("\n//done...");
}
void loop()
{
}
void generate_header()
{
Serial.println();
@ -146,4 +149,6 @@ void generate_bit_tan(int t)
Serial.println();
}
// -- END OF FILE --

View File

@ -1,7 +1,6 @@
//
// FILE: fastTrig_optimize.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// PURPOSE: sketch to optimize the table for interpolation
// DATE: 2020-09-06
@ -9,8 +8,10 @@
// TODO make a python script for this ?
#include "FastTrig.h"
float getError(int i)
{
float error = 0;
@ -120,9 +121,10 @@ void test_isin_error_1(bool show)
}
void loop()
{
}
// -- END OF FILE --

View File

@ -1,7 +1,6 @@
//
// FILE: fastTrig_plot.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// PURPOSE: testing the fastTrigonio functions
// DATE: 2020-09-07
@ -15,6 +14,7 @@
volatile float x;
int i;
void setup()
{
Serial.begin(115200);
@ -62,8 +62,11 @@ void setup()
Serial.println("done...\n");
}
void loop()
{
}
// -- END OF FILE --

View File

@ -1,7 +1,6 @@
//
// FILE: fastTrig_test1.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// PURPOSE: testing the fastTrigonio functions
// DATE: 2020-08-30
// (c) : MIT
@ -15,6 +14,7 @@ uint32_t start, d1, d2;
volatile float x;
int i;
void setup()
{
Serial.begin(115200);
@ -305,4 +305,5 @@ void loop()
{
}
// -- END OF FILE --

View File

@ -4,7 +4,7 @@
// PURPOSE: testing the itan functions
// DATE: 2021-08-10
// (c) : MIT
//
#include "FastTrig.h"
@ -14,6 +14,7 @@ uint32_t start, d1, d2;
volatile float x;
int i;
void setup()
{
Serial.begin(115200);
@ -26,7 +27,6 @@ void setup()
}
void test_tan(int n)
{
Serial.println(__FUNCTION__);
@ -57,6 +57,7 @@ void test_tan(int n)
delay(10);
}
void test_itan_error_1(bool show)
{
Serial.println(__FUNCTION__);
@ -119,4 +120,6 @@ void loop()
{
}
// -- END OF FILE --

View File

@ -15,6 +15,7 @@ volatile float x;
int i;
float f;
void setup()
{
Serial.begin(115200);
@ -32,6 +33,7 @@ void setup()
Serial.println("done...\n");
}
/////////////////////////////////////////////////////////////
//
//
@ -287,4 +289,6 @@ void loop()
{
}
// -- END OF FILE --

View File

@ -1,7 +1,6 @@
//
// FILE: fastTrig_playground.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: playground to play with tables.
// DATE: 2020-09-08
@ -31,6 +30,7 @@
// tables generated with other sketch.
uint32_t isinTable24[] = {
0,
292803, 585516, 878052, 1170319, 1462231, 1753697, 2044628, 2334937, 2624535, 2913333,
@ -44,6 +44,7 @@ uint32_t isinTable24[] = {
16570660, 16613940, 16652160, 16685308, 16713372, 16736348, 16754222, 16766994, 16774660, 16777215,
};
uint32_t isinTable20[] = {
0,
18300, 36595, 54878, 73145, 91389, 109606, 127789, 145933, 164033, 182083,
@ -57,6 +58,7 @@ uint32_t isinTable20[] = {
1035665, 1038370, 1040759, 1042831, 1044585, 1046021, 1047138, 1047936, 1048415, 1048575,
};
uint16_t isinTable16[] = {
0,
1144, 2287, 3430, 4571, 5712, 6850, 7987, 9121, 10252, 11380,
@ -160,10 +162,11 @@ void test_performance()
sum = 0;
}
void loop()
{
}
// -- END OF FILE --

View File

@ -1,17 +1,20 @@
# Syntax Coloring Map For FastTrig
# Syntax Colouring Map For FastTrig
# Datatypes (KEYWORD1)
# Data types (KEYWORD1)
# Methods and Functions (KEYWORD2)
isin KEYWORD2
icos KEYWORD2
itan KEYWORD2
icot KEYWORD2
iasin KEYWORD2
iacos KEYWORD2
# Instances (KEYWORD2)
# Constants (LITERAL1)
isinTable16 LITERAL1
isinTable8 LITERAL1

View File

@ -15,8 +15,9 @@
"type": "git",
"url": "https://github.com/RobTillaart/FastTrig"
},
"version": "0.1.8",
"version": "0.1.9",
"license": "MIT",
"frameworks": "*",
"platforms": "*"
"platforms": "*",
"headers": "FastTrig.h"
}

View File

@ -1,5 +1,5 @@
name=FastTrig
version=0.1.8
version=0.1.9
author=Rob Tillaart <rob.tillaart@gmail.com><pete.thompson@yahoo.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library with interpolated lookup for sin() and cos()

View File

@ -30,8 +30,10 @@
unittest_setup()
{
fprintf(stderr, "FAST_TRIG_LIB_VERSION: %s\n", (char *) FAST_TRIG_LIB_VERSION);
}
unittest_teardown()
{
}
@ -47,6 +49,7 @@ unittest(test_isinTable16)
}
}
unittest(test_isinTable8)
{
fprintf(stderr,"Table8 error is < 1%% \n");
@ -57,6 +60,7 @@ unittest(test_isinTable8)
}
}
unittest(test_max_error_table16)
{
fprintf(stderr,"Table16 max error: ");
@ -71,6 +75,7 @@ unittest(test_max_error_table16)
assertEqualFloat(0, m, 0.001);
}
unittest(test_max_error_table8)
{
fprintf(stderr,"Table8 max error: ");
@ -85,6 +90,7 @@ unittest(test_max_error_table8)
assertEqualFloat(0, m, 0.01);
}
unittest_main()
// --------