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
2010-04-30 10:57:52
Don't forget `labs` for longs! :-)
tomlogic
2010-04-30 16:33:57
A:
fabsf(f)
returns the absolute value of f
: fabsf(40) == 40
and also fabsf(-40) == 40
.
Thomas
2010-04-30 10:58:29
+2
A:
Are you looking for absolute value?
#include<math.h>
includes abs
for integers, fabs
for floats.
Gauthier
2010-04-30 11:00:07