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

Joh*_*ony 0 delphi double variable-assignment

我需要将值分配给线性,但是当我检查它时,结果是错误的。表达式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.
Run Code Online (Sandbox Code Playgroud)

我在做什么错了,如何获得正确的价值?

注意:为了评估表达式,我使用了调试器的“评估/修改”命令。

在此处输入图片说明

Dav*_*nan 5

很可能您已启用编译器优化,并且编译器会识别出该变量,linearn已分配给该变量,但随后从未读取过。因此,在分配这些变量后,编译器无需保留这些变量。

试试这个代码

linear := 1/exp( 2.30258509299 * (abs(dB)/20) );
ShowMessage(FloatToStr(linear));
Run Code Online (Sandbox Code Playgroud)