Created
September 27, 2016 15:59
-
-
Save barretlee/f8227c1bce1570121ea25964e299557d to your computer and use it in GitHub Desktop.
double-bits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const db = require('double-bits'); | |
const pad = require('pad'); | |
// [lo, hi] where lo is a 32 bit integer and hi is a 20 bit integer. | |
const base2Str = (n) => { | |
const f = db.fraction(n); | |
const s = db.sign(n) ? '-' : ''; | |
const e = `2^${db.exponent(n) + 1}`; | |
const t = `0.${pad(f[1].toString(2), 20, '0')}${pad(f[0].toString(2), 32, '0')}`; | |
return `${s}${e} * ${t}`; | |
}; | |
console.log(base2Str(0.1).toString(2)); | |
console.log(base2Str(0.2).toString(2)); | |
console.log(base2Str(0.3).toString(2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment