r/badcode • • Dec 03 '19

python So many things wrong...

Post image
707 Upvotes

106 comments sorted by

View all comments

Show parent comments

13

u/valzargaming Dec 03 '19 edited Dec 03 '19

This will loop approximately 2147483650 times before an underflow to positive.

47

u/Badel2 Dec 03 '19

No, this is python, we don't do that here. Integers can get as big or as small as you want. unless you are using numpy or some other library which is essentially a wrapper around c code, then the library may return incorrect results

11

u/Yanitsko97 Dec 03 '19

Infinite integers?

7

u/Astrokiwi Dec 04 '19

Once integers get too big, python repackages them into a "BigInt" type, which is basically several integers in a trenchcoat.

2

u/Yanitsko97 Dec 04 '19

Damn that‘s awesome. I‘m used to Java so this sounds like magic to me:p

5

u/Astrokiwi Dec 04 '19

The tricky bit is those big integers are a lot slower than raw integers, and it won't warn you when it switches to them. It also means that there's one less warning sign if you have a bug that gives you huge numbers by accident. This is the trade-off of the "just handle it" versus "die gracefully" conundrum.

You can do BigIntegers in Java though, you just have to do it manually.

2

u/Yanitsko97 Dec 04 '19

Yeah doesn‘t seem that magical anymore:p