Michael Jacobson: For the conversion of IEEE floating point numbers here are two Tcl procedures to convert to and from a MIL-STD-1750A 32-bit floating number. The procedure 1750afloat take a integer value (1750a) and returns the floating point representation. The procedure float1750a take a floating point number and returns the integer representation of the 1750a value
if {0} { 1750A floating point numbers are represented as a fractional mantissa times 2 raised to the power of the exponent. The 32-bit format is: MSB LSB smmm mmmm mmmm mmmm mmmm mmmm seee eeee 0 31 } proc 1750afloat {32BitInt_1750a} { if {![string is integer $32BitInt_1750a]} { return "invalid input: \"$32BitInt_1750a\" not a integer" } set mantissa [expr {$32BitInt_1750a >> 8}] set exponent [expr {$32BitInt_1750a & 0xff}] if {[expr {$exponent & 0x80}] != 0} { set exponent [expr {int($exponent - 256)}] } set fnd_float [expr {$mantissa * pow(2, $exponent - 23)}] return $fnd_float } proc float1750a {floatnum} { if {![string is double $floatnum]} { return "invalid input: \"$floatnum\" not a double" } if {$floatnum == 0} { set 32BitInt_1750a 0 } else { # you just have to love math set exponent [expr {int(ceil(log(abs($floatnum)) / log(2.)))}] set mantissa [expr {round($floatnum / pow(2, $exponent - 23))}] #boundary condition check and repair at 0x800000 if {$mantissa == 8388608} { set mantissa [expr {$mantissa / 2}] incr exponent } set 32BitInt_1750a [expr {($mantissa << 8) | ($exponent & 0xff)}] } return $32BitInt_1750a }
# examples per MIL-STD-1750A section 4.1.5
format %X [float1750a [expr .9999999 * pow(2,127)]] ;#7FFFFF7F format %X [float1750a [expr .5 * pow(2,127)]] ;#4000007F format %X [float1750a [expr .625 * pow(2,4)]] ;#50000004 format %X [float1750a [expr .5 * pow(2,1)]] ;#40000001 format %X [float1750a [expr .5 * pow(2,0)]] ;#40000000 format %X [float1750a [expr .5 * pow(2,-1)]] ;#400000FF format %X [float1750a [expr .5 * pow(2,-128)]] ;#40000080 format %X [float1750a [expr .0 * pow(2,0)]] ;#0 format %X [float1750a [expr -1.0 * pow(2,0)]] ;#80000000 format %X [float1750a [expr -.5000001 * pow(2,-128)]] ;#BFFFFF80 format %X [float1750a [expr -.7500001 * pow(2,4)]] ;#9FFFFF04
# wrap test of the procedures
1750afloat [float1750a 1.222332] ;#1.22233200073