Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
572 views
in Technique[技术] by (71.8m points)

algorithm - Number.sign() in javascript

Wonder if there are any nontrivial ways of finding number's sign (signum function)?
May be shorter / faster / more elegant solutions than the obvious one

var sign = number > 0 ? 1 : number < 0 ? -1 : 0;

Short answer!

Use this and you'll be safe and fast (source: moz)

if (!Math.sign) Math.sign = function(x) { return ((x > 0) - (x < 0)) || +x; };

You may want to look at performance and type-coercing comparison fiddle

Long time has passed. Further is mainly for historical reasons.


Results

For now we have these solutions:


1. Obvious and fast

function sign(x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }

1.1. Modification from kbec - one type cast less, more performant, shorter [fastest]

function sign(x) { return x ? x < 0 ? -1 : 1 : 0; }

caution: sign("0") -> 1


2. Elegant, short, not so fast [slowest]

function sign(x) { return x && x / Math.abs(x); }

caution: sign(+-Infinity) -> NaN, sign("0") -> NaN

As of Infinity is a legal number in JS this solution doesn't seem fully correct.


3. The art... but very slow [slowest]

function sign(x) { return (x > 0) - (x < 0); }

4. Using bit-shift
fast, but sign(-Infinity) -> 0

function sign(x) { return (x >> 31) + (x > 0 ? 1 : 0); }

5. Type-safe [megafast]

! Seems like browsers (especially chrome's v8) make some magic optimizations and this solution turns out to be much more performant than others, even than (1.1) despite it contains 2 extra operations and logically never can't be faster.

function sign(x) {
    return typeof x === 'number' ? x ? x < 0 ? -1 : 1 : x === x ? 0 : NaN : NaN;
}

Tools

Improvements are welcome!


[Offtopic] Accepted answer

  • Andrey Tarantsov - +100 for the art, but sadly it is about 5 times slower than the obvious approach

  • Frédéric Hamidi - somehow the most upvoted answer (for the time writing) and it's kinda cool, but it's definitely not how things should be done, imho. Also it doesn't correctly handle Infinity numbers, which are also numbers, you know.

  • kbec - is an improvement of the obvious solution. Not that revolutionary, but taking all together I consider this approach the best. Vote for him :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

More elegant version of fast solution:

var sign = number?number<0?-1:1:0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...