为什么我将数字分配给 Double 或 Extended 变量时得到不正确的值?

Why I get incorrect value when I assign number to Double or Extended variable?

我需要给线性赋值,但是当我检查它时,结果是错误的。表达式 1/exp( 2.30258509299 * (abs(dB)/20) ) 结果是 0,063095734448(这是正确的值),但线性是 -3,6854775808e+4863,n 是 1,805186914e-307 .

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
  procedure OnCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.OnCreate;
var dB: integer;
    linear: extended;
    n: Double;
begin
  dB := -24;
  linear := 1/exp( 2.30258509299 * (abs(dB)/20) );
  n := 0.063095734448;
  showmessage(inttostr(db));
end;


end.

我做错了什么以及如何获得正确的值?

注意:为了计算表达式,我使用了 debuger "Evaluate/Modify" 命令。

很可能您启用了编译器优化,并且编译器识别出变量 linearn 已分配给但随后再也不会读取。所以编译器不需要在分配后保留这些变量。

试试这个代码

linear := 1/exp( 2.30258509299 * (abs(dB)/20) );
ShowMessage(FloatToStr(linear));