General
Follow


Umar Khan

Which one makes sense?

Umar Khan on 4 Feb 2024
Latest activity Edit by Stephen23 on 22 Feb 2024

I am confused, is the matlab answer better or Julia’s?

Stephen23
Stephen23 on 20 Feb 2024 (Edited on 20 Feb 2024)
What exactly are you asking about?
Both Julia and MATLAB define 0/0 to be NaN. Python natively cannot handle 0/0 and throws an error, as your screenshot shows.
According to the official Julia documentation here:
MIN() uses ISLESS() to determine which value is less than the other. Interestingly ISLESS(0,NaN) returns TRUE, so either Julia's MIN() is buggy or Julia's documentation is incorrect. Even more confusingly, the ISLESS() documentation states "Values that are normally unordered, such as NaN, are ordered after regular values", which seems to directly contradict what MIN() does.
Julia's special logic leads to mathematical confusion very quickly. For example, given the relationship A<B<C and the ISLESS() definition above then I would expect
max(A,B)==min(B,C)
to be TRUE if A and B are finite values and C=NaN. But it isn't.
Note that MATLAB treats NaN as being not equal to, not less than, and not greater than any other values (including other NaNs), which is exactly as required by IEEE 754:
Julia does not follow this specification. Why should NaN be greater than the finite values... why not less than the finite values? That seems very arbitrary. In a sense, Julia has redefined Not-A-Number into A-Number-Greater-Than-What-You-Are-Thinking-Of: select any finite number and Julia will happily tell you that your number ISLESS() than NaN.... but confusingly will then return NaN as being both the MIN() and the MAX() of them. Odd.
Stephen23
Stephen23 on 20 Feb 2024 (Edited on 20 Feb 2024)
PS: note that with MATLAB you can get NaN as the output by simply by setting the MISSINGFLAG:
min(0,NaN,'includenan')
Mukhtar Ullah
Mukhtar Ullah on 21 Feb 2024
Even though 1/0 and 0/0 are both undefined, they are different mathematically. 1/0 is infinite (unbounded and hence definitely larger than any real number) whereas 0/0 is arbitrary (indeterminate and hence cannot be compared with any real number). Julia distinguishes the two cases by returning Inf for 1/0 and NaN for 0/0.
Stephen23
Stephen23 on 21 Feb 2024 (Edited on 21 Feb 2024)
"Julia distinguishes the two cases by returning Inf for 1/0 and NaN for 0/0."
So does MATLAB.
You can easily check this yourself by performing this very simple operation in MATLAB or by reading the MATLAB documentation:
It is unclear why you have brought up 1/0 (not mentioned anywhere by the OP nor by me) which is very unlikely to result in any confusion in the context of MIN() and MAX() that the OP asked about.
Mukhtar Ullah
Mukhtar Ullah on 22 Feb 2024 (Edited on 22 Feb 2024)
Please ignore that line as I forgot to remove it from another post I wrote on Facebook on this topic. My follow up reasoning (after recalling that Inf is definitely larger than any real number and NaN may be smaller, larger, or equal to 1 because indeterminate is not necessarily unbounded):
In MATALB, both min(NaN,1) and min(Inf,1) return 1 whereas, in Julia, only min(Inf,1) returns a definite answer 1. Stated otherwise, when it comes to comparison with 1, MATLAB does not distinguish between NaN and Inf whereas Julia does.
Stephen23
Stephen23 on 22 Feb 2024 (Edited on 22 Feb 2024)
"Stated otherwise, when it comes to comparison with 1, MATLAB does not distinguish between NaN and Inf whereas Julia does.
Making that claim based solely on the output of MIN() is like claiming that MATLAB does not distinguish between 0 and 180 based solely on the output of SIND(). A function output can be the same for many different reasons, and so far you have not sufficiently clarified the actual reason. The real reasons why both MIN(1,Inf) and MIN(1,NaN) both return 1 are actually different and have nothing to do with your claim:
  • 1 is less than Inf
  • NaN is not returned by default (with a few exceptions)
Those are the real reasons.
Lets now test your claim that "when it comes to comparison with 1, MATLAB does not distinguish between NaN and Inf":
>> Inf==NaN % Inf is distinguished from NaN
ans =
logical
0
>> Inf>1
ans =
logical
1
>> NaN>1
ans =
logical
0
We can see Inf is distinguished from NaN.
Lets see if your claim is supported by MIN():
>> min(Inf,NaN) % 1
ans =
Inf
>> min(NaN,Inf) % 2
ans =
Inf
If, as you state "MATLAB does not distinguish between NaN and Inf" why does 1. return the value of the 1st input, whereas 2. returns the value of the 2nd input? The only way that could happen is if MATLAB does indeed "distinguish between NaN and Inf".
Both MATLAB and Julia distinguish between NaN and Inf. The difference with MIN() and MAX() is that:
  • Julia returns NaN
  • MATLAB does not return NaN by default (with some exceptions and options to change this behavior).
"NaN may be smaller, larger, or equal to 1 ..."
MATLAB consistently follows the IEEE 754 standard that by definition any comparison with NaN is unordered. In other words, by definition NaN is not less than, not greater than, nor equal to anything (not even itself). In MATLAB both MIN() and MAX() by default do not return NaN (with a few exceptions) by default, I guess based on that definition. In any case, MATLAB correctly implements comparisons with NaN:
In contrast Julia gets itself into something of a mess (again): apparently everything should have a meaningful comparison using ISLESS() (in direct conflict with IEEE 754), which it then promptly ignores within MIN() and MAX() (and possibly other functions).
It is still unclear to me why you have introduced the topic of Inf which adds no clarity to the topic the OP asked about.

See Also

Tags

No tags entered yet.