역삼각함수란 삼각함수의 역함수를 의미하고,

역쌍곡선함수란 쌍곡선함수의 역함수를 의미한다.

수학에서 sin 함수의 역함수는 arcsin 으로 표기되는데,

FreePascal 언어에서는 asin 함수로 구현되어 있다.

FreePascal 은 http://www.freepascal.org 에서 구할 수  있다.

아래의 소스는 FreePascal 의 (명령줄 컴파일 명령) fpc 로 컴파일되는 소스이다.

 

참고 1. FreePascal 은 이전(MS-DOS 시절)에 많이 쓰였던 Turbo Pascal 과 유사하며, Turbo Pascal을 발전시킨 것이라고 보변 된다. 아래는 Wikipedia 에서 적어 놓은 FreePascal 의 설명이다.

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

Free Pascal Compiler (FPC for short, and formerly known as FPK Pascal) is a free Pascal and Object Pascal compiler.

In addition to its own Object Pascal dialect, Free Pascal supports, to varying degrees, the dialects of several other compilers, including those of Turbo Pascal, Delphi, and some historical Macintosh compilers. The dialect is selected on a per-unit (module) basis, and more than one dialect can be used to produce a single program.

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

 

참고 2.  FreePascal 은 대소 문자 구분을 하지 않는다.

참고 3. uses 블럭에 sysutilsv 를 추가 한 것은 부동소수점수를 출력할시 작당한 반올림을 하여 출력하기 우함이다.

참고 4. 자연로그 함수로 다른 프로그램 언어에서는 보통 log 또는 Log 라는 이름으로 구현되어 있지만, FreePascal 에서는 ln 이라는 이름으로 구현되어 있다.

참고 5.  쌍곡선함수 sinh, cosh 의 역함수들이 각각 arsinh, arcosh 라는 이름으로  FreePascal 에 이미 구현되어 있지만, 비교를 위해 아래의 소스에 asinh, acosh 라는 이름의 함수로 구현해 놓았다. (문자 r 이 있고 없음의 차이)

 

(*
 * =============================================================
 * Filename: testArcSine.pas
 *
 *    This source should be compiled by using FreePascal,
 *    which can be downloaded from http://www.freebasic.net
 *
 *    See: http://www.freepascal.org/docs-html/rtl/sysutils/format.html
 *            for formatted strings.
 *
 * Compile: fpc testArcSine.pas
 * Execute: testArcSine
 *
 * Date: 2013. 1. 2.
 * Copyright (c) pkim _AT_ scripts.pe.kr
 * =============================================================
 *)

program CalculateArcSine;

uses 
    sysutils,   (* for format *)
    math;

var
     x : real;
     y : real;
     u : real;
     v : real;

function asinh(x : real) : real;
var
     y : real;
begin
     y := ln(x + sqrt(x*x + 1));
     asinh := y;
end;

function acosh(x : real) : real;
var
     y : real;
begin
     y := ln(x + sqrt(x*x - 1));
     acosh := y;
end;


begin
        x := -0.9;
        y := arcsin(x);
        Writeln( Format('y = asin(%.1f) = %.10f', [x, y]) );
        Writeln( Format('sin(y) = sin(%.9f) = %.1f', [y, sin(y)]) );
        Writeln();
       
        x := 1.1;
        u := arcosh(x);
        Writeln( Format('u = acosh(%.1f) = %.10f', [x, u]) );
       
        v := arsinh(x);
        Writeln( Format('v = asinh(%.1f) = %.10f', [x, v]) );

        Writeln( Format('cosh(u) = cosh(%.10f) = %.1f', [u, cosh(u)]) );
        Writeln( Format('sinh(v) = sinh(%.10f) = %.1f', [v, sinh(v)]) );
        Writeln;

        Writeln( Format('acosh(cosh(u)) = acosh(%.1f) = %.10f', [cosh(u), acosh(cosh(u))]) );
        Writeln( Format('asinh(sinh(v)) = asinh(%.1f) = %.10f', [sinh(v), asinh(sinh(v))]) );

end.


(*
Output:
y = asin(-0.9) = -1.1197695150
sin(y) = sin(-1.119769515) = -0.9

u = acosh(1.1) = 0.4435682544
v = asinh(1.1) = 0.9503469298
cosh(u) = cosh(0.4435682544) = 1.1
sinh(v) = sinh(0.9503469298) = 1.1

acosh(cosh(u)) = acosh(1.1) = 0.4435682544
asinh(sinh(v)) = asinh(1.1) = 0.9503469298

----------------
Unformatted output:
y = asin(-9.00000000000000E-001) = -1.11976951499863E+000
sin(y) = sin(-1.11976951499863E+000) = -9.0000000000000000E-0001

*)

 

 

 

Posted by Scripter
,

근사 공식

         \frac{\pi}{\sqrt{12}} = \sum^\infty_{k=0} \frac{(-3)^{-k}}{2k+1}

을 이용하여 근사값 계산하는 프로그램을 Pascal 언어로 작성해 보았다.



(*****************************************************) (* Filename: approximatePiOverSqrt12.pas *) (* *) (* Compile: fpc approximatePiOverSqrt12.pas      *) (* Execute: approximatePiOverSqrt12,pas *) (*****************************************************) program ApproximatePiOverSqrt12; var BOUND : integer = 100; value1, value2, err : real; function approximate(BND : integer) : real; (* BND : 급수에서 합할 항의 개수 *) (* Using the series: \frac{\pi}{\sqrt{12}} = \sum^\infty_{k=0} \frac{(-3)^{-k}}{2k+1} *) var i : integer; sum : real = 0; x : real; begin x := 1; sum := 1; for i := 1 to BND do begin x := - (x / 3); sum := sum + x/(2*i + 1); end; approximate := sum; end; begin writeln( 'Approximate the value of PI/sqrt(12)' ); writeln( '-------------------------------------------' ); value1 := PI / SQRT(12.0); writeln( ' Evaluated directly:', value1 ); value2 := approximate(BOUND); writeln( 'Estimated by series:', value2 ); err := value2 - value1; writeln( ' Error:', err ); end.


실행 결과:
Approximate the value of PI/sqrt(12)
-------------------------------------------
 Evaluated directly: 9.06899682117109E-001
Estimated by series: 9.06899682117109E-001
              Error: 2.22044604925031E-016



Posted by Scripter
,

Pascal 언어의 for 반복문 양식은

       for varName := startValue to finalValue do
     begin
             block;
     end;

또는 

       for varName := startValue downto finalValue do
     begin
             block;
     end;


* Pascal 언어의 for 반복문에는 step 이 없다.

* Pascal 언어는 C 언어와 달리 변수명과 프로시듀어명, 함수명에 대소문자를 구별하지 않는다. 즉, 아래의 소스에서 변수명 dan 과 Dan 은 같은 것아고,  프로시듀어명 printDan 과 PrintDan 은 같은 것아다.

* C 언어에서 void 함수라고 하는 것 즉 리턴값이 없는 함수를 Pascal 언어에서는 function 이라 하지 않고 procedure 라고 한다. 즉 리턴값의 있고 없고에 따라 function 과 procedure 를 엄격히 구분한다.


procedure 의 구문은

        procedure 프로시듀어명(변수들);
        const
            상수명 : 타입 = 값;
        var
            변수명 : 타입 = 값;
        begin
            블록;
        end

이고, function 의 구문은

        function 함수명(변수들) : 리턴타입;
        const
            상수명 : 타입 = 값;
        var
            변수명 : 타입 = 값;
        begin
            블록;
            함수명 := 리턴값;
        end


* Pascal 언어에서는 각 구문의 끝에 세미콜론(;)을 붙여야 할지 말아야 할지를 반드시 지켜야 한다, 예를 들어, if ... then ... else .... 조건문에서

      if 조건절 then
        begin
            라인1;
            라인2;
            라인3;
        end     (* 여기는 else 직전이므로 세미콜론이 없어야 한다. *)
      else
        begin
            라인1;
            라인2;
            라인3; 
        end      (* 여기는 end 직전이므로 세미콜론이 없어야 한다. *)
    end;


* 더 자세한 것은 http://wiki.freepascal.org/IF 를 참조

* C 언어 사용자를 위한 Free Pascal: http://wiki.freepascal.org/Pascal_for_C_users

* Free Pascal 로 하는 Canvas 드로우잉 프로그래밍: http://wiki.freepascal.org/Drawing_with_canvas

* Pascal 언어로 작성된 소스의 끝은 반드시 end. 로 끝나야 한다.
  여기서 end 다음에 마침표(.)가 있는 것이 매우 중요함.

'
소스 파일명: for_test.pas
------------------------------[소스 시작]

(******************************************) (* Filename: for_test.pas *) (* *) (* Compile: fpc for_test.pas *) (* Execute: for_test *) (******************************************) program Gugudan; var dan : integer; procedure PrintDan(Dan : integer); (* 매개변수(parameter) dan 으로 넘어온 *) (* 구구단 표를 출력한다. *) var i : integer; begin for i := 1 to 9 do begin writeln( dan, ' x ', i, ' = ', dan*i:2 ); end end; begin dan := 2; printDan(dan); end. ------------------------------[소스 끝]

컴파일> fpc for_test.pas
실행> for_test
2 x 1 =  2
2 x 2 =  4
2 x 3 =  6
2 x 4 =  8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18



Posted by Scripter
,
컴피일은 Free Pascal 컴파일러(fpc)를 이용합니다.
Free Pascal 2.6.0 이 2011년 12월 31일에 출시되었습니다.
(******************************************) (* Filename: hello.pas *) (* *) (* Compile: fpc hello.pas *) (* Execute: hello *) (******************************************) program HelloWorld; begin writeln('Hello, world!'); writeln('안녕하세요?'); (* readln; *) end.

Posted by Scripter
,