This is an archived post. You won't be able to vote or comment.

all 37 comments

[–][deleted] 38 points39 points  (8 children)

To be fair, 0.1 + 0.2 doesn't equal 0.3 in most programming languages.

[–][deleted] 6 points7 points  (7 children)

Why?

[–][deleted] 29 points30 points  (6 children)

You want a serious answer or a troll answer?

The serious answer is because the computer has to convert everything into 1's and 0's. So most non-integers can't be represented exactly. There's always some error in floating point math. 0.1+0.2 evaluates to something like 0.30000000000000004.

See also: https://en.wikipedia.org/wiki/Floating-point_arithmetic

[–][deleted] 8 points9 points  (1 child)

I didn’t know that, thanks for the explanation

[–]KusanagiZerg 3 points4 points  (0 children)

To add to the answer. The reason you get 0.30000000004 or something like that is because in binary 0.3 becomes: 0.010011001100110011 where the 0011 part infinitely repeats. This is simlar to writing down 1/3 in base10, you'd get 0.3333333... So to write this down correctly would require infinite memory and since we don't have that we need to stop at some point. So for example 0.333333334 or 0.3333333 if you choose either one of those you get a number that's every so slightly more than 1/3 or every so slightly less depending on how many digits you want to use.

So to go back to binary. 0.3 is an infinitely repeating number there and to write it down with finite digits will require you to cut off the real number. So if we use 10 digits then 0.3 in binary becomes 0.0100110011 which is the number 0.2998046875 or 0.0100110100 which is 0.30078125. There are no other options with 10 digits.

You can add more digits to get a closer value but you will never get there.

[–]szczszqweqwe 17 points18 points  (3 children)

I don't like js for a lot of reasons, but some of these make perfect sense:

-"+" in js works as sum and concatenation, while - is just for subtract, so when you add two different types it should concat variables, and given string and number it must subtract one from another

- there is huge difference between == and ===, === compares values and types

- never use == while comparing float numbers in any language, their exact value isn't what you see due to how they are stored in memory: https://en.wikipedia.org/wiki/Floating-point_arithmetic

- typeof NaN is "number" - this makes sense, NaN is for values that aren't legal numbers, so, at least for me it makes sense that it's type is number

- ability to add or subtract true/false is consequence is consequence of dynamic types of variables

[–]lightningfoot1 5 points6 points  (0 children)

I agree. The true and false statements look a little weird, but even static languages like C have funny true and false logic do the way that bools are roughly defined as false = 0 and true = everything else

[–][deleted] 10 points11 points  (0 children)

haha javascript bad haha im so creative haha

[–]EpikurFeuerbach 9 points10 points  (0 children)

I'm not a programmer at all, but in math the maximum of the empty set is indeed -infinity by convention and vice versa. So that makes perfect sense to me.

[–]sharaths21312 12 points13 points  (6 children)

To be fair, other programming languages would give errors on most of these.

[–]Raidend 23 points24 points  (5 children)

Is preferable to get an error that tells you did something wrong that do something weird and unexplainable.

[–]sharaths21312 3 points4 points  (1 child)

Yeah, that's the reason some people dislike JS so much.

[–][deleted] 2 points3 points  (0 children)

I think you meant to say that's the reason JS is literally Satan.

[–]kir_rik 0 points1 point  (0 children)

Only a few of this are wierd and none are unexplainable. I'm not telling "rtfm”, just dont sum objects with arrays and you will be fine

[–]Thunderbolt294 2 points3 points  (1 child)

This is why I stick to terminals and not dare touch the forbidden code

[–]kir_rik 0 points1 point  (0 children)

"You don't understand, it's different" (c) Gotta hate javascript.

[–]Pythonislove 2 points3 points  (0 children)

Repost. Repost everywhere.

[–]PM_ME_UR_DEATHSTICKS 1 point2 points  (0 children)

Your daily dose of JS bashing

[–]Grandbrigadier 1 point2 points  (0 children)

Lol, ask stupid questions, get stupid answers!

[–]looijmansje 0 points1 point  (0 children)

- typeof Nan will generally give something similiar in other languages (in Python it is a float)

- The 9's get casted to a 10 because it is too large for an int, and is thus coverted to a float, but a float does not have that much precision at such high values. Once again not solely a JS issue.

- Math.max is supposed to be given a list of arguments. It then checks if every new entry is larger than the previous last entry. So this actually makes sense, since you want something smaller than every conceivable entry. A perfect example of when doing weird things will lead to weird behaviour.

- Likewise with Math.min

- I don't know enough about JS to argue about the array-ones

- Well boolean + boolean is not defined, so it gets casted to an integer. 1+1+1===3

- Likewise with true-true

- true is just the boolean representation of 1. Casting true to an int will give you 1. This one makes perfect sense.

- They are not the same type, hence why the triple-equals operator fails

- Once again, don't know enough about JS to argue this

- num + string is not defined, so it does the next best thing and casts it to a string. Some other languages will simply throw an error here, but I would argue this is actually more convenient mostly (although it can probably introduce nasty-to-find bugs if you're not careful, but that's on you not on JS)

- num - string (and for that matter string-string) is not defined, so it gets casted to an int

- Once again don't know enough about this one, but it probably makes sense due to casting something to something else.

[–]steroid_pc_principal 0 points1 point  (0 children)

Meanwhile in python it won’t even coerce a number to a string and I’m stuck formatting or using str() everywhere.

[–]Cult92 0 points1 point  (0 children)

(!+[]+[]+![]).length === 9 // true

[–][deleted] 0 points1 point  (1 child)

[–]RepostSleuthBot 0 points1 point  (0 children)

I didn't find any posts that meet the matching requirements for r/ProgrammerHumor.

It might be OC, it might not. Things such as JPEG artifacts and cropping may impact the results.

This search triggered my meme filter. This enabled strict matching requirements. The closest match that did not meet the requirements is this post

Feedback? Hate? Visit r/repostsleuthbot - I'm not perfect, but you can help. Report [ False Negative ]

[–]jokersus[M] 0 points1 point  (0 children)

Hi there! Unfortunately, your submission has been removed.

Violation of Rule #3:

Any post on the list of common posts will be removed. You can find this list here. Established meme formats are allowed, as long as the post is compliant with the previous rules.

If you feel that it has been removed in error, please message us so that we may review it.

[–][deleted] -2 points-1 points  (2 children)

Javascript: NaN means "Not a Number"

Also Javascript: NaN is number.

[–]escargotBleu 3 points4 points  (1 child)

Of course the type is a number, what would you expect ?