Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. const db = require('double-bits');
  2. const pad = require('pad');
  3.  
  4. // [lo, hi] where lo is a 32 bit integer and hi is a 20 bit integer.
  5. const base2Str = (n) => {
  6. const f = db.fraction(n);
  7. const s = db.sign(n) ? '-' : '';
  8. const e = `2^${db.exponent(n) + 1}`;
  9. const t = `0.${pad(f[1].toString(2), 20, '0')}${pad(f[0].toString(2), 32, '0')}`;
  10. return `${s}${e} * ${t}`;
  11. };
  12.  
  13. console.log(base2Str(0.1).toString(2));
  14. console.log(base2Str(0.2).toString(2));
  15. console.log(base2Str(0.3).toString(2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement