MATLAB Answers

the cyclist

Output of mod() for large input

Asked by the cyclist
on 23 Apr 2012

In the Stanford online course in probabilistic graphical models, they use their own linear congruential random number generator. The results from that generator differ between R2012a and earlier versions of MATLAB, and I boiled it down to a difference in the calculation of mod() for a very large number:


Since the first argument is 17 digits, and therefore not accurately stored as an integer, it is no surprise to me that mod() might struggle; but I am wondering if anyone can give me some more specific insight as to why the behavior may have changed from version R2011b to R2012a.

  1 Comment

Perhaps R2012a automatically used uint64?



1 Answer

Answer by Richard Brown
on 23 Apr 2012
 Accepted answer

@Walter Roberson is correct, in R2012a MATLAB automatically casts the number into a uint64. For this piece of code

[mod(25754210418041856,179424673), mod(uint64(25754210418041856),179424673)]

In R2011b I get this:

            149629568            149629566

In R2012a I get this:

            149629566            149629566

  1 Comment

Also, from the release notes for R2012a:

"The following functions show improved performance:
Arithmetic and similar basic math functions for double, single, and integer data types."

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!