Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get absolute value from double - c-language

Tags:

c

double

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.

like image 714
user3155478 Avatar asked Jan 06 '14 18:01

user3155478


People also ask

Can you use abs on double C?

Especially since Java has classes and can overload methods, but C does not. abs is only implemented for integer in C.

What is the difference between abs () and fabs () functions 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.

What is Fabsf in C?

In the C Programming Language, the fabs function returns the absolute value of a floating-point number.


1 Answers

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

double d1 = fabs(-3.8951); 
like image 136
herohuyongtao Avatar answered Sep 21 '22 18:09

herohuyongtao