Brady Dean Brady Dean - 2 months ago 6
C++ Question

C++ std::div doesn't return expected results

I ran this code on my machine and it output:

11/5 = 2.1
Now I might be crazy but
11/5 = 2.2


I read into the reference and it says div_t.rem is x%y, which in this case does equal 1, but how can
std::div
be a feature of the language if it doesn't return the expected result of 11/5?

#include <iostream>
#include <cstdlib>

int main() {

std::div_t q;
q = std::div(11, 5);

std::cout << "11/5 = " << q.quot << '.' << q.rem << std::endl;
}

Answer

q.rem holds the remainder. 11/5 = 2 with a remainder of 1, there's nothing weird about these results.