这个要怎么计算得到FLOAT数据?求大神帮助

mz941 2017-12-14 09:17:07

...全文
534 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mz941 2017-12-21
  • 打赏
  • 举报
回复
strExponent:=''; strMantissa:=''; strExponentComplement:=''; strMantissaComplement:=''; 已经解决, 计算前先清空,防止运算结果叠加
mz941 2017-12-21
  • 打赏
  • 举报
回复
@Bzdr 这个代码使用后 怎么只能得到一个数据?是不是哪个地方使用错误了?谢谢 procedure TForm1.Button48Click(Sender: TObject); function BintoInt(Value:String):LongInt; var i,Size:Integer; begin Result:=0; Size:=Length(Value); for i:=Size downto 1 do begin if Copy(Value,i,1)='1' then begin Result:=Result+(1 shl (Size-i)); end; end; end; var Value_Group,StrL_NuOBSValue_Group:TStringList; i,i_NuOBSValue,iExponent,iMantissa:Integer; // temp:Int64; strBinValue,strExponent,strMantissa:string; strExponentComplement,strMantissaComplement:string; Str_NuOBSValue,FLOATType_Value:string; begin StrL_NuOBSValue_Group := TStringList.Create; Value_Group := TStringList.Create; StrL_NuOBSValue_Group.Add('41 82 00 01 0A A0 00 00 00 73'); StrL_NuOBSValue_Group.Add('4B 6C 00 00 17 A0 FE 00 0D FE'); StrL_NuOBSValue_Group.Add('4B B8 00 00 02 20 FF 00 03 E8'); StrL_NuOBSValue_Group.Add('4B B0 00 00 02 00 FE 00 00 32'); for i_NuOBSValue := 0 to StrL_NuOBSValue_Group.Count - 1 do //i_NuOBSValue±ÜÃâÔÚ½âÎö FLOATType_Valueʱ Óë½âÎö´úÂë¶ÎÖеÄiÖظ´ begin Str_NuOBSValue := trim(StrL_NuOBSValue_Group.strings[i_NuOBSValue]); //temp:=StrToInt64('$'+Edit19.Text); temp:=StrToInt64('$'+trim(StringReplace(copy(Str_NuOBSValue,19,11),' ','',[rfReplaceAll]))); //--Get Bin Value for i:=32-1 downto 0 do begin if temp and (1 shl i)<>0 then begin strBinValue:=strBinValue+'1'; end else begin strBinValue:=strBinValue+'0'; end; end; //--Get exponent,mantissa string strExponent:=Copy(strBinValue,1,8); strMantissa:=Copy(strBinValue,9,24); //--2's complement form if Copy(strExponent,1,1)='1' then begin for i:=1 to Length(strExponent) do begin if Copy(strExponent,i,1)='1' then begin strExponentComplement:=strExponentComplement+'0'; end else begin strExponentComplement:=strExponentComplement+'1'; end; end; iExponent:=0-(BintoInt(strExponentComplement)+1); end else begin iExponent:=(BintoInt(strExponent)); end; if Copy(strMantissa,1,1)='1' then begin for i:=1 to Length(strMantissa) do begin if Copy(strMantissa,i,1)='1' then begin strMantissaComplement:=strMantissaComplement+'0'; end else begin strMantissaComplement:=strMantissaComplement+'1'; end; end; iMantissa:=0-(BintoInt(strMantissaComplement)+1); end else begin iMantissa:=BintoInt(strMantissa); end; case iMantissa of //$7FFFFF:ShowMessage('Not a Number'); //$800000:ShowMessage('Not at this resolution'); //$7FFFFE:ShowMessage('+INFINITY'); //$800002:ShowMessage('-INFINITY'); $7FFFFF:FLOATType_Value := ''; $800000:FLOATType_Value := ''; $7FFFFE:FLOATType_Value := ''; $800002:FLOATType_Value := ''; else if iExponent < 0 then begin FLOATType_Value := FormatFloat('0.0',StrToFloat(FloatToStr(Power(10,iExponent) * iMantissa))); Value_Group.Add(FLOATType_Value); end; if iExponent >= 0 then begin FLOATType_Value := FloatToStr(Power(10,iExponent) * iMantissa); Value_Group.Add(FLOATType_Value); end; // FE000E31 :36.33 FE000DFE :35.82 FE000DF8 : 35.76 end; end; for i := 0 to Value_Group.Count - 1 do //-------------****************************** begin Memo7.Lines.Add(Value_Group.strings[i]); end; Value_Group.free; StrL_NuOBSValue_Group.free; end;
Mr Dang 2017-12-18
  • 打赏
  • 举报
回复

procedure TForm1.btn1Click(Sender: TObject);
  function BintoInt(Value:String):LongInt;
  var
      i,Size:Integer;
  begin
      Result:=0;
      Size:=Length(Value);
      for i:=Size downto 1 do
      begin
          if Copy(Value,i,1)='1'   then
          begin
              Result:=Result+(1 shl (Size-i));
          end;
      end;
  end;
var
    i,iExponent,iMantissa:Integer;
    temp:Int64;
    strBinValue,strExponent,strMantissa:string;
    strExponentComplement,strMantissaComplement:string;
begin
    temp:=StrToInt64('$'+edt1.Text);
    //--Get Bin Value
    for i:=32-1 downto 0 do
    begin
        if temp and (1 shl i)<>0 then
        begin
            strBinValue:=strBinValue+'1';
        end else
        begin
            strBinValue:=strBinValue+'0';
        end;
    end;
    //--Get exponent,mantissa string
    strExponent:=Copy(strBinValue,1,8);
    strMantissa:=Copy(strBinValue,9,24);
    //--2's complement form
    if Copy(strExponent,1,1)='1' then
    begin
        for i:=1 to Length(strExponent) do
        begin
            if Copy(strExponent,i,1)='1' then
            begin
                strExponentComplement:=strExponentComplement+'0';
            end else
            begin
                strExponentComplement:=strExponentComplement+'1';
            end;
        end;
        iExponent:=0-(BintoInt(strExponentComplement)+1);
    end else
    begin
        iExponent:=(BintoInt(strExponent));
    end;
    if Copy(strMantissa,1,1)='1' then
    begin
        for i:=1 to Length(strMantissa) do
        begin
            if Copy(strMantissa,i,1)='1' then
            begin
                strMantissaComplement:=strMantissaComplement+'0';
            end else
            begin
                strMantissaComplement:=strMantissaComplement+'1';
            end;
        end;
        iMantissa:=0-(BintoInt(strMantissaComplement)+1);
    end else
    begin
        iMantissa:=BintoInt(strMantissa);
    end;
    case iMantissa of
    $7FFFFF:ShowMessage('Not a Number');
    $800000:ShowMessage('Not at this resolution');
    $7FFFFE:ShowMessage('+INFINITY');
    $800002:ShowMessage('-INFINITY');
    else
            ShowMessage(FloatToStr(Power(10,iExponent) * iMantissa));
    end;
end;
00FFFFFB 这个值应该是 -5
Mr Dang 2017-12-18
  • 打赏
  • 举报
回复
对于 00FFFFFB 代码里面要改下 ,要判断一下高位是否为1,就像 Exponent这个值一样
  • 打赏
  • 举报
回复
他文章里给出的格式是错误的,应该是1个符号位、8个指数位、23个尾数位。
mz941 2017-12-17
  • 打赏
  • 举报
回复
@Bzdr 00FFFFFB 这个value 是我从下位机接收过来的数据 下位机对应显示的是个负数 。 大概在-2到 -4 之间的
mz941 2017-12-17
  • 打赏
  • 举报
回复
@Bzdr 如果 FLOAT-Type 的实际值是负数,怎么办?改天我去接收几组负数的 FLOAT-Type放上来,谢谢
Mr Dang 2017-12-16
  • 打赏
  • 举报
回复

procedure TForm1.btn1Click(Sender: TObject);
  function BintoInt(Value:String):LongInt;
  var
      i,Size:Integer;
  begin
      Result:=0;
      Size:=Length(Value);
      for i:=Size downto 1 do
      begin
          if Copy(Value,i,1)='1'   then
          begin
              Result:=Result+(1 shl (Size-i));
          end;
      end;
  end;
var
    i,iExponent,iMantissa:Integer;
    temp:Int64;
    strBinValue,strExponent,strMantissa:string;
    strExponentComplement,strMantissaComplement:string;
begin
    temp:=StrToInt64('$'+edt1.Text);
    //--Get Bin Value
    for i:=32-1 downto 0 do
    begin
        if temp and (1 shl i)<>0 then
        begin
            strBinValue:=strBinValue+'1';
        end else
        begin
            strBinValue:=strBinValue+'0';
        end;
    end;
    //--Get exponent,mantissa string
    strExponent:=Copy(strBinValue,1,8);
    strMantissa:=Copy(strBinValue,9,24);
    //--2's complement form
    if Copy(strExponent,1,1)='1' then
    begin
        for i:=1 to Length(strExponent) do
        begin
            if Copy(strExponent,i,1)='1' then
            begin
                strExponentComplement:=strExponentComplement+'0';
            end else
            begin
                strExponentComplement:=strExponentComplement+'1';
            end;
        end;
        iExponent:=0-(BintoInt(strExponentComplement)+1);
    end else
    begin
        iExponent:=(BintoInt(strExponent));
    end;
    iMantissa:=BintoInt(strMantissa);
    case iMantissa of
    $7FFFFF:ShowMessage('Not a Number');
    $800000:ShowMessage('Not at this resolution');
    $7FFFFE:ShowMessage('+INFINITY');
    $800002:ShowMessage('-INFINITY');
    else
            ShowMessage(FloatToStr(Power(10,iExponent) * iMantissa));
    end;
end;
mz941 2017-12-16
  • 打赏
  • 举报
回复
非常感谢
mz941 2017-12-15
  • 打赏
  • 举报
回复
@a295281315 有空的时候给补个全代码。谢谢。我还在初学中
Mr Dang 2017-12-15
  • 打赏
  • 举报
回复

procedure TForm1.btn1Click(Sender: TObject);
var
    i:Integer;
    temp:Int64;
    Str_BinValue,Str_Exponent,Str_Mantissa:string;
begin
    temp:=StrToInt64('$'+edt1.Text);
    //--Get Bin Value
    for i:=0 to 32-1 do
    begin
        if temp and (1 shl i)<>0 then
        begin
           Str_BinValue:=Str_BinValue+'1';
        end else
        begin
           Str_BinValue:=Str_BinValue+'0';
        end;
    end;
    //--Get exponent,mantissa string
    Str_Exponent:=Copy(Str_BinValue,1,8);
    Str_Mantissa:=Copy(Str_BinValue,9,24);
    //--2's complement form  (2的补码)

    //--转换成整数

    //--判断exponent大于还是小于0

    //--(mantissa) * 10 的 exponent
end;
大概是这样 ,下班了 不写了
  • 打赏
  • 举报
回复
就是Delphi中的single类型,single单精度浮点,double双精度浮点
mz941 2017-12-14
  • 打赏
  • 举报
回复
补充一下: Basic Data Types The C data types defined here make use of the following basic types: u_8 unsigned 8 bit wide integer u_16 unsigned 16 bit wide integer u_32 unsigned 32 bit wide integer i_8 signed 8 bit wide integer i_16 signed 16 bit wide integer i_32 signed 32 bit wide integer The mapping of these types to data types used in a Computer Client application is machine specific and compiler dependent.

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧