if x=0, expected output of 1/x is Inf, but matlab output -Inf
load('x.mat')
x==0
Unrecognized function or variable 'x'.
1/0
1/x

댓글 수: 2

Matt J
Matt J 2024년 2월 13일
Your attached file doesn't contain a variable called x.
Image Analyst
Image Analyst 2024년 2월 13일
x==0 is a comparison, not an assignment. Since you didn't assign x yet, you can't see if it's equal to zero, hence the error. I think you meant x=0, not x==0.

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

 채택된 답변

Stephen23
Stephen23 2024년 2월 13일
편집: Stephen23 2024년 2월 13일

3 개 추천

"A bug? input: 1/0 output: -Inf"
Not a bug. Because your value x is actually negative zero not positive zero:
Lets try it now:
x = -0
x = 0
1/x
ans = -Inf
num2hex(x) % yep, definitely negative zero
ans = '8000000000000000'
x = +0
x = 0
1/x
ans = Inf
num2hex(x) % yep, that is positive zero
ans = '0000000000000000'
So far everything is working exactly as expected. Note that a few simple arithmetic operations will convert negative zero to positive zero without affecting any other values, e.g. adding zero:
x = -0;
1/x
ans = -Inf
x = 0+x;
1/x
ans = Inf

댓글 수: 4

Lets take a look at your uploaded data:
S = load('x.mat');
x = S.deltaX
x = 0
1/x
ans = -Inf
num2hex(x) % yep, definitely negative zero
ans = '8000000000000000'
qilin
qilin 2024년 2월 13일
It is helpful, maybe the next step is to figure out why my ''deltaX'' is negative zero, thank you.
Dyuman Joshi
Dyuman Joshi 2024년 2월 13일
How did you obtain that value?
Stephen23
Stephen23 2024년 2월 13일
"maybe the next step is to figure out why my ''deltaX'' is negative zero"
Most likely it makes zero(!) difference: note that by definition negative and positive zeros have the same value, so if you are happy with your array being zero-value then simply multiply your array by one (or add zero) and move on to more important tasks.

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

추가 답변 (1개)

Matt J
Matt J 2024년 2월 13일
편집: Matt J 2024년 2월 13일

0 개 추천

Theorem: 1/0 = Inf if and only if 1/0 is also -Inf.
Proof: Assume first that 1/0=Inf and let us deduce the implication that 1/0=-Inf. By multiplying the numerator and denominator by -1, we obtain,
1/0 = (-1)/(-0) = (-1)/0 = -(1/0) = -Inf
proving the implication Now assume that 1/0=-Inf. Proceeding similarly,
1/0 = (-1)/(-0) = (-1)/0 = -(1/0) = -(-Inf) = +Inf
and the reverse implication is also proved. Q.E.D.

댓글 수: 2

qilin
qilin 2024년 2월 13일
Thank you for your answer, it's interesting, but: .
In other words, when we say , actually it's , here , so the second
''='' of ''1/0 = (-1)/(-0) =...'' should be modified.
Matt J
Matt J 2024년 2월 13일
You took it way too seriously...

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

카테고리

도움말 센터File Exchange에서 Time Series Events에 대해 자세히 알아보기

제품

릴리스

R2023b

태그

질문:

2024년 2월 13일

댓글:

2024년 2월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by