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

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

수학에서 sin 함수의 역함수는 arcsin 으로 표기되는데, D 언어에서는 asin 함수로 구현되어 있다. D 언어에서 지수함수, 로그함수, 삼각함수, 역삼각함수, 쌍곡선함수, 역쌍곡선함수 등을 이용하려면 import 구문

import std.math;

가 필요하다.

D 언어에 쌍곡선함수 sinhcosh 의 역함수로 각각 asinhacosh 가 이미 구현되어 있지만, 아래의 소스에서 arcsinharccosh 라는 이름의 함수로 자체 구현해 보았다.

삼각함수 sin, cos, tan 값은 cast(double) 로 캐스팅해서 (double 타입으로) 명시적인 타입변환해야 한다. 안 그러면 전혀 다른 값이 되어 버린다. (real 타입과의 충돌 때문에 이런 버그(?)가 있는 것 같은데, 이런 현상은 DMD 1.0 이든 DMD 2,0 이든 마찬가지이다.) 쌍곡선함수 sinh, cosh, tanh 값에는 이런 현상이 없다.

 

/*
 * Filename: testArcSine.d
 *
 * Cpmpile: dmd testArcSine.d
 * Execute: ./testArcSine
 *
 * Date: 2013. 1. 4.
 * Copyright (c) pkim _AT_ scripts.pe.kr
 */

import std.c.stdio;    // printf 함수 사용을 위해
import std.stdio;    // writeln 함수 사용을 위해
import std.math;

double arcsinh(double x) {
    double y = log(x + sqrt(x*x + 1));
    return y;
}

double arccosh(double x) {
    double y = log(x + sqrt(x*x - 1));
    return y;
}

int main (string[] args) {
    double x, y, u, v;

    x = -0.9;
    y = asin(x);

    printf("y = asin(%g) = %.9f\n", x, y);
    printf("sin(y) = sin(%.9f) = %g\n", y, cast(double) sin(y));
    writeln("");
   
    x = 1.1;
    u = acosh(x);
    printf("u = acosh(%g) = %.10f\n", x, u);

    v = asinh(x);
    printf("v = asinh(%g) = %.10f\n", x, v);

    printf("cosh(u) = cosh(%.10f) = %g\n", u, cosh(u));
    printf("sinh(v) = sinh(%.10f) = %g\n", v, sinh(v));
    writeln("");

    printf("arccosh(%g) = %.10f\n", x, arccosh(x));
    printf("arcsinh(%g) = %.10f\n", x, arcsinh(x));
    return 0;
}

/*
Output:
y = asin(-0.9) = -1.119769515
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

arccosh(1.1) = 0.4435682544
arcsinh(1.1) = 0.9503469298
*/

 

 

 

'프로그래밍 > D' 카테고리의 다른 글

if ... else ... 조건문 사용 예제 for D  (0) 2008.03.08
명령행 인자 처리 예제 for D  (0) 2008.03.08
for 반복문 예제 For D  (0) 2008.03.08
Hello 예제 for D  (0) 2008.03.08
Posted by Scripter
,

D 언어의 if ... else ... 조건문 구문은 C/C++/Java/Groovy 의 것과 같다.
단, 한글이 포함된 소스파일은 UTF-8 인코딩으로 저장해야 한다.
또 C/C++ 언어에서 쓰이는 exit() 함수를 D 언어에서 쓰기 위해서는

        import std.c.stdlib;

구문을 소스의 앞부분에 미리 써 놓아야 한다.


소스 파일명: testIfD.d

  1. import std.c.stdio;    // printf 함수 사용을 위해
  2. import std.c.stdlib;    // exit 함수 사용을 위해
  3. import std.conv;        // to! 변환함수 사용을 위해
  4. // 사용법을 컨솔에 보여주는 함수
  5. void printUsing() {
  6.     printf("Using: testIfD [number]\n");
  7.     printf("This determines whether the number is positive or not.\n");
  8. }
  9. int main (string[] args) {
  10.     float val;
  11.     if (args.length != 2) {
  12.         printUsing();
  13.         exit(1);
  14.         return 1;
  15.     }
  16.     // 명령행 인자의 스트링을 가져와서
  17.     // 배정밀도 부동소수점수로 변환하여
  18.     // 변수 val에 저장한다.
  19.     val = to!(float)(args[1]);
  20.     // 변수 val에 저장된 값이 양수인지 음수인지 0인지를
  21.     // 판단하는 if...else... 조건문
  22.     if (val > 0.0)
  23.         printf("%g is a positive number.\n", val);
  24.     else if (val < 0.0)
  25.         printf("%g is a negative number.\n", val);
  26.     else
  27.         printf("%g is zero.\n", val);
  28.     return 0;
  29. }

컴파일> dmd testIfD.d

실행> testIfD
Using: testIfD [number]
This determines whether the number is positive or not.

실행> testIfD 1.234
1.234 is a positive number.

실행> testIfD 1.234
-1.234 is a negative number.

실행> testIfD 0
0 is zero.



Posted by Scripter
,
소스에 한글이 포함된 경우에는 파일을 UTF-8 인코딩으로 저장해야 한다.
반복문은 for( ; ; ) 구문을 써도 되지만, 여기서는 foreach 구문을 사용하였다.


소스 파일명: testArgumentsD.d
  1. import std.c.stdio;  // prinf 함수의사용을 위해
  2. import std.conv;     // to! 변환함수 사용을 위해
  3. int main (string[] args) {
  4.     double sum = 0.0;    // 초기화
  5.     // 명령행 인자(command-line argument) 개수 출력
  6.     printf("Count of arguments: %d\n", args.length);
  7.     foreach (string arg; args[1 .. args.length]) {
  8.         // 스트링을 배정밀도 부동소수점수로 변환하여 누적
  9.         // sum += std.conv.to!(double)(arg);
  10.         sum += to!(double)(arg);
  11.     }
  12.     // 누적된 배정밀도 값을 출력
  13.     printf("The sum of arguments is %g\n", sum);
  14.     return 0;
  15. }


컴파일> dmd testArgumentsD.d

실행> testArgumentsD 1 2 3 4
Count of arguments: 5
The sum of arguments is 10

실행> testArgumentsD 1 2 3 4.1
Count of arguments: 5
The sum of arguments is 10.1

Posted by Scripter
,

D 언어에는 froeach를 사영하는 반목문도 있지만,
C/C++/Java 언어에서 쓰인 보총의 for ( ;  ;) 을 그대로 써도 된다.

 
소스 파일명: testForLoopD.d
------------------------------[소스 시작]
import std.c.stdio;

int main (string[] args) {
    int x;
    x = 2;
    for (int i = 1; i < 10; i++) {
        printf("%d x %d  = %d\n", x, i, x*i);
    }
    return 0;
}
------------------------------[소스 끝]


컴파일> dmd testForLoop.d

실행> testForLoop
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




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,

D 언어 홈페이지는 http://www.digitalmars.com/d/index.html 이다.

컨솔에 문자 출력하는 D 언어 구문은  C 언어의 것과 같다.

       printf(format,  ...);

 
소스 파일명: hello.d
------------------------------[소스 시작]
import std.c.stdio;

int main (string[] args) {
    printf("Hello, world!\n");
    return 0;
}
------------------------------[소스 끝]


컴파일> dmd hello.d

실행> hello
Hello, world!



Posted by Scripter
,