does matlab have a problem with modular integer arithmetic?

1 回表示 (過去 30 日間)
alexander sharp
alexander sharp 2017 年 3 月 24 日
コメント済み: Walter Roberson 2017 年 3 月 24 日
>> x = -78907292 * 1941317253; >> y = 2^32 +1
y =
4.29496729700000e+009
>> x
x =
-153.184087347109e+015
>> mod(x,y)
ans =
1.51932828600000e+009
correct answer:
= 1519328274 (python) and others...
  1 件のコメント
KSSV
KSSV 2017 年 3 月 24 日
With format long g as option mod(x,y) is 1519328286

サインインしてコメントする。

回答 (3 件)

Walter Roberson
Walter Roberson 2017 年 3 月 24 日
Try
X = int64(-78907292) * int64(1941317253)
Remember that the default data type is double not one of the integer data classes.

Roger Stafford
Roger Stafford 2017 年 3 月 24 日
編集済み: Roger Stafford 2017 年 3 月 24 日
As has so often been pointed out in this forum, matlab’s “double” in everyone’s computers possesses a significand (mantissa) consisting of 53 binary digits. Consequently it is incapable of representing the above product -78907292*1941317253 exactly. For that reason the errors it must necessarily make will certainly be manifest using the mod function as given here. Have a heart! Or better still use the symbolic forms of numbers for such calculations.

alexander sharp
alexander sharp 2017 年 3 月 24 日
an integer is an integer is an integer - by any other name. it is wrong
  1 件のコメント
Walter Roberson
Walter Roberson 2017 年 3 月 24 日
MATLAB frequently allows people to use abbreviated forms. In MATLAB your line
x = -78907292 * 1941317253;
is considered to be an abbreviated form of
x = times(-78907292.0, 1941317253.0);
An integer might, as you say, be an integer, but you did not enter any integers.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by