2018年5月24日 星期四

delphi float double 浮點數

https://github.com/Tominator2/HEXtoIEEE-754

http://www.davdata.nl/math/floatingpoint.html

http://www.efg2.com/Lab/Mathematics/NaN.htm
http://www.efg2.com/Lab/Library/Delphi/MathInfo/index.html

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/About_Floating-Point_Arithmetic

http://rvelthuis.de/articles/articles-floats.html


IEEE standard for floating-point 754 1985 2008  

https://ieeexplore.ieee.org/document/4610935/definitions?ctx=definitions

https://en.wikipedia.org/wiki/Double-precision_floating-point_format

var
  f:Single;
  pb:  pbyte;
  pba:array[1..sizeof(Single)] of byte;
  ttb4,ttb3,ttb2, ttb1 : Byte;
  i : Integer;
  s:string;
  Lines: TStrings;
begin

  s:='';

  f := string.ToSingle(Edit1.Text);
  pb := addr(f);
  for I := 1 to SizeOf(f) do
  begin
    pba[I]:= pb^;
    inc(pb);
  end;
  I:=1;

  ttb4:= pba[SizeOf(f)];
  ttb3:= pba[SizeOf(f)-1];
  ttb2:= pba[SizeOf(f)-2];
  ttb1:= pba[SizeOf(f)-3];

  s:= s +'Sign:'+ byte(ttb4 shr 7).ToHexString+',';
  s:= s +',Exp:'+ byte((ttb4 shl 1) or (ttb3 shr 7)).ToHexString;
  s:= s+',Frac:'+ byte((ttb3 shl 1 )shr 1).ToHexString + byte(ttb2).ToHexString+ byte(ttb1).ToHexString;

  s:= s +',Full:';
  for I := SizeOf(f) downto 1 do
  begin
    s:= s +','+ pba[I].ToHexString;
  end;
  I:=1;

  //32 bits (1 for the sign 8 for the exponent, and 23 for the mantissa).

  Memo1.Lines.Add(FormatFloat('0000000.0000000',f));
  Memo1.Lines.Add(s);
  Memo1.Lines.Add(
    '[Sign:'+ f.Sign.ToString+
    '][Exp:'+ word(f.Exp).ToHexString+
    '][Frac:'+ f.Frac.ToHexString
    +']');
  Memo1.Lines.Add('Exponent:'+ f.Exponent.ToHexString);
  Memo1.Lines.Add('Mantissa:'+ f.Mantissa.ToHexString);