Is there a way to solve wrong results when using (mod) operation?

Hi all,
I am working with many (mod) operations to create my own functions in matlab. However, I face a problem of wrong results that are appears. For example,
***** Manuale calculations ***********
temp = (16667 * 16667 * 16667 ) = 4629907412963
temp = = 2533 * 4629907412963 = 11727555477035279
temp = temp % 25000
temp = 10279
***** Matlab calculations ***********
temp = (16667 * 16667 * 16667 )
temp = 2533 * 4.6299e+12
temp = 1.1728e+16
temp = mod (1.1728e+16 , 25000)
temp = 10280
******************************
As you can see, there are two different results even it is just one value! Also I have another
example with a huge difference between the two results.
Is there a convenient way to solve this issue?
Thanks in advance!

9 Comments

Hi Abdulatif,
What happens if you cast the numbers (16667,2533) as 64-bit unsigned integers rather than using the default data type of double?
Or perform the mod(*,25000) on every product rather than waiting until the end against the incredibly large number?
Geoff
Performing Mod operation on every product does not solve the issue.
What is your largest possible product?
For example, I can't get this result correctly:
temp = (16667 * 16667 * 16667 *16667) = 77166666851854321
temp = temp * 15472 = 1193922669531890054512 --> I can't get this result
correctly.
even I have to deal with numbers bigger than this one. So, I need general
solution.
You can't get the above result because the product exceeds the maximum for a 64-bit unsigned integer (assuming that you have casted the 16667 and 15472 as uint64).
Are you still performing the mod operation with m=25000? If so, while the below is tedious and inefficient, if you don't have access to the symbolic toolbox then it may still do what you need as long as no single product exceeds 18446744073709551615:
x = uint64(16667);
y = uint64(15472);
m = uint64(25000);
z = mod(mod(mod(mod(x*x,m)*x,m)*x,m)*y,m);
Which is equivalent to 16667*16667*16667*16667*15472 mod 25000.
Thanks : )
Can you explain more about the symbolic toolbox? I am not sure if I have it or not. I am just a beginner in Matlab, and how can I use it to solve this issue.
Thank you
Please see Walter's explanation below. If you are unsure whether you have the symbolic toolbox or not (I don't!) just type ver at the command line and it will list the MATLAB version and all toolbox versions (of those that you have). If you see the symbolic toolbox then you are good to go!
It works with the symbolic toolbox.
Thank you so much : )
Both answers helps me
Thank you Geoff Hayes & Walter Roberson

Sign in to comment.

 Accepted Answer

You could work with int64 or uint64 datatype. That would postpone the problem.
To solve the problem (for most practical purposes) you would need to switch to a high precision arithmetic system, such as using the symbolic toolbox or using John D'Errico's File Exchange contribution "VPI"

4 Comments

Could you give me a hint about the symbolic toolbox? Because it seems that I have to work with a huge numbers!
I need to learn the simplest way to solve this issue because I have time limit : )
Thank you
Are you working with a Student Version (Bundled) license? If so then you have the Symbolic Toolbox.
temp = sym('16667')^3;
temp = sym('2533') * temp;
temp = mod(temp, sym('25000'));
In the case where the individual numbers are integers less than 2^53, you can omit the quotation marks:
temp = sym(16667)^3;
temp = sym(2533) * temp;
temp = mod(temp, sym(25000));
You can even abbreviate this as
temp = sym(16667)^3;
temp = 2553 * temp;
temp = mod(temp, 25000);
because when a symbolic number is one operand, the other operand will be converted to symbolic automatically.
It works with the symbolic toolbox.
Thanks for your comments and explanations : )
Thank you so much
Both answers helps me
Thank you Geoff Hayes & Walter Roberson

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!