# ConvertSignedInt64ToDouble

NOTE: This routine has not been thoroughly tested.

// ConvertSignedInt64ToDouble(highWord, lowWord)
// NOTE: This routine has not been thoroughly tested.
// Converts a signed 64-bit integer expressed as two 32-bit words into a double.
// The two 32-bit words are typically read into Igor using FBinRead/F=3/U.
// Double-precision floating point can represent integer values up to 2^53 precisely.
// Above that integers are only approximated. Therefore this routine supports
// values up to +/- 2^53 and returns NaN for values that are too big.
// Examples:
//  Print ConvertInt64ToDouble(0x00000000, 0x00000001)  // Plus 1
//  Print ConvertInt64ToDouble(0xFFFFFFFF, 0xFFFFFFFE)      // Minus 1 in 2's complement
Function ConvertInt64ToDouble(highWord, lowWord)
Variable highWord, lowWord

Variable result
if ((highWord & 0x80000000) == 0)
// Positive number
if ((highWord & 0xFFE0000) != 0)            // Check high order 11 bits
result = NaN
else
result = lowWord + 2^32*highWord
endif
else
// Negative number stored as 2's complement
// Theoretically the conversion would be:
//  result = 2^64 - lowWord + 2^32*highWord
//  result = -result
// However Igor calculations are done in double-precision floating point
// and the largest integer that it can represent precisely is 2^53. Thus
// we have to discard some of the high order bits to get the signed value
// into a range were we can do the conversion precisely.
if ((highWord & 0xFFE0000) != 0xFFE0000)    // Check high order 11 bits
result = NaN
else
highWord = highWord & 0x001FFFFF        // Zap 11 most significant bits
result = lowWord + 2^32*highWord
result = 2^53 - result
result = - result
endif
endif
return result
End Forum Support Gallery