A bug? input: 1/0 output: -Inf
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
if x=0, expected output of 1/x is Inf, but matlab output -Inf
load('x.mat')
x==0
1/0
1/x
2 Kommentare
Image Analyst
am 13 Feb. 2024
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.
Akzeptierte Antwort
Stephen23
am 13 Feb. 2024
Bearbeitet: Stephen23
am 13 Feb. 2024
"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
1/x
num2hex(x) % yep, definitely negative zero
x = +0
1/x
num2hex(x) % yep, that is positive zero
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
x = 0+x;
1/x
4 Kommentare
Stephen23
am 13 Feb. 2024
"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.
Weitere Antworten (1)
Matt J
am 13 Feb. 2024
Bearbeitet: Matt J
am 13 Feb. 2024
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 Kommentare
Siehe auch
Kategorien
Mehr zu Functions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!