Problem when testing long decimal numbers
2 ビュー (過去 30 日間)
古いコメントを表示
I generated a set of numbers (integers and long decimals) and I tested them are they integers or not.
e.g., when I tested the number 2.00000000000000000000000000095861, I write
>> mod(2.00000000000000000000000000095861,1)
ans =
0
i.e., the number 2.00000000000000000000000000095861 is integer. Althought, it is decimal.
Note: The same problem occurs when using Floor function for testing the long decimal number is it integer or not.
What is the problem? I appreciate your help.
0 件のコメント
採用された回答
John D'Errico
2021 年 7 月 4 日
編集済み: John D'Errico
2021 年 7 月 4 日
The problem is merely your understanding of floating point numbers and the methods used to store them.
MATLAB uses only 52 binary bits of precision to store a floating point number.
What would it require to store that number?
log2(str2sym('2.00000000000000000000000000095861') - 2)
So 100 binary bits would just barely suffice to approximate that number, but not that closely. And as I said, MATLAB stores a floating point number as a DOUBLE, thus only 52 binary bits. There is no 16 byte floating point class, and even that would probably be barely sufficient. And of course, if you had such a long precision form available, we would constantly see questions just like yours...
"Why won't MATLAB exactly represent this number?
2.00000000000000000000000000000000000000000000000000000000000000095861
The answer is to understand how a number is stored and how to work with that. This sometimes requires a good grounding in numerical methods.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で NaNs についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!