Johny Bony
Johny Bony

Reputation: 373

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

I need to assign value to linear, but then when I check it the result is wrong. The expression 1/exp( 2.30258509299 * (abs(dB)/20) ) result is 0,063095734448 (which is correct value) but the linear is -3,6854775808e+4863 and n is 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.

What am I doing wrong and how to get the correct value?

Notice: For evaluation of the expression I used debuger "Evaluate/Modify" command.

enter image description here

Upvotes: 0

Views: 267

Answers (1)

David Heffernan
David Heffernan

Reputation: 612854

Most likely you have compiler optimisations enabled and the compiler recognises that the variables linear and n are assigned to but then never subsequently read from. So the compiler doesn't need to retain those variables after they have been assigned.

Try this code

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

Upvotes: 5

Related Questions