开发者

How to ignore the sign of a variable in C?

I am using C as the programming language. How to ignore the sign when using f开发者_如何学运维loat/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


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)


Are you looking for absolute value?

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


abs or fabs?


use the function abs() to return the absolute value


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜