Search code examples
cdouble

How to get absolute value from double - c-language


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.


Solution

  • Use fabs() (in math.h) to get absolute-value for double:

    double d1 = fabs(-3.8951);