4 ms·
parseInt(0.00005) // 0 parseInt(0.000005) // 0 parseInt(0.0000005) // 5 parseInt(0.00000005) // 5 Huh??!
by ccvannorman 3y ago
parseInt(0.00005)
// 0
parseInt(0.000005)
// 0
parseInt(0.0000005)
// 5
parseInt(0.00000005)
// 5
Huh??!
- amelius 3y ago0.00005 -> "0.00005" -> 0 0.00000005 -> "5e-8" -> 5
- Groxx 3y ago^ because: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... > The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems). Surprisingly, if you put garbage in, you get garbage out.
- marcosdumay 3y agoThe garbage obviously being the language's std library.
- Groxx 3y agonah. just type coercion. Why would you parse a float? Parsing is for strings. Converting a float to an int is done with stuff like `Math.floor`.
- xigoi 3y ago> Why would you parse a float? You wouldn't, which is why it should produce an error.
- deleted 3y ago[deleted]
- Groxx 3y agoWhich would be changing the paradigm away from implicit coercion, yes. That would make it a different, entirely hypothetical, ill-defined language. A very clear strawman that isn't particularly worth fighting. It would quite possibly be a better language, but it's not a thing you "just do" to solve footguns - it has pretty big effects on the language and ecosystem.
- foooorsyth 3y agoYeah, that’s terrible behavior for such a critical function like parseInt. JS apologists can legitimately protest some of these examples as being highly unrealistic or just normal type coercion/floating point behavior, but this one here is an unforgivable bug factory.