Skip to content

Instantly share code, notes, and snippets.

@Zubnix
Last active November 12, 2017 22:10
Show Gist options
  • Save Zubnix/09724b5eb13e015263e034c3d92a2d87 to your computer and use it in GitHub Desktop.
Save Zubnix/09724b5eb13e015263e034c3d92a2d87 to your computer and use it in GitHub Desktop.
const number = -1234.56
console.log('original ' + number)
console.log('original as hex ' + number.toString(16))
// integer part
const integerPart = (number >> 0).toString(16) // 32bit integer
console.log('integer ' + integerPart)
// fractal part
const asHex = number.toString(16)
const decimalIndex = asHex.indexOf('.')
const fractalPart = asHex.substring(decimalIndex + 1, decimalIndex + 3)
console.log('fractal ' + fractalPart)
const rawFixed = (~~parseInt(integerPart + fractalPart, 16)) >> 0
console.log('fixed ' + rawFixed.toString(16))
const rawHex = rawFixed.toString(16)
const integerPart1 = rawHex.substring(0, rawHex.length - 2)
const fractalPart1 = rawHex.slice(rawHex.length - 2)
const result = parseFloat(~~parseInt(integerPart1, 16).toString(10) + '.' + parseInt(fractalPart1, 16).toString(10))
console.log('reconstructed ' + result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment