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!
我想要绝对值从负的双精度-我认为ab -函数和java一样容易使用-但不是!
It seems that the abs
-function returns an int because I have the value 3.8951 and the output is 3.000000
似乎ab函数返回一个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
.
我该如何解决这个问题?也就是,我想要一个二重的绝对值。
3 个解决方案
#1
62
Use fabs()
(in math.h) to get absolute-value for double
:
使用fabs()(在math.h中)获取绝对值为double:
double d1 = fabs(-3.8951);
#2
11
Use fabs
instead of abs
to find absolute value of double
(or float
) data types. Include the <math.h>
header for fabs
function.
使用fabs而不是abs来找到双(或浮点)数据类型的绝对值。包括 <数学。用于fabs函数的h> 头。
double d1 = fabs(-3.8951);
#3
5
It's worth noting that Java can overload a method such as abs
so 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语言中,不存在重载,所以需要不同的函数来表示整数和双精度。
#1
62
Use fabs()
(in math.h) to get absolute-value for double
:
使用fabs()(在math.h中)获取绝对值为double:
double d1 = fabs(-3.8951);
#2
11
Use fabs
instead of abs
to find absolute value of double
(or float
) data types. Include the <math.h>
header for fabs
function.
使用fabs而不是abs来找到双(或浮点)数据类型的绝对值。包括 <数学。用于fabs函数的h> 头。
double d1 = fabs(-3.8951);
#3
5
It's worth noting that Java can overload a method such as abs
so 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语言中,不存在重载,所以需要不同的函数来表示整数和双精度。