tags:

views:

103

answers:

5

I am using C as the programming language. How to ignore the sign when using float/signed int variables? For example if float/signed int f = -40 or +40 The result should be same after a mathematical operation like a+b*f

+10  A: 

Use abs for ints or fabs for floats.

a+b*abs(f)

EDIT: It's not clear wether you want -40 to be treated as 40 or vice versa, if you for some reason wan't the latter:

a+b*-abs(f)
Andreas Brinck
Don't forget `labs` for longs! :-)
tomlogic
A: 

abs or fabs?

Alex
A: 

use the function abs() to return the absolute value

knittl
A: 

fabsf(f) returns the absolute value of f: fabsf(40) == 40 and also fabsf(-40) == 40.

Thomas
+2  A: 

Are you looking for absolute value?

#include<math.h> includes abs for integers, fabs for floats.

Gauthier