Re: Delphi Math Unit -- Books


[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]

Posted by webmaster Guido on December 16, 2002 at 03:22:50:

In Reply to: Delphi Math Unit -- Books posted by p12219 on December 16, 2002 at 01:31:19:

: Anyone know of a book telling how to use the Delphi math unit's with examples. I work with Trig, Angles, Bearings, Sin, Arcsins, Tan, etc.. I have purchased several books on Delphi starting with Delphi 2, delphi 3, Delphi 5 and Delphi 6, now I see Delphi 7 books being advertised. I am tired of buying "How to books" only to find out that they avoid all mention of the Math Unit or Math applications. I am a past Turbo Pascal user and back in the old days(-; you could find many source codes and math examples. What happened?---If you know of a book source, I would appreciate hearing from you.
------------------------

Sorry, I don't recall any specific books, but here's a very useful source on the web: efg's Computer Lab: Delphi Math Info.

Some basic info for those not familiar with Delphi's MATH unit:

Starting with Delphi 4, every version has the Math unit (it was not included in the Standard edition of D2 nor D3, only the Professional and Client/Server versions had the Math unit).

Some math functions like Sin() and Cos() are in the System unit, others like Tan() and the important DegToRad are in the Math-unit. DegToRad = "Degrees To Radians" is needed because the trigonometric functions require the angles expressed in radians; for a small example, see the end of this post.

The Math-unit isn't included by default in the template of a new form, so you have to add it yourself to the "uses" directive on top of the unit. For example, if it says:

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

then you have to change it to:

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, MATH;

----------------------------------------


A simple example of code that needs the MATH unit:
Enter degrees, minutes and seconds of an angle in 3 edit boxes, and display the sine and the cosine of the angle. Of course, in real life you have to do some error checking on what is entered into the TEdit boxes.

procedure TForm1.btnCalculateClick(Sender: TObject);
var
Deg, Min, Sec: integer;
D, R, S, C: real;
begin
Deg := StrToInt(editDeg.Text);
Min := StrToInt(editMin.Text);
Sec := StrToInt(editSec.Text);
D := Deg + Min / 60 + Sec / 3600; // degrees plus decimal fraction
R := DegToRad(D); // convert degrees to radians
S := Sin(R);
C := Cos(R);
labelDegDec.Caption := FloatToStr(D);
labelRad.Caption := FloatToStr(R);
labelSin.Caption := FloatToStr(S);
labelCos.Caption := FloatToStr(C);
end;



Related Articles and Replies:


[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]