필터 지우기
필터 지우기

Does matlab supports rational number types?

조회 수: 8 (최근 30일)
Anver Hisham
Anver Hisham 2016년 9월 29일
편집: Stephen23 2024년 4월 3일
I see Matlab stores numbers in floating points [1], which causes round of errors!
For eg:
The above round off error is resulted, because 10^-5 is not a valid 64 bit floating point number in matlab.
However in *Mathematica*, numbers can be stored in rational format [2] (ie integer/integer format), which avoids any round off error for the same computations.
So my question is, will it be possible to avoid such round of errors in matlab?
[2]: https://reference.wolfram.com/language/tutorial/LinearAlgebraMatrixTypes.html
  댓글 수: 2
Adam
Adam 2016년 9월 29일
I think you need the Symbolic Maths toolbox to have handling for this.
Stephen23
Stephen23 2024년 4월 3일
편집: Stephen23 2024년 4월 3일
All binary floating point numbers are rational numbers.
Also note that the proposed solution just shifts the problem: how should those two integers be stored? Presumably they would be finite integers stored on your computer... which means that there will always be rational numbers that cannot be represented (unless you happen to have infinite memory installed on your laptop).
Using (finite) rational numbers (in the sense the OP proposed) does not completely avoid errors: there are many more irrational numbers than rational numbers, some of which are commonly used. For example, there is no way to exactly represent e.g. sqrt(2) or pi or e as ratios of finite integers. Any attempt to store those values numerically will always include some error.

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2016년 9월 29일
MATLAB uses floating point numbers by default. This makes it more efficient to do things like work well on large scale linear algebra problems, but it introduces problems like this. There is no fractional integer class in MATLAB. (Well, there was one posted on the FEX that I recall, that worked with my own vpi.)
Yes, you can use the symbolic toolbox to do the kinds of things you want to do. But you then need to recognize that those computations will be quite a bit slower. You trade off speed for ultimate precision. And given the number of people who are never satisfied with the speed of their computers on any given problem, speed is important.
In the end, the real solution is to learn to use floating point arithmetic. That is a skill and sometimes an art. It is the gist of scores of numerical methods courses, teaching students how to do computations without falling under the wheels of a limited number of digits in their computations.
  댓글 수: 4
Walter Roberson
Walter Roberson 2023년 3월 8일
quite true, rationals can be useful. Unfortunately, there is no hardware support for rationals.
Omar Lakkis
Omar Lakkis 2024년 4월 3일
편집: Omar Lakkis 2024년 4월 3일
The power 5 is a distraction, in fact the example works with squares in the following form
0.01-(0.1)^2
and the reason is that the rational number 1/10 has the infinite (periodic) binary expansion: 0.000110011... This means that using 0.1 in a calculation, except in the simplest situation, will necessarily introduce rounding errors.
A commong misconception is that the smaller the numbers the harder are they to represent as floats, but this is not true. It is instructive to compare the above with the following (luckily exact) operation
0.00390625-(0.0625)^2

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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