hello everyone
im using the remainder() function to calculate the mode of 2 positive double numbers but sometimes the result is negetive!
could you please tell me why does it happen? and how much this function is accurate?
The function std::remainder(x,y) gives you the closest value to zero you can get by subtracting y from x. Sometimes that will be a negative number. This function does NOT give the leftover positive value when you have divided x by y.
I think you thought this was a different function. I think you're looking for the modulo operator, %
Specific functions or operators aside, what are you actually trying to solve? Mixing floating-point numbers with remainder logic may lead to unexpected results.
fmod() is % for doubles, sort of. You REALLY need to review what that function does to see if it is what you seek. you may also simply want (int)x % (int)y