Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

~~ vs parseInt? [duplicate]

Tags:

javascript

Possible Duplicate:
What is the “double tilde” (~~) operator in JavaScript?

The D3 tutorial gives a function that produces a random sequence:

var t = 1297110663, // start time (seconds since epoch)
    v = 70, // start value (subscribers)
    data = d3.range(33).map(next); // starting dataset

function next() {
  return {
    time: ++t,
    value: v = ~~Math.max(10, Math.min(90, v + 10 * (Math.random() - .5)))
  };
}

Note the ~~ (tilda tilda) in:

    value: v = ~~Math.max(10, Math.min(90, v + 10 * (Math.random() - .5)))

From playing around in the javascript terminal, I see:

~~1
1
~~-1
-1
~~-1.3
-1
parseInt(5)
5
parseInt(-5)
-5
parseInt(-5.3)
-5
parseInt(5.3)
5

Since ~~ and parseInt seem to be equivalent, whats the rationale for using parseInt?

like image 399
Allyl Isocyanate Avatar asked May 31 '12 21:05

Allyl Isocyanate


People also ask

Is it better to use number or parseInt?

Hence for converting some non-numeric value to number we should always use Number() function. eg. There are various corner case to parseInt() functions as it does redix conversion, hence we should avoid using parseInt() function for coersion purposes.

What can I use instead of parseInt?

parseFloat( ) parseFloat() is quite similar to parseInt() , with two main differences. First, unlike parseInt() , parseFloat() does not take a radix as an argument. This means that string must represent a floating-point number in decimal form (radix 10), not octal (radix 8) or hexadecimal (radix 6).

What is difference between parseInt and parseFloat?

parseInt is for converting a non integer number to an int and parseFloat is for converting a non float (with out a decimal) to a float (with a decimal). If your were to get input from a user and it comes in as a string you can use the parse method to convert it to a number that you can perform calculations on.


2 Answers

They are not equivalent.

  • parseInt() turns strings into numbers, reading up to and ignoring the first non-integer character, and also possibly performing base conversion (e.g. interpreting a string as octal, or hexadecimal).

console.log(



  parseInt(011)           +'\n'+ // 9
  parseInt('011')         +'\n'+ // 11
  parseInt('42 cats')     +'\n'+ // 42
  parseInt('0xcafebabe')  +'\n'+ // 3405691582
  parseInt('deadbeef',16) +'\n'+ // 3735928559
  parseInt('deadbeef',36) +'\n'  // 1049836114599



);
  • var x = ~~y; is a 'trick'—similar to var x = y << 0;—that (ab)uses the unary bitwise NOT operator to force the result to be in the range of a signed 32-bit integer, discarding any non-integer portion.

console.log(



  parseInt(011)                +'\n'+// 9
  parseInt('011')              +'\n'+// 11
  parseInt('42 cats')          +'\n'+// 42
  parseInt('0xcafebabe')       +'\n'+// 3405691582
  parseInt('deadbeef',16)      +'\n'+// 3735928559
  parseInt('deadbeef',36)      +'\n' // 1049836114599



);

Using ~~x is often done because:

  1. It's usually faster than calling a method.
  2. It's faster to type than anything else.
  3. It makes power users feel cool because it's sort of inscrutable and also sort of justifiable. :)

As seen in the cafebabe test, numbers above 231  -1 (  2,147,483,647  ) or below  -231
(  −2,147,483,648  ) will "wrap around" due to the limits of a signed 32-bit integer.

like image 200
Phrogz Avatar answered Oct 06 '22 09:10

Phrogz


parseInt isn't limited to signed 32 bit numbers.

   // Top range for a bitwise operator provides a valid result
~~((Math.pow(2,32)/2)-1); // 2147483647

   // Beyond the top range provides undesired result
~~(Math.pow(2,32)/2); // -2147483648

Also, with parseInt you can specify the radix.

like image 22
2 revsuser1106925 Avatar answered Oct 06 '22 08:10

2 revsuser1106925