Puzzler: Hex to IEEE floating point

Monkey Forums/Monkey Programming/Puzzler: Hex to IEEE floating point

AdamRedwoods(Posted 2012) [#1]
Not sure what I'm missing here. When checked to on-line converters, the fraction part seems correct, but exponent result seems wrong.

Ideas? My eyes are tired, i may be missings omething obvious.
EDIT: fixed below, was trying too hard

Import mojo.app

Function Main()
	
	Local app:= New Game
	
End Function


Class Game Extends App

	Method OnCreate()	
	
		Local set:Int[] = [$C4214006,$BB831198,$3F800006,$4B188069]
		Local str:String[] = ["-645.0003454","-0.0039999000434","1.00000070003","9994345.0035"]
		
		For Local num:Int = 0 To 3
		
		Local b:Int = set[num]
		
		Print "test:"+str[num]
		
		Local res:Float = 0.0
		
		Local s:Int = (b & $80000000)
		Local e:Int = ((b & $7f800000) Shr 23 ) -127
		Local m:Int = b & $007fffff
		
		Local si:Float = 0.00000011920928955078125
		Local bit:Int = $00000001
		For Local i:Int=0 To 22
			
			If bit & m
				res += si
			Endif
			bit = bit Shl 1
			si += si
			
		Next
		res = res + 1.0
		
		Print "fraction: "+res
		Print "exp: "+(e)
		
		If m<>0
			If e> 0 Then res = res * Float(2 Shl e) Elseif e<0 res = res / Float(2 Shl -e)
		Else
			res =0.0
		Endif
		
		If s Then res = -res
		
		Print "float: "+res
		Print ""
		
		Next
	End

End




AdamRedwoods(Posted 2012) [#2]
duh! what was i thinking...
If m<>0
	res = res * Pow(2,e)
Else