'How does "-float" works?

I have just seen a code in my homework. It says result=-float("inf"). I don't really understand what -float means.



Solution 1:[1]

It's not -float, it's -float(...).

float(x) makes a floating-point number from x. Here, float("inf") represents positive infinity, so the - negates it and you get negative infinity.

Another way to get negative infinity is float("-inf").

For reference, see operator precedence in the docs. A call is more tightly binding than unary negation.

Solution 2:[2]

float is a built-in function in Python that returns a floating point number constructed from a number or string.

float("inf") returns the floating-point representation of positive infinity, so -float("inf") is how you can get negative infinity.

There are a number of Built-in Functions in Python that it pays to be familiar with.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2