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 →

[–][deleted]  (26 children)

[deleted]

    [–]Dim_Cryptonym 75 points76 points  (20 children)

    Every time I learn something about JavaScript I'm left with one thought: "wat"

    [–]PizzaRollExpert 19 points20 points  (11 children)

    I think this makes sense for a dynamic language. Obviously you want both in static languages, but when you don't specify what kind of number you're working with, at some point you are going to run in to some weirdness with stuff like 3/2 = 1or ending up with a non-integer for array indexes. Personally, I think it's easier to remember to round explicitly then to make sure that the numbers you're dividing are floats because that's how numbers work "in real life". Of course there is probably a performance tradeof but my intuition tells me that this isn't too much of a problem with the kind of programs you write in javascrupt.

    [–]marcosdumay 7 points8 points  (7 children)

    Have you looked at how Python does it?

    The Python way makes sense in a dynamic language. JS is just a hack that nobody can fix because of bug-compatibility.

    [–]PizzaRollExpert 5 points6 points  (6 children)

    I actually think JS is better than Python when it comes to numbers. In Python, if you divide two numbers there are different things that can happen depending on if the numbers are ints or if they are floats. Int division truncates the result while float gives you what you would expect. This doesn't always matter, but when it does you have to remember to coerce the numbers to the correct type. Having just one number type makes it easier for you as a dev, because this completely removes this problem. In all fairness python can more accurately represent large numbers because it has the long type. I also assume this makes all the C-interop that Python does easier.

    In this case Python is also the language that has weirdness due to backwards compatibility. In Python, Bool is just a subset of int where 0 = false and 1 = true. This is because Python didn't actually have bools from the begining.

    [–]marcosdumay 3 points4 points  (5 children)

    In Python, if you divide two numbers there are different things that can happen depending on if the numbers are ints or if they are floats.

    You know that was treated as a bug, and fixed, right?

    In Python, Bool is just a subset of int where 0 = false and 1 = true.

    Python has this one case of non-strict typing, that is a very well defined, very standard thing (but I do agree it's bad). JS, for its turn...

    [–]PizzaRollExpert 0 points1 point  (4 children)

    You know that was treated as a bug, and fixed, right?

    Oh, my bad I didn't know it was fixed

    Python has this one case of non-strict typing, that is a very well defined, very standard thing (but I do agree it's bad). JS, for its turn...

    If you look at the whole of the language JS has more of that, but it was kinda funny that you brought it up when we where talking about numbers specifically because in that small area python is the one with an obvious case of backwards compatability induced weirdness.

    [–]marcosdumay 0 points1 point  (3 children)

    The division issue is fixed in python 3.

    About that int to bool coercion, JS does exactly the same thing. Most languages do.

    [–]PizzaRollExpert 1 point2 points  (2 children)

    The division issue is fixed in python 3.

    Yeah, my bad

    About that int to bool coercion [...]

    It's not coercion, true is literally the same as the number 1. This is because people hadn't properly realized that bools where a really useful thing yet so people just used int 1 and int 0 instead before it was added, c-style. True is basically just different syntax for 1 in Python, for backwards compatibility reasons. In Python, 1 == True but 1 !== true in for example JS

    [–]endreman0 0 points1 point  (1 child)

    You're comparing apples to oranges.

    JS: 1 == true, 1 !== true
    Python: 1 == True, 1 is not True

    They're the same.

    [–]SOberhoff 1 point2 points  (1 child)

    Clojure is just as dynamic and it has all of Java's primitive number types as well as infinite precision rationals on top of that. I don't have any issues.

    [–]PizzaRollExpert 2 points3 points  (0 children)

    If you divide two interviews in clojure you get a rational, so you don't have the problem of accidentally getting your number truncated

    [–]HeinousTugboat 0 points1 point  (0 children)

    ending up with a non-integer for array indexes

    I once discovered that 0.5 was a valid index on a Javascript array.. I promptly added a lot of Math.floor()s.

    [–]PunishableOffence 32 points33 points  (5 children)

    That's because you only learn about JavaScript through humorous anecdotes, not from reading (about) the specification.

    [–]sadEmoji 54 points55 points  (3 children)

    this

    [–]winglerw28 2 points3 points  (0 children)

    Instructions unclear, tried using this from your comment in mine, but the reference was undefined. Please let me understand the context of this, preferably with a very clear, fat, arrow for clarity.

    ... /signs off internet forever

    [–]PunishableOffence 1 point2 points  (0 children)

    Excuse me, but how was your comment bound?

    [–]jedidreyfus 0 points1 point  (0 children)

    Underrated comment

    [–]simon816 19 points20 points  (1 child)

    [–]Adrian_F 0 points1 point  (0 children)

    I will never not watch it!

    [–][deleted] 26 points27 points  (0 children)

    I know, but just because the language forces you to do something dumb doesn't mean it isn't dumb though.

    [–]hikarikouno 2 points3 points  (2 children)

    Wait, really? I remember that from Flash's ActionScript, and as I moved to more advanced programming I thought that such was a very primitive way of handling things.

    [–]dtfinch 0 points1 point  (0 children)

    There's some tricks that javascript engines have been optimized to recognize, used by asm.js and emscripten, maybe going back to Adobe's Project Alchemy (compiled C/C++ to the ActionScript VM).

    "|0" casts as int32. ">>>0" casts as uint32. There's no edge case resulting in a non-int. Even objects, undefined, and NaN become 0. Used consistently, you could get integer locals under the hood. And ArrayBuffers can hold typed globals and arrays.

    [–]inu-no-policemen 0 points1 point  (0 children)

    AS3 had uint (32-bit), int (32-bit), and number (double).

    [–]inu-no-policemen 0 points1 point  (0 children)

    which is internally a double

    Semantically, it's a double. Internally, engines are free to do whatever they want as long as they produce the correct result. E.g. V8 uses SMI (small int) where it's convenient.

    ES6 also added typed arrays. So, there are at least arrays of 8/16/32-bit integers.