Live data from Hacker News

JavaScript Is Weird

jsisweird.com

301–310 of 383 posts

Re: JavaScript Is Weird

#301
post #51

Earlier quoted context omitted.

> That's not really a JS problem, that's a floating point problem More accurately it's a binary problem. 0.1 and 0.3 have non-terminating representations in binary, so it's completely irrelevant whether you're using fixed or floating point. Any number that can be represented as the sum of powers of 2 and 5 have a terminating decimal representation, whereas only numbers that can be represented as the sum of powers of…

No, it is problem for any base. For example decimal system can represent 1/5,1/4, 1/8 and 1/2 properly. But, what about 1/3, 1/7, 1/6, 1/9 as decimal numbers with finite number of digits. This will be a problem for any base representation when it has to be boxed in finite number of digits or memory. One good thing is decimal is widely used format, so it is good to go with that for representing stuff. But, it is more…

Did not read the whole parent comment and my message is redundant. But, yes, objectively decimal can represent more numbers (the numbers that are composed of 1/2 and 1/5).

I think there are arguments to use decimal as representation is there, (where I originally came to know about this problem) [1].

[1] - https://www.crockford.com/dec64.html

Re: JavaScript Is Weird

#302

Earlier quoted context omitted.

I've been thinking for a while that modern languages shouldn't default to floating point computations. They're exactly the right thing if you do data stuff or scientific computing, but given how much of the internet relies on things like e-commerce and how often floating point is still misused for dealing with money, coupled with the fact that even many senior developers aren't fully aware of the subtleties of floati…

> "# use-floating-point" pragma I haven't come across the "pragma" directive in a Javascript context. I guess it's another new feature (I have trouble keeping up these days).

I don't think they have it (although, you can do whatever you want with babel nowadays, I guess). It was a suggestion aimed at programming languages in general.

Re: JavaScript Is Weird

#303
post #244

Earlier quoted context omitted.

Can it be that JS is not good starting language then? Maybe strongly typed compiled languages would be a better fit? One can argue that if you want to program it might be too much information for starters - but IMO basic types are such a fundamental concept that it would be better to teach that concept early on. It also clears up a lot of newbie confusion as you cannot assign string to an int by mistake with strongly…

> Can it be that JS is not good starting language then? @ozim, I don't believe you are trolling! JS is an absolutely awful language for beginners; it's a 155mm howitzer pointed at your foot. Although you can get the hang of the basics quickly, AND it runs in the browser (super-convenient unless you're headless), those "basics" that you thought you'd got the hang of turn out to have semantics that make sense, but are…

I am not trolling just a honest question based on parent poster experience.

I agree OO is too much for any beginner. Getting started with procedural thinking + basic types is in my opinion a requirement.

I agree nice parts about starting with JS is that basically only things you need is text editor and a browser.

Unfortunately data types will show up one way or the other so there is no need of hiding those. Even in Excel creating formulas one has to understand text vs number and difference between '.' and ',' and how operating system configuration make sometimes '.' to be decimal separator and sometimes ','.

Re: JavaScript Is Weird

#304
post #19

0.2 + 0.1 === 0.3 That's not really a JS problem, that's a floating point problem. Plenty of languages will have the same issue. +!![] "" - - "" (null - 0) + "0" Calling these things weird is fair enough but I can't help thinking this is code you'd never actually write outside of the context of a "Look how weird JS is!" post. It's like picking examples from the Annual Obfuscated C Contest to show how hard it is to un…

I've been thinking for a while that modern languages shouldn't default to floating point computations. They're exactly the right thing if you do data stuff or scientific computing, but given how much of the internet relies on things like e-commerce and how often floating point is still misused for dealing with money, coupled with the fact that even many senior developers aren't fully aware of the subtleties of floati…

I agree. I think a high level programming language like JavaScript should default to the more "correct" (least surprising) behaviour, and let the programmer opt in to floating point numbers when needed for performance.

In modern JavaScript, you can use BigInt literals by suffixing an n, like this:

  const maxPlusOne = 9007199254740992n;
If I could magically redesign the language, I would make all integer literals be effectively BigInt values, and I would make all decimal literals be effectively decimal values so that 0.1 + 0.2 === 0.3. I would reserve the letter suffixes for more performant types like 64-bit floating point numbers that have surprising behaviour.

Re: JavaScript Is Weird

#305

Earlier quoted context omitted.

No, it is problem for any base. For example decimal system can represent 1/5,1/4, 1/8 and 1/2 properly. But, what about 1/3, 1/7, 1/6, 1/9 as decimal numbers with finite number of digits. This will be a problem for any base representation when it has to be boxed in finite number of digits or memory. One good thing is decimal is widely used format, so it is good to go with that for representing stuff. But, it is more…

Did not read the whole parent comment and my message is redundant. But, yes, objectively decimal can represent more numbers (the numbers that are composed of 1/2 and 1/5). I think there are arguments to use decimal as representation is there, (where I originally came to know about this problem) [1]. [1] - https://www.crockford.com/dec64.html

Discussion on hn about this : https://news.ycombinator.com/item?id=16513717

Re: JavaScript Is Weird

#306

Earlier quoted context omitted.

Some of them are stretched examples, others comes from other languages/constraints (floating point, octal, ...), but some other are legitimately weird and error prone: [1, 2, 3] + [4, 5, 6] // -> "1,2,34,5,6" [,,,].length // -> 3

The first is only weird if you expect the + operator to perform an operation on arrays. It doesn't, so each array becomes a string and those two strings are concatenated. The second is only weird in that you are constructing an array with implicit undefined values, which is exactly what I would expect to happen if my linter didn't complain about the syntax and I had to guess what might be happening.

> The first is only weird if you expect the + operator to perform an operation on arrays.

Like GP said, comes from other languages. That line can be copy/pasted into python and it performs concatenation.

Re: JavaScript Is Weird

#307
post #49

Fortunately nowadays including any of those in your code would award you the review equivalent of bashing your head in. With the advent of linters most of these patterns started being rightfully considered bad form by default. Currently even innocent stuff like +new Date() (outputs a timestamp) is already something that I've rarely seen make it through review. JS is goofy, but ES2015 managed to avoid making the same…

I am eternally surprised at how long it took node to support ESM. CommonJS modules served their purpose, but they are about to go the way of JS module systems. This is where the appeal of deno comes in. It's a runtime that tries very, very hard to give you the same API surface as that of the browser. I just hope deno catches on faster.

I consider Deno to be DOA - hopefully I'm wrong.

The scope was just too broad.

Re: JavaScript Is Weird

#308
post #75

Earlier quoted context omitted.

That’s a load of gibberish. JS as a language has nothing to do with browser APIs, why would you judge it based on that? Also, ES6 was ratified six years ago. Seems like you had a bad experience with espruino / jerryscript or something and are projecting based on that? Dynamic languages are easier to program in. That’s a fact, and why they are so popular.

Although I like Javascript (Typescript) a lot, I have to bite: > Dynamic languages are easier to program in. Only if you don't care about writing correct and maintainable programs. The only thing that dynamic languages make easier is writing code. Or, more specifically, the first couple of versions of it. That's not what typical programming as a process mostly consists of.

By the definition of formally correct, yes, you’re right. But dynamic programming still allows programs to be functionally correct, and verified by testing, which is more often than not good enough.

Re: JavaScript Is Weird

#309
post #19

0.2 + 0.1 === 0.3 That's not really a JS problem, that's a floating point problem. Plenty of languages will have the same issue. +!![] "" - - "" (null - 0) + "0" Calling these things weird is fair enough but I can't help thinking this is code you'd never actually write outside of the context of a "Look how weird JS is!" post. It's like picking examples from the Annual Obfuscated C Contest to show how hard it is to un…

I've been thinking for a while that modern languages shouldn't default to floating point computations. They're exactly the right thing if you do data stuff or scientific computing, but given how much of the internet relies on things like e-commerce and how often floating point is still misused for dealing with money, coupled with the fact that even many senior developers aren't fully aware of the subtleties of floati…

Having the full number stack supported (natural numbers - integers - rationals - reals) would be, indeed, awesome.

Sadly, most people don't understand the distinctions, so this will never happen. (Even in reply to your post people keep talking about "decimals", as if the number base is at all relevant here.)

Re: JavaScript Is Weird

#310
post #51
post #19

0.2 + 0.1 === 0.3 That's not really a JS problem, that's a floating point problem. Plenty of languages will have the same issue. +!![] "" - - "" (null - 0) + "0" Calling these things weird is fair enough but I can't help thinking this is code you'd never actually write outside of the context of a "Look how weird JS is!" post. It's like picking examples from the Annual Obfuscated C Contest to show how hard it is to un…

> That's not really a JS problem, that's a floating point problem More accurately it's a binary problem. 0.1 and 0.3 have non-terminating representations in binary, so it's completely irrelevant whether you're using fixed or floating point. Any number that can be represented as the sum of powers of 2 and 5 have a terminating decimal representation, whereas only numbers that can be represented as the sum of powers of…

> 0.1 and 0.3 have non-terminating representations in binary

No. "1", "3" and "10" can all fit easily in just four bits.

Just use rational numbers and solve the problem for good.

Post reply on HN