No, it was just a signature to be used in the discussion, so not implemented.
Might be a function for my fast_math library in some future (if it can be made fast enough)
A simple for loop may be faster than floats using ln() and exp().
I do not know why pow(2,2) would yield 3.999999 instead of 4.0...
Floating point numbers — Sand or dirt
I went from using floats with round-ups in small business software to applying my maths skills to using integers for speed and precision. If I want 2 digit dollars, my working units may be hundredths or thousandths giving me places to lose in divisions always done after multiplications in scaling operations. I choose my precision when I choose my tiny units. Works every time!
Only time that floats are faster is when doing powers of ten operations.
Thanks for all the insights!
Only time that floats are faster is when doing powers of ten operations.
and usable for numbers that lie between integers ![]()
and usable for numbers that lie between integers
Answer is to pick smaller units. If I want fractional meters, I might work in micrometers to get 6 places between integers. Type long gives me 9 places and IIRC type long long is good to 19 places.
You surprise me Rob, I’m pretty sure that you climbed higher on mount maths than I ever did! I only got as far as finite and infinite series.
Answer is to pick smaller units.
I understood way from the beginning. I have worked a few months on financial software and management had everything to be done in cents to get no rounding effects. Then we needed to implement VAT …
mount maths
Have seen quite some math indeed, however one of the real math mountains in the Netherlands is ASML. Not worked for ASML (only indirectly). They make one of the most impressive (and expensive) machines in the world imho. The tube is an hour.
What board are you using? With Uno on the simulator, I took the example from the docs
float x = 2.0; // base number
float y = 10.0; // exponent number
void setup() {
Serial.begin(9600);
double z = pow(x, y);
Serial.print("The power result is: ");
Serial.println(z);
}
void loop() {
}
changed the type of the result, and added some code that won't get compiled (yet)
float x = 2.0; // base number
float y = 10.0; // exponent number
void setup() {
Serial.begin(9600);
int z = pow(x, y);
Serial.print("The power result is: ");
Serial.println(z);
}
void loop() {
#ifdef SKIP_THIS
int z = pow(x, y);
Serial.print(y);
Serial.print('\t');
Serial.println(z);
if (--y < 0) for (;;) delay(1);
#endif
}
Changing double to int, it still worked "as expected", result is: 1024. Changing it to y = 3.0, it also results in 8. But changing the #ifdef to #ifndef so that it no longer skips the added code
The power result is: 1023
10.00 1023
9.00 511
8.00 255
7.00 127
6.00 63
5.00 31
4.00 15
3.00 7
2.00 3
1.00 2
0.00 1
I could not reproduce this with actual boards I have. The difference may be that on AVR, double is the same as float; the lack of precision causes whatever calculation that is done to be slightly off. There might be shortcuts if the exponent is one or zero, but otherwise it is not looping do the power calculation.
Without the code in loop, some compiler optimization could compute the single result at compile-time, and pow is not even linked into the binary uploaded to the board.