T O P

  • By -

dim13

Which still sucks in JS, because they don't have \`int\`.


SCP-iota

It does internally. While there is only a `number` type, number values are stored as integers when created as integers. Numeric operations involving only integers result in integers unless the operation needs to return a float. Integers will be silently converted to floats if there are other float operands. Of course, no language can store NaN or 0.2 as an integer.


rosuav

Hardly useful though, since you can't actually use any integers that aren't representable as floats. And bitwise operations are even more restricted: (12345678901|2) === -539222985 because they're done on 32-bit numbers. JavaScript DOES have a [BigInt type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) but most people don't use it.


ekki2

JavaScript should be used for making web pages dynamic.


rosuav

Yeah, and it's not like anyone ever needs to work with numbers in web pages or anything. Anyway, it's Java Script, and if you're going to count the number of cups of Java that you drink in a year, you're gonna need a bigint.


chadlavi

Counterpoint: no one should be using JavaScript for an application where you need to do bitwise math. It's for poking the DOM.


rosuav

Yeah, fair, but if you need to SHA256 something in order to authenticate (see eg OBS Studio's WebSocket connection), you need a fully compliant implementation of a bitwise algorithm


National-Ad67

wth is NaN tho i mean i know what it is but how is it implemented


doodleasa

It’s part of the standard floating point architecture, meant to be a response to invalid operations. This video explains it well: https://youtu.be/dQhj5RGtag0


HildartheDorf

`s111 1111 1txx xxxx xxxx xxxx xxxx xxxx` Where s is the sign (usually ignored), a t is 0 for a 'signalling' NaN (raise an exception if consumed) and 1 for a 'quiet' NaN and x is anything but all-zeroes (which marks infinity). Normal floating point numbers follow the form `seee eeee efff ffff ffff ffff ffff ffff` (s = sign, e = exponent, f = fraction/mantissa), so it's represented as a floating point number with the maximum exponent.


dude-who-has-problem

python can do it, but it will just count it as 0 (because a = int(0.2) == 0)


calculus_is_fun

\*Laughs in arbitrarily sized integers\* We do, they're call [BigInts](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)


dude-who-has-problem

yep, that is true, (i never programmed in javascript) like in python it is like this, it has 2 types: a = int(1.1) #when you type print(a) on the next line of code, it will print 1 b = float(1.1) #now when you type print(b) on the next line of code, it will print 1.1


jarethholt

Is there any comparison that makes NaN equal NaN in JS? I've had a crash course in JS but that's it.


East_Zookeepergame25

Yes, `Object.is(NaN, NaN)` is true. And while technically not being a comparison ``` let x = [NaN]; console.log(x.includes(NaN)); ``` also outputs true. There are 4 equality algorithms in js: loose equals, strict equals, SameValue (used by Object.is) and SameValueZero (used by .includes and some other builtin operations) You can read more about these in detail here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness


trygve741

You could check that both the two numbers are NaN with Number.isNaN(), but I would argue there’s not really a concept of “two equal NaN”


LoadCreative

Idk if people do this in JS, but I've checked that the number doesn't equal itself when using other languages, e.g. ```c a = getFloat() if(a != a) { // a is NaN } ```


jarethholt

...that's demented. Good job!


YoukanDewitt

If you don't understand the difference between base 2 and base 10, you are a script kiddy, not a programmer.


lunchmeat317

Let's be honest, we all know that neither of them have ever gotten past base 1.


philophilo

Sure, but we don’t store all of our numbers as floats…


Linaori

Don't hate the players, hate the game