1

I am trying to find the number of times one double divides into another in C++ and the only function I found to do so was remquo. But when using it it does not give the expected results with the resulting quotients being only between 0 and 3 when they should be from 0 to 20.

smaller sized doubles work does it have problems with more precise floating point numbers?

int quotient;
    double remainder = remquo((coordX+maxX), tileSizeX, &quotient);
    cout << coordX+maxX << " / " << tileSizeX << " = " << quotient << " r " << remainder << endl;
    return quotient;

In this case maxX will be 0.773 and coordX will be a number from -0.773 to 0.773 and lastly tileSizeX will be about 0.073619.

here are some outputs of the cout call:

0.773 / 0.073619 = 2 r 0.0368095 but 0.073619 * 2 + 0.0368095 = 0.1840475, nowhere near 0.773 like I expected.

0.085 / 0.073619 = 0 r 0.011381 <- wrong

0.181 / 0.073619 = 2 r 0.0337619 <- right

0.433 / 0.073619 = 3 r -0.00871429 <- wrong

1.269 / 0.073619 = 0 r 0.0174762 <- wrong

The remainder always seem right but the quotient almost never is. Is there something I am not understanding.

  • Please post a [MCVE](https://stackoverflow.com/help/mcve) and also give compiler version and OS – M.M May 08 '19 at 01:49
  • 1
    Looking at [documentation](https://en.cppreference.com/w/cpp/numeric/math/remquo), *Additionally, the sign and at least the three of the last bits of x/y will be stored in quo, sufficient to determine the octant of the result within a period.*. 3 bits isn't very much. – Shawn May 08 '19 at 01:55
  • 1
    Also the remainder is defined as *The IEEE floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n\*y, where the value n is the integral value nearest the exact value x/y. When |n-x/y| = ½, the value n is chosen to be even.*, which is not what you're trying to treat it as. – Shawn May 08 '19 at 01:59
  • the MCVE is important as I think there are rounding errors not shown in your code; 0.773/0.073619 is actually very slightly larger than 10.5 , so the remainder should be negative, but you showed a positive remainder (so I suspect the divisor is not exactly the value shown) – M.M May 08 '19 at 02:15
  • NB. I didn't mark as dupe (the other q is not really a dupe) I vtc as not including MCVE. You could reopen by including MCVE and the results copy pasted. – M.M May 08 '19 at 11:36

1 Answers1

2

The definition of remquo includes (C11 7.12.10.3/1, referred to by C++17):

The remquo functions compute the same remainder as the remainder functions. In the object pointed to by quo they store a value whose sign is the sign of x/y and whose magnitude is congruent modulo 2n to the magnitude of the integral quotient of x/y, where n is an implementation-defined integer greater than or equal to 3.

You seem to be incorrectly expecting quo to hold the actual value of the integral quotient.


I am trying to find the number of times one double divides into another in C++

There are various ways to do this, e.g. std::trunc(x/y) or std::lrint(x/y). It might also be helpful to read the documentation for std::remainder and std::fmod. You can look all of these functions up on cppreference.com or in ISO C and C++ drafts.

M.M
  • 138,810
  • 21
  • 208
  • 365