Posts

Showing posts with the label jsint

What are integers in JavaScript?

According to the ECMAScript specification, all numbers in JavaScript are floating-point. Yet, the notion of integer comes up occasionally. This blog post explains what it means. What are integers? JavaScript has only floating-point numbers. Integers appear internally in two ways. First, most JavaScript engines store a small enough number without a decimal fraction as an integer (with, for example, 31 bits) and maintain that representation as long as possible. They have to switch back to a floating point representation if a number’s magnitude grows too large or if a decimal fraction appears. Second, the ECMAScript specification has integer operators: namely, all of the bitwise operators. Those operators convert their operands to 32-bit integers and return 32-bit integers. For the specification, integer only means that the numbers don’t have a decimal fraction, and 32-bit means that they are within a certain range. For engines, 32-bit integer means that an actual integer (non-floatin...

Binary bitwise operators in JavaScript

This blog post describes JavaScript’s binary bitwise operators: bitwise And ( & ), bitwise Or ( | ), and bitwise Xor ( ^ ). These operators work with 32-bit integers. That is, they convert their operands to 32-bit integers and produce a result that is a 32-bit integer (encoded as a floating point number, at least externally). Inputting and outputting binary numbers In the following examples, we work with binary numbers, via the following two operations: parseInt(str, 2) parses a string str in binary notation (base 2). For example: > parseInt('110', 2) 6 num.toString(2) converts the number num to a string in binary notation. For example: > 6..toString(2) '110' Binary bitwise operators Bitwise And: number1 & number2 > (parseInt('11001010', 2) & parseInt('1111', 2)).toString(2) '1010' Bitwise Or: number1 | number2 > (parseInt('11001010', 2) | parseInt('1111', 2)).toString(2) ...

Safe integers in JavaScript

Update 2014-02-08: Follow-up blog post “ What are integers in JavaScript? ” JavaScript can only safely represent integers i in the range −2 53 < i < 2 53 . This blog post examines why that is and what “safely represent” means. It is based on an email by Mark S. Miller to the es-discuss mailing list. Safe integers The idea of a safe integer is about how mathematical integers are represented in JavaScript. In the range (−2 53 , 2 53 ) (excluding the lower and upper bounds), JavaScript integers are safe : there is a one-to-one mapping between mathematical integers and their representations in JavaScript. Beyond this range, JavaScript integers are unsafe : two or more mathematical integers are represented as the same JavaScript integer. For example, starting at 2 53 , JavaScript can only represent every second mathematical integer. > Math.pow(2, 53) 9007199254740992 > Math.pow(2, 53)+1 9007199254740992 > Math.pow(2, 53)+2 9007199254740994 ...

parseInt() doesn’t always correctly convert to integer

In JavaScript, all numbers are floating point. Integers are floating point numbers without a fraction. Converting a number n to an integer means finding the integer that is “closest” to n (where “closest” is a matter of definition). You might think that parseInt() performs this task, but it doesn’t. At least, not always. How parseInt() works parseInt() has the following signature: parseInt(value, radix?) It converts value to string, ignores leading whitespace and then parses as many consecutive integer digits as it can find. The radix If radix is missing then it is assumed to be 10 except if value begins with '0x' or '0X' , in which case radix is set to 16 (hexadecimal). > parseInt('0xA') 10 If radix is already 16 then the hexadecimal prefix is optional. > parseInt('0xA', 16) 10 > parseInt('A', 16) 10 So far we have described the behavior of parseInt() according to the ECMAScript specification . Add...