gpt4 book ai didi

Delphi大指数扩展值字符串转换错误

转载 作者:行者123 更新时间:2023-12-03 18:21:12 25 4
gpt4 key购买 nike

我试图在 Win 7-64 上的 Delphi 7 中转换具有大指数的扩展值,并且在我认为有效的值上收到“无效浮点运算”异常。该值为 3.6854775808e-4912,调试器报告该值没有问题。

已尝试以下例程,所有例程都会生成此异常:

FloatToStr 。格式 。格式 float 。 float 到文本。

有人知道这是怎么回事吗?

编辑:

我可能应该提到,我正在缓冲区中搜索特定的扩展值,并且该位置可以位于缓冲区中的任何偏移处。该方法将 10 个字节复制到变量记录并从记录中提取扩展值。然后尝试使用上述例程之一将扩展值转换为字符串,这会生成错误。我还发现尝试将该值乘以 1.0 也会产生错误。

TDecoderRec = record
case integer of
0 : ( Binary: TBinaryArray );
1 : ( Character : TCharArray );
2 : ( ShortIntVal: ShortInt );
3 : ( ByteVal: Byte );
4 : ( SmallIntVal: SmallInt );
5 : ( WordVal: Word );
6 : ( IntegerVal: Integer );
7 : ( LongWordVal: LongWord );
8 : ( Int64Val: Int64 );
9 : ( SingleVal: Single );
10 : ( Real48Val: Real48 );
11 : ( RealVal: Real );
12 : ( DoubleVal: Double );
13 : ( CompVal: Comp );
14 : ( CurrencyVal: Currency );
15 : ( ExtendedVal: Extended );
end; { TDecoderRec }

fldExtended.AsExtended:= DecoderRec.ExtendedVal;

procedure TCustomNumericEdit.SetAsExtended( Value: Extended );
begin
AssignText( FloatToStr( Value )); { <<---- FloatToStr causes the error }
end; { TCustomNumericEdit.SetAsExtended }

这些是组成 ExtendedVal 的十六进制字节:

74 02 08 03 54 6F 70 03 A4 00

这是调试器报告的值:

3.6854775808e-4882

program ProjectFloatError;

{$APPTYPE CONSOLE}

uses
System.SysUtils;
var
Value : Extended;
s : String;
begin
try
Value.Bytes[0] := $74;
Value.Bytes[1] := $02;
Value.Bytes[2] := $08;
Value.Bytes[3] := $03;
Value.Bytes[4] := $54;
Value.Bytes[5] := $6F;
Value.Bytes[6] := $70;
Value.Bytes[7] := $03;
Value.Bytes[8] := $A4;
Value.Bytes[9] := $00;
s := FloatToStr( Value ); // <-- Invalid floating point operation
WriteLn(s);
ReadLn;
except
on E: Exception do
begin
Writeln(E.ClassName, ': ', E.Message);
ReadLn;
end;
end;
end.

最佳答案

您要转换的数字是非正态数字的示例(请参阅 https://en.wikipedia.org/wiki/Extended_precision#x86_Extended_Precision_Format )。所有有效的数字扩展值必须在最高尾数位置显式存储 1 位(这与 single 和 double 不同,其中最高位是隐含和隐藏的,但不存储)。

如果该位为零,则该数字非正常,Wiki 说:仅在 8087 和 80287 上生成。80387 及更高版本将其视为无效操作数。

对这些数字的操作过去依赖于编译器,例如BP7 输出 ?.?<000000000000E-4882 ,但任何 32+ Delphi 都会引发异常。

关于Delphi大指数扩展值字符串转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32681072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com