# Why does rem(a, b) function give different answers for input values' class 'double' and 'uint64'?

3 views (last 30 days)
Omkar Chavan on 28 Oct 2020
Edited: Omkar Chavan on 29 Oct 2020
I define two values as double,
a1 = 9419588158802421600; b1 = 44;
I am trying to use built-in remainder function,
v1 = rem (a1,b1)
output>> v1 = 28
Now, I define the same values but as uint64
a2 = uint64(9419588158802421600); b2 = uint64(44);
and use remainder function
v2 = rem (a2,b2)
output>> v2 = (uint64) 0
Why two different answers are obtained for the same input values?

Steven Lord on 28 Oct 2020
Let's look at the value stored in a1 converted to a uint64.
>> a1 = 9419588158802421600; b1 = 44;
>> a2 = uint64(9419588158802421600); b2 = uint64(44);
>> [uint64(a1); a2; intmax('uint64')]
ans =
3×1 uint64 column vector
9419588158802421760
9419588158802421600
18446744073709551615
Not all integer values on the magnitude of 9419588158802421600 can be exactly represented by a double. For example, the next representable number after a1 is:
>> uint64(a1 + eps(a1))
ans =
uint64
9419588158802423808
and the one immediately before it:
>> uint64(a1 - eps(a1)/2)
ans =
uint64
9419588158802419712
You can see this by looking at the hex representation of the numbers:
>> [a1 - eps(a1)/2; a1; a1 + eps(a1)]
ans =
43e0572330befc9c
43e0572330befc9d
43e0572330befc9e
They differ only in the last hex digit. So the computations in double and in uint64 are operating on different data. When you try to compute on numbers that are close to or greater than flintmax be very careful.

### Categories

Find more on Logical in Help Center and File Exchange

R2020a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!