error in large integers

I am currently working with fractions involving 10digit integers.
for example, typing 1168347147/863334886 into MATLAB produces the answer 4045/2989.
But I want to store the fractions exactly. How do I go about doing this?
Many thanks.

답변 (2개)

Aldin
Aldin 2012년 3월 21일

0 개 추천

Hi,
use vpa functon
for example:
vpa('4045/2989',100) ---> for hundred digits

댓글 수: 5

Walter Roberson
Walter Roberson 2012년 3월 22일
No, you cannot get "fractions exactly" by using VPA. Try vpa('1/3') for example: the representation you get will not be of 1/3 because 1/3 is an infinitely repeating decimal.
Aldin
Aldin 2012년 3월 22일
I have one even better solution:
>>1168347147/863334886
ans =
1.3533
USE "rats" function:
>>rats(ans)
the result will be: 4045/2989
Eigo Takkeda
Eigo Takkeda 2012년 3월 22일
I wish to use the value 1168347147/863334886 and store it exactly for further calculations, but MATLAB automatically rounds it to 4045/2989, and therefore looking for a way to avoid this rounding.
Aldin
Aldin 2012년 3월 22일
I think is more better to use smaller number like 4045/2989 ???
Aldin
Aldin 2012년 3월 22일
Do you know:
1168347147/863334886 = 4045/2989

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

Walter Roberson
Walter Roberson 2012년 3월 22일

0 개 추천

If you have the symbolic math toolbox, then
sym('1168347147/863334886')
Note that the result will be a symbolic number, not a double precision number.

댓글 수: 3

Eigo Takkeda
Eigo Takkeda 2012년 3월 22일
could I then use this for further calculations?
Walter Roberson
Walter Roberson 2012년 3월 22일
Yes, if you do have the symbolic toolbox then you would be able to use symbolic numbers in further calculations; the results would be symbolic numbers.
There might be places along the way where you needed to convert the symbolic numbers to double precision using double(), as there are MATLAB routines that cannot work with symbolic numbers.
Aldin
Aldin 2012년 3월 22일
Hello sir Walter,
Can you please explain:
>> sym('1168347147/863334886')
ans =
1168347147/863334886
>> ans * 1,4
ans =
8178430029/4316674430
--------------------------
But if i check last result with 'vpa' function i got this:
>> vpa('863334886*1,4)
ans = 1208668840.4

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

카테고리

질문:

2012년 3월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by