Prev: Best jQuery Code Reviews?
Next: FAQ Topic - How do I access a property of an object using a string? (2010-06-05)
From: Dr J R Stockton on 4 Jun 2010 13:03 A variable of type Number always has a detectable sign, except when its value is NaN. Some experienced programmers, and many others, do not know how to determine this sign in all cases. Therefore I suggest a new ECMA Global Function, returning a zero value for NaN, +1 for anything else with the sign bit clear, and -1 for anything else with the sign bit set. To determine the sign of any non-NaN Number X, it seems to me that one can form X+1/X and compare with zero. The following Sign function also handles NaN. Test : Arr = [-Infinity, -1.0, 1/-Infinity, -0, NaN, +0, 1/+Infinity, +1.0, +Infinity] function Sign(X) { X += 1/X ; return (X > 0) - (X < 0) } function FF(A) { for (var J = 0 ; J < A.length ; J++) A[J] = Sign(A[J]) } FF(Arr) // NOW, Arr = [-1, -1, -1, -1, 0, +1, +1, +1, +1] -- (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links; Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc. No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News. |