3 ms·
If you use a floating point number as a key in a `dict` the wtf should not be pointing at Python. These are not wtfs, please. These examples read like someone
by JohnKemeny 1y ago
If you use a floating point number as a key in a `dict` the wtf should not be pointing at Python.
These are not wtfs, please.
These examples read like someone who is learning to program and who is confused.
x is not y
x is (not y)
How can you confuse these two?!
- mvanbaak 1y agoAI
- deleted 1y ago[deleted]
- woooooo 1y agoFp32 or fp64 in a strongly typed language would be no problem as hash keys. Yes, they're imprecise for math but the 64 bits of data should work just like a long as a hash key.
- marcosdumay 1y agoNo, they are not suitable for use as hash keys unless you are treating them as opaque binary data. And if you are there, you are better putting your values in some type that means opaque binary data for the start.
- spacechild1 1y agoThey are perfectly suitable as long as you know what you are doing. Here's a real use case: when compiling a scripting language, map float constants to slots in a "constant table" so that all constants with the same value share the same slot.
- zahlman 1y agoIf they are actually compile-time constants and you never have to deduce the value by a runtime calculation, then that is exactly what is meant about treating them as opaque data. Otherwise, prepare for the horror of realizing that `{0.3: 'test'}[0.1 * 3]` (or the syntactic equivalent) fails — in every programming language that lets you try.
- ozgrakkurt 1y agoShouldn’t represent float constants as actual float in a compiler right? Probably better to represent them using some kind of infinite precision decimal
- woooooo 1y agoThey ARE fixed-width binary data, and they're effectively opaque unless you have IEEE 754 open on your desk, it's all handled in hardware otherwise.