4 ms·
By that logic, you should also test if `a` is a string that can be parsed into a number/bigint as well. Or if it's a Uint8Array that you can decode into a numbe
by hombre_fatal 7y ago
By that logic, you should also test if `a` is a string that can be parsed into a number/bigint as well. Or if it's a Uint8Array that you can decode into a number/bigint.
But that's not how you write software. In an application, you have a canonical representation that you pass through your business logic.
- dlbucci 7y agoI don't know if that follows. I think it's reasonable to expect a piece of code that works with integer numbers to also work with bigints. But it's not really reasonable to expect it to work with anything that closely resembles a number. That sort of weak typing is much more problematic and should be avoided. But I dunno, maybe it shouldn't be expected to work with bigints either. It's not a regular int, so I doubt people will start using these as array indices in for loops or what have you, so maybe it should be a different type...
- hombre_fatal 7y agoBut it's really not. Pretty major difference between floats and arbitrary precision, like how you'll get different arithmetic results passing in 42 vs 42n. Just like any language that doesn't let you mix float and integer operands. Also, what's even the return value? You need to make a deliberate decision about your data, not deck the halls with just-in-case programming. The software you write will be much better for it. Besides, even if you could justify a bunch of typeof checks and you were doing it everywhere, Javascript already has a way to spare you from repetitive code: a function.
- recursive 7y agoWell, bigints are fine as array indices. However they are not interoperable with numbers in any type of arithmetic. I don't think it's reasonable to expect to use bigints in any place where one uses numbers. "Regular int" is not a distinct type in javascript. There are now two numeric types: 64-bit floating point, and bigint.
- IggleSniggle 7y agoObligatory informational: JavaScript numbers are IEEE-754 standard. You get 53 bits of precision, but can still represent other numbers outside that range, and there are the usual issues relating to dealing with floats. Math.pow(2,53) // 9007199254740992 Math.pow(2,53)+1 // 9007199254740992 Math.pow(2,53)+2 // 9007199254740994 0.1 + 0.2 // 0.30000000000000004
- mr_toad 7y ago> In an application, you have a canonical representation that you pass through your business logic. Not if your code works with arbitrary inputs. I have some collation sequences that I’ll need to tweak.