views:

271

answers:

5
+2  Q: 

abs function in C

Hi people!

I wrote some code in C (not in C++):

Mask1 = abs(Area1 * 2 + Area2 * -2);

Area1, Area2 and Mask1 are three double variables. (e.g. 3.00556, 34.3333) My problem is that abs returns an integer value (e.g 30).

What I need to do to fix it?

Regards.

+10  A: 

Use fabs

FreeMemory
Note that this is because C doesn't support method overloading, for those coming from "other" languages.
Andrew Coleson
In fact, C doesn't even have methods. It only has functions. ;)
Dima
+1  A: 

Use fabs()

qrdl
+3  A: 

fabs http://www.manpagez.com/man/3/fabs/

Andrew Garrison
+3  A: 

abs() takes an integer as an argument and returns an integer result. Your doubles are being automatically truncated to integers. A decent C++ compiler would give you a warning. ;)

The function to use here is fabs() for doubles or fabsf() for floats.

Dima
A: 

use fabs()

quest49