Last active
March 27, 2020 02:10
-
-
Save cruxicheiros/cebbaf38a5f3420094b27f12c8e88234 to your computer and use it in GitHub Desktop.
The refactored ternary - IEEE 754
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 isSpecialCase(exponent, bias) { | |
| return exponent == (bias << 1) + 1 | |
| } | |
| function handleSpecialCases(significand, signal) { | |
| if (significand) { | |
| return NaN; | |
| } else if (signal) { | |
| return -Infinity; | |
| } else { | |
| return +Infinity; | |
| } | |
| } | |
| function calculateNumberFromComponents(exponent, bias, significand) { | |
| if (exponent) { | |
| return Math.pow(2, exponent - bias) * (1 + significand); | |
| } else { | |
| return Math.pow(2, -bias + 1) * significand; | |
| } | |
| } | |
| function applySignalToAbsoluteValue(signal, absoluteValue) { | |
| return absoluteValue * (1 + signal * (-2)); | |
| } | |
| function convertIEEE754ToNumber(bias, significand, signal, exponent) { | |
| if (isSpecialCase(exponent, bias)) { | |
| return handleSpecialCases(significand, signal); | |
| } else { | |
| let absoluteValue = 0; | |
| if (exponent || significand) { | |
| absoluteValue = calculateNumberFromComponents(exponent, bias, significand); | |
| } | |
| return applySignalToAbsoluteValue(signal, absoluteValue); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment