#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;
}
Currently, I made the code as below, but if you print it out as lf after setting z as double type, it comes out well, but if you use long double type, you get 0. But the problem condition is that z must be a long double type, so how can I divide the long double right away in the calculation?
c long-double type calculate
When std=c++11 in clang, there is no problem as shown below (cling is a clang-based interpreter).)
What is the compiler and version you used?
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
For your information, I will also upload the results of 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
© 2024 OneMinuteCode. All rights reserved.