11
votes
Different ways (and the fastest) to compute sines (and cosines) in Arduino
The two basic methods are mathematical calculation (with polynomials) and lookup tables.
The Arduino's math library (libm, part of avr-libc) uses the former. It is optimised for the AVR in that it ...
11
votes
On an esp8266 what is faster, 64-bit math or float math?
Calculating the relative performance of 64 bit integer versus floating point multiplication is a little more difficult than it would appear.
It's easy to time a loop that does thousands of ...
8
votes
Accepted
Is there a non-float alternative to pow()?
For the general case, @dat_ha 's answer is correct, but it is worth noting that you want a very special case... powers of two. Because computers use binary arithmetic, operations involving powers of ...
8
votes
Different ways (and the fastest) to compute sines (and cosines) in Arduino
There are some good answers here but I wanted to add a method which hasn't been mentioned yet, one very well suited to computing trigonometric functions on embedded systems, and that's the CORDIC ...
8
votes
Accepted
How are errors (not related to syntax) managed in arduino and in the AVR architecture in general?
The simple answer is: they are not handled at all.
According to the C and C++ standards, what you are invoking is called
undefined behavior, meaning anything can happen. In practical
terms, it means ...
7
votes
Different ways (and the fastest) to compute sines (and cosines) in Arduino
I have been playing a bit with computing sines and cosines on the
Arduino using fixed-point polynomial approximations. Here are my
measurements of average execution time and worst case error, compared
...
6
votes
Accepted
Unexpected negative values from abs() function inline with analogRead
abs() is defined in Arduino.h as a macro:
#define abs(x) ((x)>0?(x):-(x))
A macro does not evaluate in the same way as a function.
sensor = abs(analogRead(A0) - 512);
This statement will be ...
6
votes
Accepted
Calculating Amp/Hrs of a solar panel
How to make your own coulomb counter (Amp*hours [Ahr] meter, or Watts*hours [Whr] energy meter) with an Arduino
Quick summary:
Jump straight down to the "Full coulomb counter example in code"...
6
votes
On an esp8266 what is faster, 64-bit math or float math?
OK, I took the trouble to code fixed-point routines and test them. Here is the code ...
// fixed-point routines for 20 bit integer / 12-bit fraction
// format 0xfffff.fff, max value is +-524288.9998
...
5
votes
eval() function for Arduino?
TinyExpr does what you want, and more.
TinyExpr is a very small recursive descent parser and evaluation engine for math expressions. It's handy when you want to add the ability to evaluation math ...
5
votes
Accepted
Is it possible to use mathematical sets and their operations in arduino?
The Standard Template Library (STL) can be installed on the Arduino. It is not there by default. That includes things like sets.
One implementation is here.
4
votes
Different ways (and the fastest) to compute sines (and cosines) in Arduino
I looked for other people that had approximated cos() and sin() and I came across this answer:
dtb's answer to "Fast Sin/Cos using a pre computed translation array"
Basically he computed that the ...
4
votes
Different ways (and the fastest) to compute sines (and cosines) in Arduino
You could create a couple of functions that uses linear approximation to determine the sin() and cos() of a particular angle.
I am thinking something like this:
For each I have broken the graphical ...
4
votes
Accepted
Rounding up and Down
As far as I know, the Serial.print (or println) does rounding up and down. All you have to do is take a float variable and do Serial.print( value, 1);
Without extra parameter, the default is choosen, ...
4
votes
Rounding up and Down
You can round in C by multiplying for the significance, adding +0.5, round down (equals as casting to an integer) and divide.
float f_rounded = ((int) (f * 10.0 + 0.5) / 10.0);
28.6 will be:
float ...
3
votes
Different ways (and the fastest) to compute sines (and cosines) in Arduino
Just for the fun of it, and to prove it can be done, I finished an AVR assembly routine to calculate sin(x) results in 24 bits (3 bytes) with one bit of error. The input angle is in degrees with one ...
3
votes
Arduino pow() making 9's
I put this code into the IDE, and for n>=2, I got 99, 999, 9999 etc. The catch is that Arduino uses floating point arithmetic to implement the pow() function, and some of your values are being ...
3
votes
Arduino pow() making 9's
pow() works with floating point numbers.
Floating point numbers are just an approximation. You will very rarely get precise results using floating point numbers.
Instead you could write your own ...
3
votes
Different ways (and the fastest) to compute sines (and cosines) in Arduino
A lookup table will be the fastest way to find sines. And if you're comfortable computing with fixed-point numbers (integers whose binary-point is somewhere other than to the right of bit-0), your ...
3
votes
Accepted
How do I find the whole number (n) that when multiplied by (m) will be closest to (x)?
First, I'll show you the "manual" way.
You can divide the resulting steps by the number they should be divisible by, round to the next integer, and multiply that again by the number. I'll ...
2
votes
Accepted
Performing 32-bit arithmetic on embedded device
You wrote:
the 16 most significant should be accurate.
Are you absolutely sure about that? Please reconsider. It is easy, when
asking a question, to ask for something better than what you strictly
...
2
votes
Accepted
Arduino randomly does incorrect addition
int only goes from -32768 to 32768, and rolls-over, as you've seen.
Use a long data type instead and you can go MUCH further from zero. Use double instead of float likewise, when you need more room. ...
2
votes
Converting `millis()` to decimal seconds?
Dividing an integer by an integer (1000) yields an integer, regardless of what you cast that result to.
Instead you need to divide the integer by a float, or cast the integer to a float first. The ...
2
votes
Accepted
Is there any way to group variables like group.variable as you can in Java/Delphi
What you are looking for is a struct:
struct foo {
uint8_t Ux;
uint8_t Uy;
uint8_t Yz;
};
Then:
struct foo A;
struct foo B;
And:
A.Ux = 3;
B.Uz = 48;
int c = A.Ux + B.Uz;
When ...
2
votes
Accepted
Formula to calculate energy required to power an Arduino board
In the datasheet you can read how much every mode uses (normal mode, sleep mode etc). However, it also depends on which peripherals inside the CPU are active and all electronics around it. So make ...
2
votes
Why is this IIR Filter unstable on the Arduino Due?
The precision of a float is about 7 significant digits. The
constant term of B in your transfer function (0.00000376) has only 3
significant digits.
Your transfer function has a pole at z = 1. It is ...
2
votes
Different ways (and the fastest) to compute sines (and cosines) in Arduino
I had a simillar question to OP. I wanted to make a LUT table to calculate the first quadrant of the sine function as unsigned 16 bit integers starting from 0x8000 to 0xffff. And i ended up writing ...
2
votes
Arduino calculator: I'm struggling to write a sketch which parses inputted characters
Characters in a char[] variable may be encoded in ASCII or Unicode. You will want to convert them to numbers in this case. Consider subtracting the ascii value of one less than 0 from each char to get ...
2
votes
Different ways (and the fastest) to compute sines (and cosines) in Arduino
As others have mentioned lookup tables are the way to go if you want speed. I've recently been investigating computation of trig functions on an ATtiny85 for use of fast vector averages (wind in my ...
2
votes
How to get average of 16bit ADC readings?
If you are trying to average out three sensor ADC reading first you need to store and add them all then divide it by 3.
int16_t rawData =0;
int32_t avg=0,ambient=0;
int i=0;
while(i<3) {
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
mathematics × 43arduino-uno × 14
float × 5
programming × 4
accelerometer × 4
arduino-ide × 3
analogread × 3
variables × 3
serial × 2
arduino-nano × 2
c++ × 2
library × 2
mpu6050 × 2
arduino-micro × 2
adc × 2
code-optimization × 2
signal-processing × 2
gyroscope × 2
esp8266 × 1
sensors × 1
power × 1
led × 1
i2c × 1
lcd × 1
arduino-due × 1