Skip to content

Instantly share code, notes, and snippets.

@cruxicheiros
Last active March 27, 2020 02:10
Show Gist options
  • Select an option

  • Save cruxicheiros/cebbaf38a5f3420094b27f12c8e88234 to your computer and use it in GitHub Desktop.

Select an option

Save cruxicheiros/cebbaf38a5f3420094b27f12c8e88234 to your computer and use it in GitHub Desktop.
The refactored ternary - IEEE 754
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