Jump to content
DesadaptadoDimensional

Problems making a calculator with DELPHI in RAD STUDIO. (Tan, Cos, Sin)

Recommended Posts

Hello, I am beginning to learn programming through this language. The teacher has told us that I have to make a calculator, I have managed to do the most fundamental operations, but I have problems with Sein, Cos and Tangent. 

 

The first two I'm not sure if they give an appropriate result. And the last one directly gives the program an error. 
 

 

 

 

This is the result that gave me https://i.imgur.com/LriaBJV.png and this is how my program should look according to my teacher.

 
 

 

 

I will show you my code.
 

 

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
  Vcl.Samples.Spin;

type
  Tx = class(TForm)
    PanelDos: TPanel;
    calcular: TButton;
    PanelTres: TPanel;
    Memo1: TMemo;
    ocultarhistorial: TButton;
    PanelUno: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    RadioGroup1: TRadioGroup;
    edt1: TSpinEdit;
    edt2: TSpinEdit;
    Button1: TButton;
    procedure calcularClick(Sender: TObject);
    procedure ocultarhistorialClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  x: Tx;

implementation

{$R *.dfm}

procedure Tx.Button1Click(Sender: TObject);
begin
   Memo1.Lines.Clear;
end;

procedure Tx.calcularClick(Sender: TObject);
var
num1: real;
num2: real;
resultado: real;
text: string;

begin


  num1:= strToFloat(edt1.Text);
  num2:= strToFloat(edt2.Text);


  case RadioGroup1.ItemIndex of

      0:
      begin
        resultado:= num1+num2;
        text:= 'la suma de ' + FloatToStr(num1) + ' + ' + FloatToStr(num2) + ' es ';
      end;
      1:  resultado:= num1-num2;
      2:  resultado:= num1*num2;
      3:  resultado:= num1/num2;
      4:  resultado:= abs(num1);
      5:  resultado:= sqrt(num1);
      6:  resultado:= num1*num1;
      7:  resultado:= num1*num1*num1;
      8:  resultado:= Sin(num1);
      9:  resultado:= Cos(num1);
      10: resultado:= Tan(num1);


 end;
    //showMessage('el resultado es ' + FloatToStr(resultado));
    Label4.Caption := FloatToStr(resultado);
    Memo1.Lines.Add(text+FloatToStr(resultado));








end;

procedure Tx.ocultarhistorialClick(Sender: TObject);
begin


      if Memo1.Visible = true then
          Memo1.visible := false
          else Memo1.Visible := true;


      {if Memo1.Visible = true then
      begin


          Memo1.Visible := false;


      end else
      begin

          Memo1.Visible := true;


      end;}

end;

end.

 

 

Thanks, I am stucked with it.

Edited by DesadaptadoDimensional

Share this post


Link to post

You should tell which error you get or which incorrect answer (along with the correct answer)!

Maybe you forgot that trigonometric functions take their argument expressed in RADIAN, not in DEGREE.

 

Share this post


Link to post
1 minute ago, FPiette said:

You should tell which error you get or which incorrect answer (along with the correct answer)!

Maybe you forgot that trigonometric functions take their argument expressed in RADIAN, not in DEGREE.

 

Hello, thanks for your reply.

 

      10: resultado:= Tan(num1);

 

gave me this error:

 

"Undeclared identifier: Tan at line XX"

 

Regarding radians, you are right ... In radians my calculator adjusts to the results of other online calculators. For now, I only have the problem with the Tan. I appreciate your collaboration FPiette.

Share this post


Link to post

In Delphi, trigonometric functions (along with other mathematic routines) are implemented in the module (unit) System.Math. Do not forget to add this unit manually to the uses section of your module where you use them:

uses
  System.SysUtils, (...other units), System.Math;

 

Share this post


Link to post
30 minutes ago, DesadaptadoDimensional said:

gave me this error:

 

"Undeclared identifier: Tan at line XX"

As I said, it is always better to have error messages. And this error is very clear: you are using an identifier "Tan" which is not declared. This error is always telling you that you either use something that doesn't exist (typo error) or you forgot to add a unit to the "uses" clause. You then have a look at the documentation and here you'll discover that trigonometric functions are defined in a unit name System.Math. Just add it to your uses clause.

Share this post


Link to post
22 minutes ago, Alexander Elagin said:

In Delphi, trigonometric functions (along with other mathematic routines) are implemented in the module (unit) System.Math. Do not forget to add this unit manually to the uses section of your module where you use them:


uses
  System.SysUtils, (...other units), System.Math;

 

Hello, thanks for your reply.

 

How I could add this unit manually? It is the first time I have heard about it.

 

Greetings.

 

 

@FPiette

 

Ok, thanks, I will take a look to that documentation.

Edited by DesadaptadoDimensional

Share this post


Link to post
10 minutes ago, DesadaptadoDimensional said:

How I could add this unit manually?

Just move the cursor near the top of your source file, locate the "uses" keyword and add System.Math in the list.

 

Delphi automatically add the units required for the components you drop on the form but not for other general purpose functions, data types, variables,... There are a huge number of units such as System.Math.

 

You should really learn how to use the error messages that the compiler gives to fix your code by searching in the help files (F1 key) or the documentation website http://docwiki.embarcadero.com (available from the "help" menu item in the IDE.

Edited by FPiette
  • Thanks 1

Share this post


Link to post
1 hour ago, FPiette said:

Just move the cursor near the top of your source file, locate the "uses" keyword and add System.Math in the list.

 

Delphi automatically add the units required for the components you drop on the form but not for other general purpose functions, data types, variables,... There are a huge number of units such as System.Math.

  

You should really learn how to use the error messages that the compiler gives to fix your code by searching in the help files (F1 key) or the documentation website http://docwiki.embarcadero.com (available from the "help" menu item in the IDE.

Thanks, François. We are starting to learn coding for Desktop through this language, my teacher mentioned that is easier for beginners. Now with the documentation will be easier. By the way, you have a very good website.

 

Just by curiosity, why almost all math operation worked, even Cos and Sin, without add in -uses- this System.Math in the list except Tangent?

 

Best regards.

Edited by DesadaptadoDimensional

Share this post


Link to post

Move the cursor to the appropriate word (eg sin) and wait a bit. A pop-up window will tell you in which unit it is located.

Share this post


Link to post
33 minutes ago, DesadaptadoDimensional said:

Just by curiosity, why almost all math operation worked, even Cos and Sin, without add in -uses- this System.Math in the list except Tangent?

Mainly for the historical reasons. Turbo Pascal back in 1980's implemented a set of mathematical functions as a part of its standard (default) library which was implicitly used by any project (and still is in Delphi). This set included among others sin, cos, arctan but not tan, because it is simply sinus divided by cosinus. Now, decades later, Delphi significally extended the function list, which are now implemented in a separate unit System.Math. But the original functions are still available by default without using this module.

  • Thanks 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×