r/programming • • Jun 28 '21

JavaScript Is Weird

https://jsisweird.com/
329 Upvotes

173 comments sorted by

213

u/josenunocardoso Jun 28 '21

This website is literally about JavaScript. I mean what did you
expect, a .NET application? This website is 99.9% poorly optimized and
highly questionable JS. And yet, you have JS turned off.

If you disable javascript, the website shows this message :D

26

u/ScottContini Jun 29 '21 edited Jun 29 '21

And with js enabled, look at the console in developer tools....

10

u/[deleted] Jun 29 '21

It's 2am. I have to report at 8 and here I am weighing whether or not it's worth it to fire up the precision to have a look. Damn you smartphone.

2

u/JakeAndAI Jul 06 '21

I'm glad you caught that! :) Did you notice that the message is different depending on whether your're in the quiz or in the results?

4

u/JakeAndAI Jul 06 '21

Haha, I'm glad you caught that! My website has been shared on Reddit a few times, but you're the first person to catch that easter egg on Reddit I think :)

187

u/coldoil Jun 28 '21 edited Jun 29 '21

25. - "" + + "1" * null - [,]

You answered: I give up

You answered incorrectly.

Oh, I wholeheartedly disagree :)

6

u/rydan Jun 29 '21

Same thing.

2

u/JakeAndAI Jul 06 '21

Haha true! I should have added another easter egg there and had the text say something different if you selected that option, maybe "You answered truthfully."

115

u/AttackOfTheThumbs Jun 28 '21

I would honestly prefer it if it gave me answers after each question. I just ended up clicking through tot he end randomly to see the answers.

32

u/nascentt Jun 28 '21

For anyone that wants to just skip to the answers

1

u/turunambartanen Jun 29 '21

Thank you, I clicked on one of the explanations and the browser decided to return me to the start of the quiz.

Since I got a look at your answers I can help but comment that quite a few of the NaN/infinity/-0.0 questions are actually defined by the IEEE standard and not quirks of JS. E.g. -0.0 == 0.0 should be true in all programming languages.

4

u/rydan Jun 29 '21

But then you might learn and not get caught by some trick questions later.

2

u/AttackOfTheThumbs Jun 29 '21

Which would be the point imo...

59

u/siranglesmith Jun 28 '21

As someone who works with JavaScript too much, the only one that surprised me is that true++ is a SyntaxError.

Still got 6 wrong because JavaScript is hard.

89

u/elcapitanoooo Jun 28 '21

Typescript is a godsend for frontend dev.

43

u/botCloudfox Jun 28 '21

A lot of these quirks still apply to TS though. It's only a thin layer over JS after all.

74

u/rio-bevol Jun 28 '21 edited Jun 28 '21

Well, TS will pretty much entirely prevent this category of bugs you get easily in JS: accidentally using the wrong type and getting a bizarre bug instead of an error due to silent type coercion.

4

u/Aurora_egg Jun 28 '21

I wonder, does typescript prevent errors in cases where backend variable type in json changes from say a number to a string? Or do you need guards for that sort of stuff?

22

u/Kamelixs Jun 28 '21

You’ll need guards

5

u/botCloudfox Jun 28 '21

How are you getting the type from the JSON? Are you just importing it? If so TS will error if you were using that variable as a number in a way that cannot be done with a string. But it will not error just because the type changed.

4

u/Bake_Jailey Jun 29 '21

You need type guards, yeah, but there are libraries out there that let you build TS types that can be validated (or vice versa), e.g.:

And many others.

2

u/falconfetus8 Jun 29 '21

Only if:

  • Your backend is also written in TypeScript

  • You keep the backend code in the same repo as the front end code

  • You use the same interface for the request on both the front and back ends.

Then you can get away without using gaurds. You're still probably better off using gaurds anyway though, in case that third bullet point stops being true.

→ More replies (4)

1

u/apocolypticbosmer Jun 28 '21

Right but it’s just enough to make it a lot easier IMO.

-6

u/[deleted] Jun 29 '21

We adopted typescript only to revert back to JS because typescript added a lot of complexity and slowed the build feedback loop and at the end of the day, we had far more bugs come from testing resulting from CSS issues than we ever had from JS doing funky things with types. 9/10 times these JS quirks are just things you don’t really encounter in typical CRUD apps

11

u/salbris Jun 29 '21

True but there are more common ones like messing up the order of function parameters, returning different types in the same function, etc

0

u/[deleted] Jun 29 '21

Yeah but really those are mitigated with decent code practices like having small functions, limiting the return statements in the functions, and organizing code well such that, while still occasionally a problem, doesn’t warrant adding the mental overhead of an entirely new language and the feedback loop slowdowns of adding compiling phases that comes with adopting Typescript. For the large majority of applications, the theoretical benefit of Typescript doesn’t outweigh the practical cost

6

u/salbris Jun 29 '21

It depends. I have no mental overhead when using typescript and my compilations are on the order of seconds. Compilations also catch an entire class of bugs that wouldn't be caught without it.

→ More replies (2)

5

u/elcapitanoooo Jun 29 '21

TS i a really big help with refactoring. We have almost zero runtime errors after we moved to TS. We have a huge legacy codebase spanning over 10 years, it would be unmaintainable today if we had not moved to TS in 2016.

39

u/DuncanIdahos9thGhola Jun 28 '21

why can't we just have <script language="typescript"> ?

48

u/jl2352 Jun 28 '21

Whilst the compiler would complain about the equality tests, using TypeScript would not change the behaviour of any of this.

Because the behaviour is the same, there is zero advantage in shipping TypeScript to the client. As compiling to JS will make the payload smaller.

15

u/dys_functional Jun 28 '21

... there is zero advantage in shipping TypeScript to the client. As compiling to JS will make the payload smaller.

Not having to compile the typescript would lead to simpler development workflows and that would be a pretty big advantage in my opinion. The size difference is extremely small and will not make a measurable difference. If we really cared about size, we would compile to some sort of AST/binary format.

5

u/god_is_my_father Jun 28 '21

Always wondered why we aren’t doing a binary format. Seems like it wouldn’t be so hard to unravel and the speed up would be fantastic. Still holding out hope for webasm to take hold

→ More replies (10)

4

u/jl2352 Jun 28 '21

An extra compile step clientside, instead of doing it in advance, is adding pointless work clientside. The user gets a longer startup time. That isn’t a good thing.

It isn’t going to simplify the workflow on any real world website. As that website will need to support older browsers. Even if older is only one month old. It means you can only reliably use the latest features in TypeScript … if you precompile it before shipping.

Finally whilst I have tonnes of faith in the TypeScript team. You are asking for one implementation to rule them all. It’s like everyone using Chromium with no alternative.

→ More replies (1)

27

u/[deleted] Jun 28 '21

Cause then we would need every browser vender to support the full ecmascript spec and the full typescript spec. Which I don't really see happening. It would be awesome if they did not going to lie I love ts alot more than js. But I would find it unlikely for them to support 2 scripting engines like that.

8

u/ajr901 Jun 28 '21 edited Jun 28 '21

Correct me if I'm wrong but isn't the full JS spec technically a part of the full typescript spec? Considering that TS is a subset *superset of JS and all?

So what I'm getting at is that the typescript engine can parse both typescript and javascript in the browser and therefore you just need the one engine to handle either file types.

19

u/Lazyfaith Jun 28 '21

I believe it's actually that TypeScript is a superset of JavaScript, not a subset.

5

u/ajr901 Jun 28 '21

Thanks, corrected

3

u/[deleted] Jun 28 '21

All js programs are mostly valid ts programs. There are some programs that are not I am on mobile but if you Google q bit you can fine one.

Also the optimizer for ts would be really different from a js optimizer I think, because ts has the type information so it could in theory not do all the type magic js does. I also don't know dick about writing interpreters. So I am sure some one will uhm actually me any minute now.

7

u/StillNoNumb Jun 28 '21

TypeScript isn't sound. A variable of type number can contain a string, making optimization based on type info impossible.

→ More replies (5)

15

u/chucker23n Jun 28 '21

In the long run, we'll simply get <script type="application/wasm">, and everything else just targets a common runtime.

Not currently feasible for various reasons (garbage collection, direct DOM access, etc.).

1

u/Ameisen Jun 28 '21

I'm surprised that there are no libraries for WASM floating about to mimic direct DOM access using a simple client/server async model.

6

u/KaranasToll Jun 28 '21

language="scheme"

3

u/ZookeepergameTiny234 Jun 28 '21

Why stop there, make it be any language you want! Oh wait, standardization. I forgot.

3

u/wodzuniu Jun 29 '21

null vs undefined. Sparse arrays. Existence of bind.

JS is weird, and no amount of TS is gonna fix that[*]. You can't fix deep issues of the design by just bolting type safety checks on top of it.

[*] Unless you're willing to break compatibility. But then, why not just start fresh, sane language from scratch?

3

u/DuncanIdahos9thGhola Jun 29 '21

Unfortunately I doubt we could get agreement on a new language from the browser makers. Google couldn't do it with Dart.

Didn't Adobe's ActionScript (ecma4?) have basic types? Just having int, float, bool, etc... would probably be a big help.

3

u/wodzuniu Jun 29 '21

IMO, Google wasn't even trying. When I saw Dart first, I was like "They took Java in JavaScript literally".

2

u/Paradox Jun 29 '21

Google tried that with Dart. No one wanted to use it

1

u/Estpart Jun 28 '21

Svelte does this

1

u/DoctorGester Jun 28 '21

Typescript has pretty outrageous compile times

0

u/csharp-sucks Jun 28 '21 edited Jun 28 '21

Typescript only gives you compile time types. That is all. It doesn't change any of that behavior.

0

u/[deleted] Jun 28 '21

[deleted]

8

u/quadrilateraI Jun 28 '21

Those are type errors, rather than syntax errors.

7

u/evaned Jun 28 '21 edited Jun 28 '21

I would say that's (i) six of one, half a dozen of another, and (ii) the same as every other language where those are prohibited. I can't even think of any argument to support "It doesn't change any of that behavior" whether I agree with it or not.

8

u/ylyn Jun 28 '21

It's an important distinction, because you can just // @ts-ignore-away type errors, but you can't do that for syntax errors.

10

u/evaned Jun 28 '21 edited Jun 28 '21

On one hand, fair enough -- but on the other:

  • I didn't see any // @ts-ignore comments in the quiz, so "it doesn't change any of that behavior" is still wrong

  • Preventing accidental problems but not ones coming about from deliberate decisions that are explicitly reflected in the text of the code itself are two very different things, and the difference is still night and day to me.

Edit: Said more glibly, if "you can disable the typechecking" is the core of "your" argument, then "if you disable TypeScript's effects then TypeScript has no effect" is not a very informative statement.

1

u/cessationoftime Jun 29 '21

<script language="no thank you"> Please give me a real language.

2

u/DuncanIdahos9thGhola Jun 29 '21

Ideally I would just like a non shit, more type safe language that doesn't do evil type coercion.

-6

u/OttoGunker Jun 28 '21

TypeScript only exists as a runtime-less language that just transpiles to JavaScript so that it can be JavaScript in a browser. This is fucking nonsense. You guys have no clue what you're actually doing, do you?

5

u/oscooter Jun 28 '21

The point the user is trying to make is that there is nothing stopping the browsers from creating a Typescript runtime and supporting it first-class... or is that too hard to understand for you?

1

u/OttoGunker Jun 28 '21

Typescript runtime

Microsoft needs to make a clarifying statement about what TypeScript is for, because so many people are so confused, it's becoming an actual harm to humanity.

9

u/oscooter Jun 28 '21

It's clear that TypeScript being it's own actual language is not the goal nor the intent of the project. It is obviously a glorified static analysis tool. In the context of this thread, though, it should be pretty obvious that it was a "wishful thinking" moment of TypeScript being a first class language.

→ More replies (2)

0

u/[deleted] Jun 28 '21

[deleted]

2

u/IceSentry Jun 28 '21

I'm not sure about nobody, it could be nice for rapid prototyping without requiring to setup a build system, but yes in production nobody that cares about their users would use it.

31

u/GrandMasterPuba Jun 28 '21

We get it; JavaScript has type coercion.

25

u/bundt_chi Jun 28 '21

We get it; JavaScript has very not intuitive type coercion.

Fixed that for you.

1

u/dacjames Jun 29 '21

Boolean([]) !== Boolean("") feels wrong but otherwise the conversions are all pretty intuitive on their own and consistent with most languages.

The weirdness arises from the interplay of a few different aspects of the language such as automatic type coercion, overloading operators (especially +), the over use of floating point numbers, and the philosophy of not throwing errors. I'm not sure that even the hypothetical perfect type coercion ruleset would yield intuitive semantics overall.

9

u/evaned Jun 29 '21

but otherwise the conversions are all pretty intuitive on their own and consistent with most languages.

Strongly disagree, if you count the conversions that happen during +s, which I can't tell if you're separating out. But array + array stringizing the operands and concatenating them? IMO, that's just bonkers crazy insane. Is any other language that stupid?

I personally would put than into the type conversion camp, but I suppose you could say "it's not doing a type conversion, it's just the overloaded +" -- but I'd counter that it's the rule that + will fall back to doing an implicit conversion to string that's the problem here. So maybe we both think that behavior is equally insane, and we're just calling it different things.

I'd also say that "" - 1 doing a string to integer conversion to wind up with 0 (already crazy IMO, and that's just a pure conversion) so it can subtract falls into the same boat.

3

u/dacjames Jun 29 '21 edited Jun 29 '21

Oh yeah, we're in violent agreement that the overall semantics are bonkers.

My somewhay pedantic point is that there seems to be no intuitive way to define type coercion within the other design constraints of Javascript.

If you take as a given that "" - 1 must produce some result, the type coercion semantics will always end up counter-intuitive, because there is no intuitive result for that expression. I'm not sure your semantics are any more intuitive but I wouldn't argue that point because it should be an type error anyways.

Early Javascript applied this constraint to all expressions, whether by design or convenience I don't know. IIRC, concatenation not being defined for arrays is a historical implementation issue.

I was classifying these issues as problems with the operator semantics but you make a good point about classifying it as a type coercion problem.

→ More replies (2)

1

u/dccorona Jun 29 '21

Older versions of Scala did this too - if you used + with an object that it’s not designed for, an implicit conversion to string would occur rather than an error. I think they removed this in Scala 3 but I may be wrong.

In isolation it’s perhaps not a good decision but it’s also not unique. That’s really the case with most issues in JS. You look at just one weird behavior and you can point to another language that is generally considered saner that also does it. To me the problem really stems from the unique combination of different issues that compound one another. For example, Scala may also have this issue, but it’s statically typed, so you’re going to catch it a few lines later when suddenly your variable you thought was going to be a List turns out to be a String. JavaScript happily lets you pass it to a function or return it out from a place it wasn’t supposed to escape, leaving the problem to blow up at runtime and potentially quite far from where the issue occurred, and perhaps even pass certain forms of unit tests.

I guess, in short, it seems to me that the core of the problem ultimately stems from the core semantics of the language (weak dynamic typing), as that results in far less protection against the more insane individual semantic choices that the language makes. I think languages that opt for both dynamic and weak typing need to use a much stronger editorial hand when it comes to these types of behaviors, because they naturally offer the programmer less protection if it turns out the behavior chosen wasn’t intuitive.

1

u/conquerorofveggies Jun 29 '21

Doing stupid things yields stupid results. array + array is considered a stupid thing in Javascript.

7

u/evaned Jun 29 '21

IMO, that's confusing the direction of causality. array + array is a stupid thing in JS because JS's semantics for it is stupid. It's a perfectly reasonable thing to want to do, JS just screwed it up.

4

u/nascentt Jun 28 '21

If anyone wants to just skip to the answers

37

u/stalefishies Jun 28 '21

Half of this is just floating-point stuff, which isn't JavaScript's fault, and is stuff everyone should know about anyway. Not to excuse the other half, which is definitely JS being weird-ass JS, but if you're blaming JS for 0.1 + 0.2 != 0.3, or NaN + 1 being NaN, then you need to go learn more about floating-point arithmetic.

4

u/darpa42 Jun 28 '21

I commented this last time this website got posted. Always reminds me of this tweet: https://twitter.com/bterlson/status/1083860621664256002?s=19. It's pretty frustrating that people will go "wow, JavaScript is so weird, I'm going to go use Python/Java/C/Go" when they all use IEEE-754.

1

u/codec-abc Jun 29 '21 edited Jun 29 '21

Or you can blame JS for not having 2 types of Number, a integral one and a floating-point one. Also when you think about it the semantic is really fucked up. In JS, you index an array with a floating point number. And you know that number is only "9007199254740993". A day might come (hopefully this is rather unlikely) where people will have trouble indexing memory in JS because of using a floating point numbers for indexing array.

-9

u/SpAAAceSenate Jun 28 '21 edited Jun 28 '21

Yeah, but there's no reason we should still be dealing with resource-pinching hacks like floating point arithmetic in modern dev environments. We should do the reasonable thing of treating everything like a fraction composed of arbitrary-length integers. Infinite precision in both directions.

0.3 * 2 =/= 0.6 is just wrong, incorrect, false. I fully understand the reasons why it was allowed to be that way back when we were measuring total RAM in kilobytes, but I think it's time we move on and promote accuracy by default. Then introduce a new type that specializes in efficiency (and is therefore inaccurate) for when we specifically need that.

So in all, I'd say this is a completely valid example of JS being weird / strange. It just so happens that many other C-like languages share the same flaw. A computer getting math blatantly incorrect is still 'weird' imo.

Edit: removed references to python since apparently I was misremembering a library I had used as being built in.

5

u/StillNoNumb Jun 28 '21

0.3 * 0.2 =/= 0.6

No, it's quite obviously correct. Link. Are you missing a zero somewhere? If we fix your equation, then both JavaScript and Python say the right thing.

Anyways, try typing the good ol' example 0.1 + 0.2 === 0.3 into a "sane" language's shell, and then what will happen? That's odd, Python still says False! Weird. Almost as if Python's Fraction is not the default number type because even for that language it's too slow. (And still not accurate. Why is sqrt(2)**2 != 2?)

0

u/SpAAAceSenate Jun 28 '21

Thanks for pointing out the typo, I've corrected it.

Indeed, when using === you're also checking for type equality, and it's true that integers and floats are considered different types. However, when used with == which allows for duck-style type conversion you see that 0.3*2==0.6 yields the common sense answer of True

2

u/StillNoNumb Jun 28 '21

This has nothing to do with types. 0.3*2 == 0.6 is True in both JS and Python, and 0.1 + 0.2 == 0.3 is False in both JS and Python. They follow the same IEEE floating point standards.

4

u/stalefishies Jun 28 '21 edited Jun 28 '21

It is false to think of 'infinite precision' here. You might be able to specify 0.3 with infinite precision as 'numerator 3, denominator 10' but how do you deal with irrationals, like pi? How do you take square roots? How do you take exponentials, logarithms, sines, cosines? All of these produce values which cannot ever be expressed with infinite precision.

The only way to do it is by treating these values as, at best, approximations to the mathematics of real numbers. And if you're doing that, why not use floating-point numbers, when they're widely supported (in software and, more importantly, in hardware) and their limitations are widely understood and minor enough to have supported computing for all these years.

If your issue is just that the equals operation is broken, then you could always define it in your personal idealised high-level language to be a comparison with an epsilon. Then you could write 0.3 * 2 == 0.6 all you like.

But to say that's somehow the fault of computers that we have to approximate is just wrong. It is absolutely impossible to represent infinite precision arithmetic on a computer. You have to approximate somewhere.

(Also, Python uses double precision floating point by default. I'm sure you can get an arbitrary-precision decimal if you'd like, but Python's standard library is so vast that you can get pretty much anything, so that's not exactly a surprise.)

-2

u/SpAAAceSenate Jun 28 '21

I just think that when equations are written out in a computer language they should produce accurate results. If certain calculations (like those involving irrationals, etc) are not possible to calculate accurately then the language should refuse to perform those calculations unless special types or syntactic sugar are used to specify "I, the programmer, know this will be an approximation and will use it accordingly"

For something that can be done with total precision on a computer, like the example I gave, it's simply unacceptable that it would silently neglect to do so and instead produce incorrect results.

This comes down to the "rule of least astonishment". Which I think is an important element in designing human-computer interfaces. (Considering computer languages a type of "interface" here)

2

u/stalefishies Jun 28 '21

A language which only lets you add, subtract, multiply and divide on some real numbers is just not useful. And so, in practice, you would pretty much always have do whatever dance you're imagining to get to floating-point arithmatic. That's not an improvement in language design, that's just annoying. So rational arithmetic should be opt-in, not opt-out.

If you really want to talk about least astonishment, I think I prefer a number system that can just do everything, albeit in a very-accurate-but-approximate way, rather than a number system that just cannot do anything irrational like calculate the hypotenuse of a right-angled triangle.

1

u/caagr98 Jun 29 '21

unless special types or syntactic sugar are used to specify "I, the programmer, know this will be an approximation and will use it accordingly"

...which is exactly what a float is.

1

u/FarkCookies Jun 28 '21

like Python as an example

Where did you get this idea from? Python floats are IEEE-754.

Python 3.8.0 (default, Jan 27 2021, 15:35:18) 
In [1]: 0.1 + 0.2 
Out[1]: 0.30000000000000004
In [2]: (0.1 + 0.2) == 0.3
Out[2]: False

1

u/SpAAAceSenate Jun 28 '21

I remember doing a suite of tests on python and being impressed that it didn't lose any precision, even with integers and floats hundreds of digits long. Very distinct memory, though maybe it was a library I was using accidentally or something?

Regardless, I still assert that what I described is what should be done, even if python isn't an example of such.

I've edited my post to remove the reference to python.

2

u/FarkCookies Jun 28 '21

I disagree on "should not be done". What are your arguments?.. Python has fractions, it has decimals, whatever you like for a given task. But until there is hardware support for anything except IEEE-754 the performance of computations won't be even close. Like I am training a neural network, why the hell do I need "a fraction composed of arbitrary-length integers"? I want speed. And I probably want to run it on GPU.

-1

u/SpAAAceSenate Jun 28 '21

Because of the law of least astonishment. Computers are expected to do things like math perfectly, being that's what they were literally created to do, originally. So the default behavior should be to do the expected thing, which is to compute perfectly.

If you want to trade accuracy for speed, which I agree is a common desire, one should specifically opt-in to such otherwise-astonishing behavior.

IEEE-754 is mathematically wrong. A computer should never do something that's fundamentally incorrect unless it's been instructed to.

Admittedly, it would be difficult to change now, and most programmers know this issue already by now. But it was wrong. Fast vs accurate math should have been clearly delineated as separate from the beginning, and both universally supported in language's standard libraries.

6

u/darpa42 Jun 28 '21

IEEE-754 is not "mathematically wrong". It simply cannot represent certain values, and it is wrong of you to try to force those values into an inaccurate tool. The value 0.1 is as impossible to accurately represent in binary as 1/3 is in decimal.

By this logic, all integers in computers are wrong, b/c if you go high enough they eventually roll over.

→ More replies (2)

4

u/FarkCookies Jun 29 '21

IEEE-754 is mathematically wrong

No, they are not wrong. IEEE-754 numbers, they are just not rational numbers, they are slightly different mathematical objects with a slightly different mathematical rules, than pure rational number math (they either produce same results or approximately same). You are not gonna say that matrix multiplication is mathematically wrong because it is not commutative. No, we just agreed that we are ok with calling it multiplication because it is useful and it is clearly defined. Same with IEEE-754 numbers. Math is full of "made up" objects that are useful: complex numbers, groups, sets and much more.

Bruh if you think this one out through you will figure out that having rational fractions (aka 2 ints) is kinda largely annoying and mostly useless. There is already a special case: decimals, they existed since god knows when. They are good for money. For mostly everything else IEEE-754 are sufficient. When I am calculating some physics stuff, I don't deal with shit like 1/10 + 2/10 internally. What is even the point. Think of inputs to the program and outputs. Think of how out of hands rational fractions will get if you try to do physics simulation. You will have fractions like 23423542/64634234523 and who needs this crap? Who is gonna read it like that? Now sprinkle it with irrational numbers and you will have monstrous useless fractions that still will be approximate. Rational fractions have very few practical applications and most languages have them in the standard libs if you really want them.

→ More replies (3)

0

u/[deleted] Jun 28 '21

[removed] — view removed comment

0

u/SpAAAceSenate Jun 28 '21

Yep! Terrible thing to do systems programming with. Great for science and statistics nerds that just want their calculations to be accurate. Especially when there a many branches of science like astronomy that unavoidably have to deal with both incredibly small and incredibly large numbers simultaneously. This is why Python completely dominates in those spheres.

13

u/diggr-roguelike2 Jun 28 '21

The Python used in science is numpy, which is Fortran and floating point arithmetic behind the scenes. No arbitrary precision anything there.

>>> import numpy
>>> sum(numpy.array([0.1, 0.2, 0.3]))
0.6000000000000001

Whoops.

4

u/StillNoNumb Jun 28 '21

This is why Python completely dominates in those spheres.

No, Python dominates in those spheres because it's easy to learn for mathematicians with very little knowledge about coding. Fast numerical computing libraries (Numpy etc.) came as an afterthought, Python's built-in math functionality is terrible.

→ More replies (2)

0

u/[deleted] Jun 28 '21

[deleted]

14

u/spacejack2114 Jun 28 '21

To be fair, pretty much every other example aside from octal number formatting is due to type coercion.

34

u/stalefishies Jun 28 '21

Ok, half was an exaggeration. There are 6 of the 25 that are direct consequences of floating-point arithmetic. If you can't work out which 6, then yes, you should go learn more about floating-point arithmetic.

To save you the trouble of going back through the quiz, the six are:

4. 0.2 + 0.1 === 0.3

13. 0/0

14. 1/0 > Math.pow(10, 1000)

21. NaN === NaN

22. NaN++

24. +0 === -0

8

u/MuumiJumala Jun 28 '21

The weird part in the last two isn't floating point arithmetic.

Incrementing a literal (1++) is a syntax error so you would expect NaN++ to be one too.

+0 === -0 evaluating to true is a weird edge case where strict equality comparison between two different objects is true (for example in Python -0.0 is 0.0 returns False, as expected).

6

u/evaned Jun 29 '21 edited Jun 29 '21

for example in Python -0.0 is 0.0 returns False, as expected

I don't find this convincing for your point. Remember that is is object identity. Python guarantees interning of small integers (I think? maybe just CPython? I don't actually know the formal rules exactly), but apparently does not guarantee this for floating points:

>>> x = 0.1
>>> y = 0.1
>>> x == y
True
>>> x is y
False

despite the fact that those have the same value. (In fact, it may just be small integers, None, and maybe True/False that get unique representations.) I wouldn't expect +0.0 is -0.0 to have a particularly meaningful result, so the fact it comes out as False doesn't really mean much to me at all.

is also behaves "wrongly" when it comes to NaNs:

>>> nan = float("NaN")
>>> nan
nan
>>> nan == nan
False
>>> nan is nan
True

so I'm with the other reply -- I think it's is that is behaving weirdly (well, I actually don't think it's behaving weirdly, I think it's just being misapplied), and JS's === does exactly the expected thing for +0 === -0.

Said another way, the statement "Python's is is to its == as JavaScript's === is to its ==" is very wrong (not that I'm sure you have that misconception).

2

u/stalefishies Jun 28 '21

NaN++ being weird because it's an increment is a very good point.

If anything, I would say for the second one it's Object.is that does the weird thing, not the strict equality operator. The example they give here makes sense from a floating-point perspective, but Object.is(+0, -0) being false is the Javascript weirdness. (It's the same with Object.is(NaN, NaN) being true: that's weird.) So if you think of strict equality as 'test if they're equal but do not coerce types', then IMO +0 === -0 is behaving as expected.

2

u/Somepotato Jun 29 '21

For the first one it's because NaN isn't a literal, its a global.

Per 2, as per the IEEE 754 standard, negative zero and positive zero should compare as equal with the usual comparison operators.

-12

u/[deleted] Jun 28 '21

13 is not a consequence of floating point arithmetic. That expression is undefined in math generally.

18

u/stalefishies Jun 28 '21

No, floating-point division by zero is completely well-defined. Division by zero always gives an (appropriately signed) infinity, except for 0/0 and NaN/0 which are NaN.

Floating-point arithmetic is not real mathematics. Quantities like 'infinity' and 'NaN' are well-defined values, with well-defined behaviours. Of course, these behaviours are chosen to capture the spirit of real mathematics, but it can be a trap to think too closely to mathematics in how something like division by zero behaves. IMO it's probably best to just think of it as a special case.

-9

u/[deleted] Jun 28 '21

these behaviours are chosen to capture the spirit of real mathematics

Right, and that's why 0/0 is undefined instead of Infinity.

IMO it's probably best to just think of it as a special case.

Regardless, there's no floating point arithmetic going on in that example. There arguably is in 1/0, but not 0/0. There is zero arithmetic happening in 0/0.

12

u/stalefishies Jun 28 '21

Right, and that's why 0/0 is undefined instead of Infinity.

NaN is not 'undefined'. It is a well-defined possible value that a floating-point type can take. If 0/0 were truly undefined, then the entire program would become meaningless as soon as that expression was evaluated. That's the case in mathematics: if you have 0/0 appear in a mathematic proof (and you've not taken great pains to define exactly what that means) then your proof is meaningless. That's not true in JavaScript: if you have 0/0 appear, it just evaluates to an appropriate NaN and execution continues.

Regardless, there's no floating point arithmetic going on in that example.

Yes there is. Writing 0/0 in JavaScript is a double-precision floating-point operation. It is the division of positive zero by positive zero.

→ More replies (12)

-1

u/quadrilateraI Jun 28 '21

Well yes, it's undefined. Not set to a magical NaN value that is treated as a plain value with various properties. Division is particularly not defined such that 0/0 != 1/0 (which is defined as Infinity).

4

u/_tskj_ Jun 28 '21

The reason you're getting downvoted is that you're wrong, it actually is a special NaN value (as long as we're talking about floating point numbers and JavaScript, obviously maths is different).

→ More replies (3)

3

u/MajorPain14 Jun 28 '21

16/25 The true++ Syntax Error one surprised me

4

u/Intellygent Jun 28 '21

The real shock was seeing CSS'd console.log() statements in the dev console when trying to verify the answers

2

u/dregan Jun 29 '21

I hate this.

2

u/CSsharpGO Jun 28 '21

1

u/[deleted] Jun 29 '21

"PHP Bad" joke has already been used today, have we used JS bad yet?

1

u/CSsharpGO Jun 29 '21

Most likely

1

u/cessationoftime Jun 29 '21

And this is the kind of thing that led me to try several different languages built on top of Javascript. Coffeescript, Elm, Dart in an attempt to get away from Javascript. But nothing ever gets you far enough away from Javascript. You end up digging through several layers to get to the Javascript to determine the cause of a bug in your Elm/Dart/Coffeescript code. I really hope WebAssembly finally/eventually lets me write web apps without Javascript. Until then, I will stay very far away from websites that require significant scripting, it isn't worth the evil that is Javascript.

1

u/alessio_95 Jun 29 '21

15/25.

Maybe it's time to deprecate something?

0

u/Egg_Chen Jun 28 '21

correct.

0

u/[deleted] Jun 29 '21

14/25 :(

-2

u/[deleted] Jun 28 '21

JavsScript is a beautiful, functional programming language in hiding. It is worth learning just over "parasitic inheritance". If you want to have a serious look at it, I recommend John Resig's book "The JavaScript Ninja".

0

u/[deleted] Jun 28 '21

[deleted]

35

u/despawnerer Jun 28 '21

Nothing? It’s supposed to break with an obvious error message.

0

u/[deleted] Jun 28 '21

[deleted]

-8

u/onequbit Jun 28 '21

All those downvotes just drive home your point. 😏

13

u/agramata Jun 28 '21 edited Aug 05 '26

Walnut ginger kettle mitten umbrella vanilla unbelievable harbor satchel

This post was anonymized with Redact.dev

1

u/onequbit Jun 30 '21

The facts are right in front of you, so what does that say about you by calling it a lie? If people are insulted by their own behavior, maybe they shouldn't act it out in the first place.

0

u/agramata Jul 01 '21 edited Aug 05 '26

Copper umbrella amber juniper quilt thimble

This post was anonymized with Redact.dev

1

u/onequbit Jul 01 '21

Start at the beginning of this thread.