Created
July 29, 2020 15:07
-
-
Save omranjamal/853ce2cfb3bb9758386be6c377037807 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function toDecimal(mantissa_str, exponent_str) { | |
const [sign_str, sig_str] = mantissa_str.split('.') | |
const sign = sign_str === "1" ? -1 : 0 | |
const [ex_sign_str, ...ex_sig_arr] = exponent_str.split("") | |
const ex_sign = ex_sign_str === '1' ? -1 * Math.pow(2, exponent_str.length - 1) : 0 | |
const ex_summed = ex_sign + ex_sig_arr.reverse().map( | |
(bit, i) => bit === '1' ? Math.pow(2, i) : 0 | |
).reduce((a, b) => a + b, 0) | |
const summed = sign + sig_str.split("").map( | |
(bit, i) => bit === '1' ? Math.pow(2, -1 * (i + 1)) : 0 | |
).reduce((a, b) => a + b, 0) | |
return summed * Math.pow(2, ex_summed) | |
} | |
console.log(toDecimal("1.1111111", "100")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment