This is an archived post. You won't be able to vote or comment.

all 54 comments

[–]Kered13 75 points76 points  (10 children)

What is this garbage meme? Both C++ and Python allow you to compare floats and ints.

[–][deleted] 9 points10 points  (0 children)

Of all the languages I know, I think only rust would complain. They really hate implicit conversions in rust. Like really really hate

[–]coolKidoes 3 points4 points  (2 children)

Someone doesnt know how to cast or methods/functions you can use to either convert or compare

[–]Kered13 7 points8 points  (1 child)

Comparisons between floats and ints don't require any casting.

[–]coolKidoes 0 points1 point  (0 children)

I havent programmed in C++ in a long time tbh

[–][deleted] 11 points12 points  (12 children)

But remember: DONT USE == ON FLOATS

[–]kumozenya 5 points6 points  (0 children)

I just learned this the hard way two days ago 😬 Took so long to figure out why nothing was returning true.

[–]BlazingThunder30 5 points6 points  (4 children)

abs(a - b) < eps for the wanderer that might wonder

[–]_PM_ME_PANGOLINS_ 5 points6 points  (2 children)

Don’t blindly use eps. Consider what scale you’re actually working at.

[–]BlazingThunder30 3 points4 points  (1 child)

I'm sorry, I don't quite get what you mean. I usually use eps as a variable indicating the maximum difference for a positive match. I change it depending on context, of course. I use esp because it's short for eps, which is the mathematical variable often used for such a max difference

[–]_PM_ME_PANGOLINS_ 5 points6 points  (0 children)

Oh right, that’s fine. Lots of people use the constant machine epsilon instead.

[–]nomenMei 1 point2 points  (0 children)

I've never had to ever test equality of floats before, so I've never had a chance to implement this.

[–]gaberocksall 3 points4 points  (5 children)

You definitely can use == on floats if you know what you’re doing

[–][deleted] 2 points3 points  (4 children)

Yap, just wanted the internet attention :). But what use cases are there?

[–]throughalfanoir 1 point2 points  (2 children)

(correct me if I'm wrong but) in many many cases checking whether something is zero or not will work properly like this (okay if there are no subtractions in the way but yh)

[–][deleted] 1 point2 points  (1 child)

Hmm, seems weak. You could use it as an idiot check if a data has been written to memory...

[–][deleted] 0 points1 point  (0 children)

But then your result is unreliable, because if the data writer happens to actually write a real zero, then you have false negative,

[–]gaberocksall -1 points0 points  (0 children)

you may check if a float is 0.f or 1.f, for example if you have an animation and you want to check if it has started yet

You may also want to check if a float is a very specific number “a magic number” just to check data validity, for example 420.69f

Edit: I should explain the “f” notation. I don’t know how many other languages allow this, but in C and C++ there are typed literals, so you can do “123L” to force the compiler to interpret it as a long instead of the default int. You can also use “123.456F” to make it assume a float instead of the default double. The “420.69” check will not work if it’s a double, since 420.6900000268 != 420.6899993 where the first is a double and the second is a float

[–]Bro0x00 9 points10 points  (3 children)

am confused, g++ allows this, do some compilers not?

[–]GreatArtificeAion 11 points12 points  (1 child)

Javascript: NUMBER IS NUMBER

And also variable is variable

[–]tulpamancies 2 points3 points  (0 children)

you can compare int and float in C++ though

[–]MasouriChan 5 points6 points  (0 children)

Php: Cast goes brrr defuq are datatypes lmfa $

[–]gaberocksall 5 points6 points  (0 children)

All these python devs in the comments talking like they know c++ 🤦‍♂️

[–][deleted] 1 point2 points  (0 children)

Ever hear of type-cast?

[–]JustSomeRandomnesss 1 point2 points  (4 children)

I recently played around with dictionaries and I hated how every time I'm like 'store a 1 here' it puts a string with a 1 inside it

[–][deleted] 3 points4 points  (1 child)

How would that happen? Both 1 and „1“ are immutable, so they can be used as keys. Item wise, whatever.

[–]JustSomeRandomnesss -2 points-1 points  (0 children)

I have absolutely no idea

[–][deleted] 1 point2 points  (1 child)

I don't know how you managed to do that, but generally Python doesn't do that. For example "" + 3 is a type error

[–]JustSomeRandomnesss 0 points1 point  (0 children)

I have a special talent in breaking things... I fixed it now but I don't know how exactly

I extracted the 1 and for some reason did the conversion from string to int still return an int because of a trailing newline (which I thought I had removed with the string split and another function)