n E Z => sin(n*pi) = -1 ?
조회 수: 3 (최근 30일)
이전 댓글 표시
the problem is simple at the first glance. if n is an integer, than sin(n*pi) should be "0". But it gives "-0,999".
isInt = @(n) sin(n*pi);
isInt(100000000000000000000000000000);
I get that the number should be small since the computation doesnt go beyond 10^17, but if I give 5, the result is "6*10^-16". Still, is not "0". How can I overcome this problem? is there a method? I need to solve the problem of "integer". It should be "exact integer". I couldn't find a way to get over it. Do you have any idea?
댓글 수: 0
답변 (4개)
Steven Lord
2022년 7월 21일
n = 100000000000000000000000000000;
sinpi(n)
댓글 수: 6
James Tursa
2022년 7월 21일
편집: James Tursa
2022년 7월 21일
"Basicly I need a way to calculate formulas beyond the limits of IEEE. which I suppose it is not possible. is it?"
Yes, it is. Use the Symbolic Toolbox as others have already suggested.
That being said, I suspect that your "algorithm", which you haven't discussed or shown us, may be producing garbage if you need 30 or more decimal digits of precision to calculate results. What else is going into this calculation and are you carrying enough precision in every variable to make the result meaningful? Can you post this code?
Stephen23
2022년 7월 21일
n = sym(100000000000000000000000000000)
sin(n*pi)
댓글 수: 1
James Tursa
2022년 7월 21일
편집: James Tursa
2022년 7월 21일
Note that sym( ) here is doing the conversion based on its calculated "intent" of the user, since the actual number that gets passed to sym( ) isn't 100000000000000000000000000000 but something "nearby" as close as IEEE double precision can represent:
n = 100000000000000000000000000000;
fprintf('%30.0f\n',n);
Also, there is another subtle conversion going on in the background. The double precision pi value is converted to its symbolic version based again on its calculated "intent" of the user. That is, here is the actual double precision pi value converted to decimal:
fprintf('%60.55f\n',pi);
But The product n*pi results in a conversion of this to the "exact" symbolic pi before sin( ) is called:
n = sym(n)
n*pi
Personally, I prefer to have these silent conversions made explicit in the code so I don't inadvertently get bit downstream. E.g., I typically would calculate this instead up front and use it downstream so there is no possible ambiguity:
sympi = sym('pi')
KSSV
2022년 7월 21일
n = vpa(100000000000000000000000000000) ;
vpa(sin(n*pi))
댓글 수: 1
John D'Errico
2022년 7월 21일
It is more subtle than you think.
n = vpa(100000000000000000000000000000)
vpa(sin(n*pi))
vpa(sin(n*pi),100)
There is a difference.
David Hill
2022년 7월 21일
Use symbolics.
function [out] = isInt(n)
n=sym(n);
out=double(sin(n*pi));
end
Call function
o=isInt('10000000000000000000000000000000000000000000000000000000000000000000');
참고 항목
카테고리
Help Center 및 File Exchange에서 Numbers and Precision에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!