0

I am porting some of my Python code to Matlab and have encountered an issue when doing mod calculations in Matlab, resulting in NaN being returned instead of the correct value like Python.

In Python the code is p = pow(8687205886,5788687615,8369428283) the same in Matlab

a = 8687205886^5788687615 
b = 8369428283
m = mod(a,b)

I have no acess to any toolboxes other than the default ones so using the Symbolic Maths Toolbox or alternatives are not an option.

How would such a calcuation be done in Matlab ?

Thank you

  • 1
    The `mod` call is not the problem, `a` is already `Inf` there. You could look for tools on the File Exchange that do large integer computations, I expect you’d find some. – Cris Luengo Jan 12 '20 at 20:03
  • 1
    Here: https://www.mathworks.com/matlabcentral/fileexchange/22725-variable-precision-integer-arithmetic — The author is well respected, I would expect this tool to be correct and well written. – Cris Luengo Jan 12 '20 at 20:06
  • Do you think casting to int 64 would work – justanothertechdude Jan 12 '20 at 23:41
  • 2
    `8687205886^5788687615` is an extremely large number that won’t fit in 64 bits. No idea how many bits you’d need, maybe thousands. You can’t do this with built-in number representations. You need to use a large integer algorithm. You can write it yourself or you can use one that is already written. Don’t write it yourself unless you want to become intimately familiar with this type of algorithm. Reusing existing code is usually the best way. – Cris Luengo Jan 12 '20 at 23:50

1 Answers1

2

Lacking the required toolbox for higher precision numbers, using python in MATLAB might be an option:

p = py.pow(int64(8687205886),int64(5788687615),int64(8369428283))

p = 

  Python long with properties:

    denominator: [1×1 py.long]
           imag: [1×1 py.long]
      numerator: [1×1 py.long]
           real: [1×1 py.long]

    539591274
Daniel
  • 36,610
  • 3
  • 36
  • 69