fmodf issue

hello,
there i was trying to "copy" a spreadsheet into a mega. Some functions were mod(float;float) so the "%" way of arduino mod could not be used. (ok, perhaps it can be used if floats are converted to ints..., but i thought it wouldnt worth the try). I decided to try fmodf.

And there it goes. Some results are correct and some not correct. After 2-3 hours i tested the following:

#include <math.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println(fmodf(10.0, 4.0));
  Serial.println(fmodf(-10.0, 4.0));
  Serial.println(fmodf(-10.0, -4.0));
  Serial.println(fmodf(10.0, -4.0));
}

void loop() {
  // put your main code here, to run repeatedly:

}

and ..voile...
2.00
-2.00
-2.00
2.00

But, modulus cannot be negative, correct?

The GCC compiler can return a negative value.
Wikipedia has extra information: Modulo - Wikipedia. They say: "When either a or n is negative, the naive definition breaks down and programming languages differ in how these values are defined". There is also a list for the different programming languages.

What is correct ? You tell me :confused:

According to that Wikipedia page, the behaviour is well defined for C and C++. The result can be negative, so the Arduino (which uses the GCC compiler) is according to the standard. Everything is therefor perfectly fine... until you start using fmod() or fmodf() and are expecting something else.

Koepel:
The GCC compiler can return a negative value.
Wikipedia has extra information: Modulo - Wikipedia. They say: "When either a or n is negative, the naive definition breaks down and programming languages differ in how these values are defined". There is also a list for the different programming languages.

What is correct ? You tell me :confused:

According to that Wikipedia page, the behaviour is well defined for C and C++. The result can be negative, so the Arduino (which uses the GCC compiler) is according to the standard. Everything is therefor perfectly fine... until you start using fmod() or fmodf() and are expecting something else.

I didnot expect it, so my fault, I didn't look for it before posting.
You see, it is the different use in spreadsheet that leads to different calculation.
Anyway, my opinion is that :

this is NOT mathematically correct,
arduino may be based on C, but I dont see any "arduino" approach on float%float (ie why the arduino user must be stuck to C, whilst there could be another "arduinomath.h" lib and use whatever he wants).

the situation is not supportive
thanks

Mathematically, I think you are correct, it should return a positive number.

However, somehow the standard for C and C++ is that it can return a negative number. That is the standard for C and C++ for a long time and it will probably stay that way. So every C and C++ compiler (and the Arduino) should follow the C and C++ standard. The C and C++ compilers and the Arduino have no choice.

When you use C or C++ you should remember that every C and C++ math library can return a negative number.
The fmod() is not used a lot, and you can not change the C and C++ standard on your own :frowning:

There is some "math oriented" discussion here: Classroom Resources - National Council of Teachers of Mathematics
The tables in the previously-link wikipedia page are fascinating - so little agreement for such a common operation!