sign-bit
Correctly returns the sign bit of a IEEE-754 number as 1 or -1, including +/-0
Last updated a year ago by bret .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install sign-bit 
SYNC missed versions from official npm registry.

sign-bit

npm travis standard

Correctly returns the sign bit of a IEEE-754 number as 1 or -1, including +/-0.

In IEEE-754, zeros are signed. In Javascript, numbers follow the IEEE-754 spec. Math.sign tries to behave like the sign function in math, but returns IEEE-754 signed zeros: tc39.github.io/ecma262/#sec-math.sign. Signed zeros are tricky do detect and work with in JS. Arguably, signed zeros and the mathematical definition of the sign don't exactly line up and there is room for confusion and ambiguity as to what the Math.sign is supposed to do in this context.

sign-bit unambiguously returns the sign bit as 1 or -1 of all Numbers in JS, including signed zeros.

Install

npm install sign-bit

Usage

var sign = require('sign-bit')

sign(3)   //  1
sign(-3)  // -1
sign(0)   //  1
sign(-0)  // -1

Math.sign(3)   //  1
Math.sign(-3)  // -1
Math.sign(0)   //  0
Math.sign(-0)  //  -0

// ¯\_(ツ)_/¯

Why?

See this thread: https://twitter.com/ydkjs/status/682225267276812288

In retrospect, I think ydkjs is wrong on the confusion here.

Contributing

Contributions welcome! Please read the contributing guidelines first.

References / Credits

License

ISC

Current Tags

  • 1.0.4                                ...           latest (a year ago)

5 Versions

  • 1.0.4                                ...           a year ago
  • 1.0.3                                ...           5 years ago
  • 1.0.2                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (3)
Dependents (2)

Copyright 2014 - 2016 © taobao.org |