C语言 如何从双 C 语言中获取绝对值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20956352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get absolute value from double - c-language
提问by user3155478
I want the absolute-value from a negative double - and I thought the abs-function was as easy to use as in java - but NOT!
我想要负双abs精度值的绝对值 - 我认为- 函数和在 java 中一样容易使用 - 但不是!
It seems that the abs-function returns an int because I have the value 3.8951 and the output is 3.000000
似乎abs-function 返回一个 int 因为我的值是 3.8951 而输出是 3.000000
double d1 = abs(-3.8951);
printf("d1: ...%lf", d1);
How can I fix this problem? That is - I want the absolute value of a double.
我该如何解决这个问题?那就是 - 我想要 a 的绝对值double。
回答by herohuyongtao
回答by haccks
Use fabsinstead of absto find absolute value of double(or float) data types. Include the <math.h>header for fabsfunction.
使用fabs代替abs来查找double(或float)数据类型的绝对值。包括函数的<math.h>标题fabs。
double d1 = fabs(-3.8951);
回答by lurker
It's worth noting that Java can overload a method such as absso that it works with an integer or a double. In C, overloading doesn't exist, so you need different functions for integer versus double.
值得注意的是,Java 可以重载一个方法,例如abs它可以处理整数或双精度数。在 C 中,不存在重载,因此整数和双精度需要不同的函数。
回答by Firas Nomaan
I have found that using cabs(double), cabsf(float), cabsl(long double), __cabsf(float), __cabs(double), __cabsf(long double)is the solution
我发现使用cabs(double), cabsf(float), cabsl(long double), __cabsf(float), __cabs(double),__cabsf(long double)是解决方案
回答by MD Rakibuz Sultan
//use fabs()
double sum_primary_diagonal=0;
double sum_secondary_diagonal=0;
double difference = fabs(sum_primary_diagonal - sum_secondary_diagonal);

