Steelmesh:
GoForSmoke:
// why not
int v = (int) ( 4000UL * z / x ); // it does the math in 32 bit unsigned THEN casts the result to signed 16 bit.One answer is variable types and casting.
Also AVR has no FPU and only has 32 bit floats whereas Arduino supports 64 bit integers good to 19 places and can do those faster than floats good to 6 places, here's the missing:
I have much to learn, can you please verify my assumptions of what just happened?
Referring to: int v = (int) ( 4000UL * z / x )
"int v" declared an integer variable as usual
"(int)" requested int output?
"(4000UL * z / x)" the operation in the parentheses uses more math friendly 32-bit stuff; the #UL just declares that 4000 as an unsigned long
(int) casts the output to type int to fit v, otherwise v would get the 16 wrong bits of a 32 bit result.
This is assuming that v is percent as in 0 to 100 or could it be 1000 or more %?
The L means use a 32 bit integer and the letter U means code it to use rollover-friendly unsigned math. Not "just".