Complex Math in arduino

I'm trying to calculate a calculation that has a lot of numbers behind the comma like 0.054631945.
but in the program it doesn't display all the results in a complex way, I've read some information about the use of mathematics in the arduino program, maybe it's because I use arduino UNO or MEGA, because only DUE can support complex calculations, is that true?
then is there a way to work around this calculation so that it can be used with complex results using Arduino UNO or MEGA?

The default for printing floating point (not "complex") numbers is two places.
This can be changed.

Serial.print (fpVariable, 6);

The Arduino Uno and Mega have a 8-bit microcontroller and a software library for single precision floating point. That is a awesome library it is fully according to the IEEE standard and it is very fast, but with complex accurate calculations the single precision can be not accurate enough.

As far as I know there is no double precision library for the Arduino Uno or Mega.
You should try an other board. The ESP32 can do double precision (in software). Also the SAMD21G on the Arduino Zero and most MKR boards can do double precision (in software).

I don't know which Arduino or compatible boards actually use the hardware double precision floating point.

There is also a BigNumber library:
That is a lot of fun, but it uses a lot of memory as well.

AVR based boards only support float, not double. The number of significant digits is 6 for a float. On the Due, you can use double (if I'm not mistaken) and the number of significant digits is 15.

Hi @sagise

Arduino Due uses the SAM3X8E, an ARM Cortex-M3 based microcontroller that supports double precision floating point numbers in software.

As already mentioned, this means that it's capable of performing calculations using the double data type, but the microcontroller doesn't have a dedicated hardware Floating Point Unit (FPU).

FPUs can be single or double precision. A FPU can significantly speed up floating point calculations, but is normally only a consideration if the calculations are absolutely time critical.

Double precision FPUs are only available on the highest end microcontrollers, such as the ARM-Cortex-M7. (PJRC's Teensy 4.0 and 4.1).

1 Like

Are you talking about complex numbers or just number of digits after decimal point? MKR has arm math library that can do some really complicated calculations. But I’ve got a feeling you do not mean that

Its closer to 7 significant digits for a single float, integer values in the range +/- 16,777,215 are represented exactly for instance

I'd rather keep it conservative; else we get the question why one not always gets 7 :smiley:

1 Like

Just to be clear, I don't think you are talking about complex numbers, which are a totally different thing (they have a 'real' and an 'imaginary' component). If you are actually referring to numbers with a lot of digits after the decimal point, then you are talking about high precision numbers.


Maybe this is interesting for some: a speedtest with double precision in Wokwi simulator.
It shows that the community build for the Raspberry Pi Pico is very good and the ESP32 is three times slower in the real world: Virtual Online Arduino and ESP32 Simulator - Wokwi Arduino Simulator features - #17 by Koepel

You also need to be careful how you write the equations, and what data types are used. If you divide an int by an int inside of parenthesis, the math is done using integer math, producing an int as the result, losing the decimal part before doing any additiinal math outside the parenthesis.

digits after decimal point


Digits after the decimal point is a completely meaningless number, unless you also specify the number of digits BEFORE the decimal point. Floating point formats support a fixed number of TOTAL significant digits. For single precision, that is ~7 digits. The following ALL contain 7 significant digits:


What is the project that needs you to calculate to this number of places , apart form printing there is little you can do with I/O with that precision ( given the resolution of the ADC and PWM outputs )?

the position of the sun using the Jean Meeus algorithm

The SolarPosition library uses an algorithm optimized for single precision floating point calculations. It is accurate to a within few arc-minutes.

is it also available to find the position of the moon?
then whether when using the library will reduce memory?

No, the SolarPosition library does not consider lunar positions. To what accuracy do you need the lunar position? The mean lunar rotation is almost constant, so if you only need long term accuracy, you can just calculate a simple cycle based on a seconds count.

Every library, and every line of code that is compiled, reduces memory. So I ask, reduce memory compared with what?

You will have to test that yourself. Implement your own version of the algorithm, compile and remember the memory usage reported by the IDE. Next write code that uses the library, compile and compare the reported memory usage with the previously remembered one.