3
votes

I use Delphi 5 for occasional programming chores, including a nice business app for my electronics biz. I've done some graphics, database, and general other stuff with it over many years.

Today, I'm making a simulation in which I want to move pixels around based on some physics, the details are not important on the app, but I've been searching all morning and some of yesterday to figure out how to use the Square Root, and Square functions without getting bizarre answers. By inspecting a simple calculation which is assigned to an Extended variable type "vector_length", I get something like this when calculating (the numbers are fixed, but same result with any float type representing those numbers):

vector_length := Sqrt(Sqr(3.4) + Sqr(3)) 

which gives me, theoretically, about 4.53. Reasonable enough using a calculator.

Delphi's debugger shows me something like this:

2.9134839203548e-322

Which is obviously a floating point representation that makes no sense.

The code does not work like expected, so I can only think that the math is wrong or I'm doing something wrong in expecting the functions to give me something along the lines of a reasonable result??

Thanks for any input.

1
There is absolutely nothing wrong with your one-liner above. (Although it can be written more succinctly using the Hypot function.)Andreas Rejbrand
Are you seeing this value when you already stepped over that line or when you are exactly on that line?Uwe Raabe
@Uwe, I think you nailed it. See my edited answer. :) If you want to roll it back and post your own, feel free to do so and I won't be offended.Ken White
@KenWhite, never mind - I'm glad I could help.Uwe Raabe

1 Answers

5
votes

There's nothing wrong with the expression. I tested with this (Delphi 2007) - I don't have D5 around any longer, but I don't believe much (if anything) changed with Sqr or Sqrt between them:

program Project3;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;

var
  Vector_Len: Extended;

begin
  Vector_Len :=  Sqrt(Sqr(3.4) + Sqr(3));
  WriteLn('Vector_Len: ' + Format('%f', [Vector_Len]));
  ReadLn;
end.

This produced the following console output:

enter image description here

I think @Uwe is on to something, though. I think what you're seeing is not a debugger glitch; I think it's just where you decided to inspect the value. Change the code to this (this is obviously a "just for display purposes" test app, as the function would be meaningless in this context, and you obviously wouldn't use a var and then assign it immediately to Result as this function does):

program Project3;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;

function CalcVectorLen: Extended;
var
  Vector_Len: Extended;
begin
  Vector_Len :=  Sqrt(Sqr(3.4) + Sqr(3));
  Result := Vector_Len;
end;

begin
  WriteLn('Vector_Len: ' + Format('%f', [CalcVectorLen]));
  ReadLn;
end.

Set a breakpoint on the Vector_Len := Sqrt( line, and run. The debugger displays this output (your actual value may vary):

(Note: Couldn't capture the actual debugger pop-up evaluation window, but it's the same as what's marked with the arrow in the Local Variables debug watch window.)

enter image description here

What's happening is that you're inspecting the uninitialized value of Vector_Len, before the expression evaluation actually occurs and the result is assigned to the variable. If you run the app the rest of the way through, you'll see the console window still displays the correct output, though.