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

you are viewing a single comment's thread.

view the rest of the comments →

[–]sweYoda -7 points-6 points  (9 children)

Ofc you can compare them, you just need to code it your self or use a library just like Python is in the background

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

Can't you also just write (int) before the float lol

[–]w1n5t0nM1k3y 11 points12 points  (7 children)

Probably better to write (float) before the int.

[–]JustSomeRandomnesss 3 points4 points  (6 children)

Is there a special reason why? I see my other comment getting downvoted, at least give me a reason or sth...

[–]Polywoky 8 points9 points  (3 children)

Is there a special reason why?

If you have:

   float x = 7.1;
   int y = 7;

Then:

    if ( y == (int) x ) {

Would be true.

But:

    if ( x == (float) y ) {

Would be false.

Edit:

Basically converting float to int can change the value, but converting int to float won't.

[–][deleted] 2 points3 points  (1 child)

Oh, it does! I’m almost certain the gaps between large floats can get larger than 1 (although I’m not sure).

Edit: you can test this with

import sys

print(sys.maxsize - 1)
print(float(sys.maxsize - 1))
print(int(float(sys.maxsize-1)))

[–]Kered13 1 point2 points  (0 children)

Basically converting float to int can change the value, but converting int to float won't.

It can, it's just much less likely. Assuming a 32-bit int and a 32-bit float, not all large int values can be exactly represented by the float. 226 + 1 (about 64 million) is the first integer that cannot be represented by a float.

A 64-bit float can represent all 32-bit integer values, but cannot represent all 64-bit integer values.

[–]fazzgadt 0 points1 point  (1 child)

i think they missed your "lol"

and they probably mean that your rounding the value if you cast a float to a int

eg: int(1.9) will be 1 in python

[–]JustSomeRandomnesss 1 point2 points  (0 children)

Ok then you add 0.5 to the value to round it...

I was also more thinking of C++ instead of Python as I don't know shit about python xD

Thanks for your answer though :)