简体   繁体   中英

Javascript function to get the difference between two numbers

我想要一个简单的 Javascript 函数来获取两个数字之间的差异,这样foo(2, 3)foo(3,2 ) 将返回相同的差异 1。

var difference = function (a, b) { return Math.abs(a - b); }

Here is a simple function

function diff (num1, num2) {
  if (num1 > num2) {
    return num1 - num2
  } else {
    return num2 - num1
  }
}

And as a shorter, one-line, single-argument, ternary-using arrow function

function diff (a, b) => a > b ? a - b : b - a

Seems odd to define a whole new function just to not have to put a minus sign instead of a comma when you call it:

Math.abs(a - b);

vs

difference(a, b);

(with difference calling another function you defined to call that returns the output of the first code example). I'd just use the built in abs method on the Math object.

It means you want to return absolute value.

function foo(num1 , num2) {
   return Math.abs(num1-num2);
} 
function difference(n, m){
    return Math.abs(n - m)
}

In TypeScript , if anyone interested:

public getDiff(value: number, oldValue: number) {
    return value > oldValue ? value - oldValue : oldValue - value;
}

I don't get how this is possible on such a simingly common question, but all the answers I found here are wrong in certain cases.

If both numbers are on the same side of zero then the accepted answer is right, but if the numbers are not on the same side of zero, then their absolute values must be added, not subtracted.

function diff( x, y ) {

    if ( Math.sign( x ) === Math.sign( y ) ) {

        return Math.abs( x - y );

    } else {

        return Math.abs( x ) + Math.abs( y );

    };

};

diff( 2, 2 ) // 0
diff( -2, -2 ) // 0
diff( -2, 2 ) // 4
diff( 2, -2 ) // 4

Solution on math.stackexchange.com : https://math.stackexchange.com/a/1893992

defining four functions I made a little benchmark, surprisingly

 fn1 = (a,b)=>(a>b)?ab:ba; fn2 = (a,b)=>{let c= ab;return c>0?c:-c;} fn3 = (a,b)=>Math.abs(ab) fn4 = (a,b)=>Math.max(ab, ba) for(let fn of [ fn1, fn2, fn3, fn4]) executionTime(()=>{console.log(fn);for(let i=0;i<1_000_000;i++)fn(500_000,i)}) function executionTime(cb){//calculate performance const t0 = performance.now(); cb(); const t1 = performance.now(); console.log(`cb took ${t1 - t0} milliseconds.`); }

Here are the results for 1million iterations:

  1. cb took 11.129999998956919 milliseconds.
  2. cb took 53.420000011101365 milliseconds.
  3. cb took 47.40000003948808 milliseconds.
  4. cb took 48.20000007748604 milliseconds.

But unfortunalelly those are false results, I mean executing one by one gave on the console resulting:

  1. 6.9ms 11.7 7.0 4.4
  2. 7.5ms 4.4 6.4 4.5
  3. 7.8ms 5.1 6.2 6.8
  4. 3.9ms 6.3 5.2 5.2

I think while looping, after each iteration garbage is collecting while other functions doing their bests. While executing each one by one, fn2 came second, while using extra variable( vm is optimizing with register ? ) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM