11

In python 3.x, are there some a, b such that max(a, b) != max(b, a)?

a and b should be of a built-in python type and do not contain any conversion.

The type of a and b can be: num, float, bool, list, tuple ...

Example:

a = 1 # Correct
a = [1, 2] # Correct
a = float('nan') # Wrong
6
  • 1
    your question is not clear Commented Oct 20, 2019 at 9:36
  • I don't think there is any... max is a built-in function that takes an iterable object as input. So, a,b is the same as b,a hence max(a,b) must equal max(b,a) Commented Oct 20, 2019 at 9:36
  • 1
    This is a good question but you should edit it and explain it more Commented Oct 20, 2019 at 9:37
  • 2
    Your edit only makes sense in the context of your comment below the most up voted answer. There and here, you fail to argue that float('nan') is not a built-in type. I would assume "built-in type" would mean anything you can create without having to create a new class. Commented Oct 20, 2019 at 19:52
  • 2
    If you want to exclude float('nan'), then why? To be clear, you're allowed to ask a question in which you exclude something like float('nan') if you want; that's not the problem. The problem's just that no one's sure what you're trying to ask. Commented Oct 20, 2019 at 20:06

2 Answers 2

17

This satisfies all criteria:

>>> max(1.0, float("nan"))
1.0

>>> max(float("nan"), 1.0)
nan

This is because any comparisons other than != or is with float("nan") always return false.

More generally, we can have max(a, b) != max(b, a) when the type(s) of a, b do not provide a total order. As Daniel Mesejo points out, sets have this property as well. For sets < means 'strict subset', but with {1, 2} and {3, 4} neither is a strict subset of the other, so:

>>> max({1, 2}, {3, 4})
{1, 2}

>>> max({3, 4}, {1, 2})
{3, 4}

This one technically doesn't have max(a, b) != max(b, a) because 1 == True, but there's still an observable difference:

>>> max(True, 1)
True

>>> max(1, True)
1
Sign up to request clarification or add additional context in comments.

9 Comments

WOW... why is that??
@stackFF4 if float("nan") is not built-in, than what is it? It is certainly built-in.
@stackFF4: float('nan') is a float. It says float right there, so I'm not sure what else you expected it to be. You can check it with type if you want.
@Anwarvic: Interestingly, it's not a python thing, it's actually how floats are implemented in hardware. NaN compares false with everything in almost every programming language.
@Nat: But nobody wants exceptions most of the time. This max behaviour is an artifact of defining it differently from C fmax (which guarantees NaN propagation), instead as something like a<b ? b : a which always produces a when the comparison is false. NaN is "unordered" wrt. any other number, so a < NaN is always false, and so is NaN < b
|
5

According to the documentation, using max on sets produces undefined results:

Accordingly, sets are not appropriate arguments for functions which depend on total ordering (for example, min(), max(), and sorted() produce undefined results given a list of sets as inputs).

Here is an example,

a = {1, 2}
b = {2, 3}
print(max(b, a) != max(a, b))

Output

True

1 Comment

It might be worth noting here that "undefined results" are probably saner in Python than in some other languages. No nasal demons are likely, and it probably won't even raise an exception. It just returns a mostly arbitrary value (for min and max) or ordering (for sorted).