Tag Info

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 ...
• 103k

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 ...
• 1,314
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 ...
• 206

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 ...
• 181
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 ...
• 38.4k

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 ...
• 38.4k
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 ...
• 7,881
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"...
• 1,308

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 ...
• 267

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 ...
• 149
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.
• 35.5k
Accepted

LED bar graph script not working

Both v and vref are integers, which means that integer division will be performed. if v is ever less than vref then the result will be 0. Multiply first, then divide.
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, ...
• 3,127

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 ...
• 12.6k

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 ...
• 3,108

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 ...
• 3,108

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 ...
• 14.6k

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 ...
Accepted

Trouble with large numbers

freq * long, since they are both integers, are calculated as integers. 440*256=112640 - in binary that is 1 1011 1000 0000 0000 and trimmed to 16 bits becomes 1011 1000 0000 0000 which is -18432 and ...
• 103k
Accepted

Avoiding float math to speed up arduino

What you are asking for is not simple, especially since you have this power function that can be hard to approximate. First, you have to figure out the range and the required precision of each of the ...
• 38.4k

Printing decimal numbers

In languages such as C or C++ there is a distinction between integer types and floating-point types. You see, computers work using bits. A bit is either 0 or 1. A bit obviously lack in both range and ...

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 ...
• 103k

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 ...
• 701

Printing decimal numbers

In the C family of languages, if you divide an integer by another integer, the compiler interprets the division operator (/) as an integer division, i.e. you get the integer part of the quotient. Thus,...
• 38.4k
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 ...
• 38.4k

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 ...
Accepted

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. ...
• 1,016

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 ...
• 103k