필터 지우기
필터 지우기

Hello , I am using Matlab 7.10.0(R2010a). I need to use mod function for int64 datatype. But it doesn't wok . Can you help me ?

조회 수: 2 (최근 30일)
mod(ans(1),48) ??? Undefined function or method 'mod' for input arguments of type 'int64'
ans(1) is a first element of array ans of type int64.

답변 (1개)

Guillaume
Guillaume 2015년 4월 17일
편집: Guillaume 2015년 4월 17일
Newer versions of matlab have mod implemented for int64, so maybe it is time to upgrade.
Failing that, the only way is to defer the operation to java BigInteger:
a = intmax('int64'); %for example
%use static method 'ValueOf' of bigInteger to convert int64 (long) into a BigInteger:
bi = javaMethod('valueOf', 'java.math.BigInteger', a)
%modulo operation needs another BigInteger
m = bi.mod(javaMethod('valueOf', 'java.math.BigInteger', 48));
%unfortunately matlab converts the long value return by the 'longValue' method into a double
%so the following may result in loss of precision
result = int64(m.longValue)
%to guarantee no precision loss, go throught the byte array representation:
barr = m.toByteArray;
[~, ~, endianness] = computer; %note that toByteArray always return bytes in big endian
if endianness == 'L' %so if machine is little endian
barr = flipud(barr); %flip the byte
end
if numel(barr) < 8 %toByteArray only return as many bytes as necessary
barr(8, 1) = 0; %so expand to the 8 required for int64
end
result = typecast(int8(barr), 'int64')
As per the comment above, the simplest way to get an int64 (long) value out of a BigInteger is through the longValue method. Unfortunately, matlab converts the long that it returns into a double, resulting in a loss of precision for large integers. The workaround is to get the byte representation and type cast the byte array back to int64.
Edit: Actually if you're on Windows, a much simpler way is to go through .Net, the Math.DivRem method:
System.Math.DivRem(intmax('int64'), 48)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by