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!
It seems that the abs
-function returns an int because I have the value 3.8951 and the output is 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
.
Especially since Java has classes and can overload methods, but C does not. abs is only implemented for integer in C.
The difference is that math. fabs(number) will always return a floating-point number even if the argument is an integer, whereas abs() will return a floating-point or an integer depending upon the argument.
In the C Programming Language, the fabs function returns the absolute value of a floating-point number.
Use fabs()
(in math.h) to get absolute-value for double
:
double d1 = fabs(-3.8951);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With