How to get absolute value from double - c-language

user3155478 picture user3155478 · Jan 6, 2014 · Viewed 120.3k times · Source

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.

Answer

herohuyongtao picture herohuyongtao · Jan 6, 2014

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

double d1 = fabs(-3.8951);