diff --git a/src/shardware.adb b/src/shardware.adb index 4aef910..d112020 100644 --- a/src/shardware.adb +++ b/src/shardware.adb @@ -82,15 +82,56 @@ package body Shardware is return UnToSignedInt(BytesToU(TheBytes)); end BytesToI; - function BytesToF (TheBytes : ByteArr) return Float is + function BytesToF (TheBytes : ByteArr) return Float is + Zero : Float := 0.0; Result : Float := 0.0; TheBits : BitArr (1 .. 32); BitBuff : BitArr (1 .. 32); Mantissa : Float := 0.0; + PowerOfTen : Integer := 0; + Exponent : Natural := 0; + Sign : Float := 1.0; begin TheBits := BytesToBits(TheBytes); - --BitsToBytes(BitBuff); + if TheBits (1) then + Sign := -1.0; + else + Sign := 1.0; + end if; + + for i in 1 .. 32 loop + if i < 10 then + null; + else + if TheBits (i) then + Mantissa := Mantissa + 2.0 ** (-1 * (i - 9)); + end if; + end if; + end loop; + + for i in 1 .. 32 loop + if i < 25 then + BitBuff (i) := False; + else + BitBuff (i) := TheBits (i - 23); + end if; + end loop; + + Exponent := Natural(BytesToU(BitsToBytes(BitBuff))); + + if Exponent > 0 and Exponent < 254 then + Mantissa := Mantissa + 1.0; + Result := Sign * Mantissa * (2.0 ** (Exponent - 127)); + elsif Exponent = 255 and Mantissa = 0.0 then + Result := 1.0 / Zero; + elsif Exponent = 0 and (Mantissa > 0.0 or Mantissa < 0.0) then + Result := Sign * Mantissa * (2.0 ** (Exponent - 127)); + elsif Exponent = 0 and Mantissa = 0.0 then + Result := 0.0; + else + Result := 0.0 / Zero; + end if; return Result; end BytesToF;