C 언어에서 long double 형 끼리 / 연산하는 방법이 없을까요?
조회수 1278회
#include <stdio.h>
#include <math.h>
int main(void)
{
double x,y;
printf("Please enter x and y : ");
scanf("%lf %lf", &x, &y);
long double z = ((x + sqrt(x*x+y*y))/2.0);
printf("The value of the expression is %25.20Lf\n", z);
return 0;
}
현재 아래와 같이 코드를 짰는데, z를 double 형으로 한뒤 lf로 출력하면 잘 나오는데 long double 형을 쓰면 0이 나옵니다. 근데 문제 조건이 z가 무조건 long double 형이여야 하는데, 어떻게 해야 /연산에서 바로 long double 끼리 나눌 수 있나요?
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
1 답변
-
clang 에서 std=c++11 일 때 아래와 같이 문제가 없습니다.(cling 은 clang 기반 인터프리터입니다.)
사용하신 컴파일러와 버전이 무엇인지요?
allinux@ip-10-0-0-29:~$ cling -std=c++11 ****************** CLING ****************** * Type C++ code and press enter to run it * * Type .q to exit * ******************************************* [cling]$ #include <math.h> [cling]$ double x = 2.0 (double) 2.0000000 [cling]$ double y = 2.0 (double) 2.0000000 [cling]$ long double z = ((x + sqrt(x*x+y*y))/2.0); [cling]$ z (long double) 2.4142136L
참고로 gcc(5.4.0)의 결과도 올려드립니다.
allinux@ip-10-0-0-29:~$ cat long_test.c #include <stdio.h> #include <math.h> int main(){ double x = 2.0; double y = 2.0; long double z = ((x + sqrt(x*x+y*y))/2.0); printf("The value of the expression is %25.20Lf\n", z); return 0; } allinux@ip-10-0-0-29:~$ gcc -std=c99 -o long_test long_test.c -lm allinux@ip-10-0-0-29:~$ ./long_test The value of the expression is 2.41421356237309492343
댓글 입력