Re: Delphi Pro 5 Math Unit

[ Related Articles and Replies ] [ DelphiLand Discussion Forum ]

Posted by webmaster Guido on April 09, 2002 at 19:51:36:

In Reply to: Delphi Pro 5 Math Unit posted by Lionel Joyner p12219 on March 27, 2002 at 18:28:34:

: Need help with the Delphi Pro 5 MATH UNIT. I understand that it exist but have not found any books or anyone that can tell me how to set it up and use it.
: Anyone that knows how to do the following, I would appreciate your help.
: Given an angle then display the Sin and Cos.
: (1)Enter an angle in Deg, Min, Sec.
: (2)Display Angle in whole deg and decimals of a deg.
: (3)Display Degrees to Radians.
: (4)Display the Sin and Cos.


For the functions Sin() and Cos you don't need the Math-unit. But because for all trigonometric functions you must expressed the angles in radians, you need the function DegToRad(), and that one is in the 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). Attention: 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:

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

then you have to change it to:

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

Below is some code to play with. The degrees, minutes and seconds are entered in the TEdit-boxes editDeg, editMin and editSec. Note that I omitted all error checking just to make the code clearer, but in a real application you have to check if the edit-boxes contain valid integer values.

procedure TForm1.btnCalculateClick(Sender: TObject);
  Deg, Min, Sec: integer;
  D, R, S, C: real;
  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);

Related Articles and Replies:

[ Related Articles and Replies ] [ DelphiLand Discussion Forum ]