12 Replies - 466 Views - Last Post: 01 March 2019 - 08:35 AM Rate Topic: -----

#1 bobsmith76   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 358
  • Joined: 14-February 17

float("INF") == "inf" - why?

Posted 18 January 2019 - 05:41 PM

Is this a bug? In python:

float("INF") == "inf"



If it's not a bug then why?
Is This A Good Question/Topic? 0
  • +

Replies To: float("INF") == "inf" - why?

#2 bobsmith76   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 358
  • Joined: 14-February 17

Re: float("INF") == "inf" - why?

Posted 18 January 2019 - 05:48 PM

Actually, it looks like the problem is restricted to my computer. I went to a random website which executes Python code and the bug did not show up there.

https://ibb.co/s67PxbH

https://ibb.co/4gh8gPH
Was This Post Helpful? 0
  • +
  • -

#3 bobsmith76   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 358
  • Joined: 14-February 17

Re: float("INF") == "inf" - why?

Posted 18 January 2019 - 06:34 PM

On second thought, I found an online editor that does replicate my bug

https://repl.it/repl...eklyDifferences
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 4,369
  • Joined: 21-June 11

Re: float("INF") == "inf" - why?

Posted 19 January 2019 - 12:11 AM

Does it? If I run print(float("INF") == "inf"), it outputs False just like on the other site.

Are you sure that this comparison actually returned True anywhere you tried it? Or did you maybe get mixed up by the fact that one can't tell the difference between strings and floats from the output of print?

To be clear, I'd expect the following code to produce the output written in the comments:

import math

print("inf") # inf
print(math.inf) # inf
print(float("inf")) # inf

print("inf" == math.inf) # False
print("inf" == float("inf")) # False
print(math.inf == float("inf")) # True

print(type(math.inf)) # <class 'float'>
print(type(float("inf"))) # <class 'float'>
print(type("inf")) # <class 'str'>



If you get different output than that anywhere, I'd be surprised.

This post has been edited by sepp2k: 19 January 2019 - 12:14 AM

Was This Post Helpful? 0
  • +
  • -

#5 bobsmith76   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 358
  • Joined: 14-February 17

Re: float("INF") == "inf" - why?

Posted 19 January 2019 - 02:48 AM

No, the bug is definitely there. I also tried out a 2.7 version here

https://repl.it/repl...windCreepyModel

and got the same result. It only happens with the specific string "INF"

I also tried type(float("INF")) and it came out as a float.

Also, you have to capitalize INF, but I also tried float("inf") and that comes out at as 'inf' as well. Finally, type("INF") outputs str

This post has been edited by bobsmith76: 19 January 2019 - 02:52 AM

Was This Post Helpful? 0
  • +
  • -

#6 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,843
  • Joined: 13-June 14

Re: float("INF") == "inf" - why?

Posted 19 January 2019 - 02:53 AM

You're printing the string representation of that float, which is inf. Testing whether the value is actually equal to inf will, as you've been told above, return False:

Python 2.7.15 (default, May  4 2018, 14:13:26) 
[GCC 5.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> float("inf")
inf
>>> float("inf") == "inf"
False
>>> float("INF") == "inf"
False
>>> float("INF") == "INF"
False
>>> 




Of course type(float("INF")) returns tells you it's a float, because it is.

This post has been edited by ndc85430: 19 January 2019 - 02:53 AM

Was This Post Helpful? 2
  • +
  • -

#7 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 4,369
  • Joined: 21-June 11

Re: float("INF") == "inf" - why?

Posted 19 January 2019 - 03:01 AM

View Postbobsmith76, on 19 January 2019 - 10:48 AM, said:

No, the bug is definitely there.


Are you saying that you ran the code I posted and the output was different than what I wrote in the comments? To be honest, I don't believe you.

Quote

I also tried out a 2.7 version here

https://repl.it/repl...windCreepyModel


Again that code is not actually checking whether the result if equal to "inf". Look here https://repl.it/repl...plicationserver. As you can clearly see, the result is False, not True.
Was This Post Helpful? 1
  • +
  • -

#8 bobsmith76   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 358
  • Joined: 14-February 17

Re: float("INF") == "inf" - why?

Posted 19 January 2019 - 03:16 AM

Ok,

float("INF")=='inf'


Does output False, but the output that I want is a ValueError. This is because I have an excel spreadsheet and with string representations of floats and the code I use to determine if a cell has a string representation of a float is

def is_str_float(obj):
    if obj == 'INF':
        return False
    try:
        b = float(obj)
        return True
    except:
        return False



All of the strings which are not representations of floats throw a value error except for "INF" and I do not understand why. I'm not interested in a better way to write the above code I just want to know why float("hey") outputs ValueError whereas "INF" does not.
Was This Post Helpful? 0
  • +
  • -

#9 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,843
  • Joined: 13-June 14

Re: float("INF") == "inf" - why?

Posted 19 January 2019 - 03:20 AM

As usual, you beat about the bush instead of getting to the point. Please, get better at asking what it is you really want to do.

Of course float("hey") throws a ValueError - the string hey doesn't represent a float. They needed a way to represent infinity as a string and they chose inf. Consequently, float("inf") returns a float that represents positive infinity. It really isn't that hard to understand.

This post has been edited by ndc85430: 19 January 2019 - 03:36 AM

Was This Post Helpful? 2
  • +
  • -

#10 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 4,369
  • Joined: 21-June 11

Re: float("INF") == "inf" - why?

Posted 19 January 2019 - 03:23 AM

Because infinity is a valid value for a float and it's written as inf (though float() also accepts any other capitalization such as "INF" or even "iNf" or "InF").

For the same reason, float("nan") will also not throw a ValueError.
Was This Post Helpful? 2
  • +
  • -

#11 bobsmith76   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 358
  • Joined: 14-February 17

Re: float("INF") == "inf" - why?

Posted 19 January 2019 - 03:48 AM

View Postndc85430, on 19 January 2019 - 10:20 AM, said:

As usual, you beat about the bush instead of getting to the point.


You can only beat around the bush if you're trying to conceal something that you don't want to admit to. That was not the case in my situation.


Quote

Please, get better at asking what it is you really want to do.


This is like telling someone who is trying to learn French to speak better French. Obviously, if I had known that 'inf' stood for infinity, then I would not have asked the question. I didn't state that the result I expected was a ValueError because I thought in this case it would be easier to illustrate the bug with a boolean.

View Postsepp2k, on 19 January 2019 - 10:23 AM, said:

Because infinity is a valid value for a float and it's written as inf (though float() also accepts any other capitalization such as "INF" or even "iNf" or "InF").

For the same reason, float("nan") will also not throw a ValueError.


Thanks, I really appreciate you informing me of this without succumbing to the urge to throw in a snide remark as others do.
Was This Post Helpful? 0
  • +
  • -

#12 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,843
  • Joined: 13-June 14

Re: float("INF") == "inf" - why?

Posted 01 March 2019 - 01:08 AM

Even if you don't intend to, you do hide information. You do this by telling us the solution you've chosen to some problem, without telling us what problem you're trying to solve (or at least, not telling us that until later). For example, in one thread, you said you needed to needed to get the types of things and then after telling us you were doing that because you had problems with pickling, we found out that what you actually wanted to do was make sure your software still worked after you changed it. In another, you said you wanted to work out what classes were and were not JSON serialisable and then it later turned out that you didn't actually care about that.

You seem to have some fascination with weird, complicated solutions to problems and you even admitted to not using classes for what they were designed. This approach really doesn't help anyone and we've been trying to tell you this for ages. I'll say this one last time: tell us what high level thing you're trying to achieve (e.g. "I want to make sure my program doesn't break when I refactor it") rather than how you're trying to achieve whatever it is (e.g. "I need to get the types of things even if I haven't imported the modules they're in"). The solutions you pick tend not to be reasonable ones and by asking for help with them, rather than backing up a few levels means you'll probably not discover good solutions.
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11436
  • View blog
  • Posts: 19,498
  • Joined: 19-March 11

Re: float("INF") == "inf" - why?

Posted 01 March 2019 - 08:35 AM

View Postbobsmith76, on 19 January 2019 - 05:48 AM, said:

You can only beat around the bush if you're trying to conceal something that you don't want to admit to. That was not the case in my situation.


That is a somewhat idiosyncratic understanding of that phrase.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1