Chaithra
Chaithra

Reputation: 1140

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 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

Upvotes: 1

Views: 4355

Answers (5)

Gauthier
Gauthier

Reputation: 42025

Are you looking for absolute value?

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

Upvotes: 2

Thomas
Thomas

Reputation: 182073

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

Upvotes: 0

knittl
knittl

Reputation: 265908

use the function abs() to return the absolute value

Upvotes: 0

Alex Budovski
Alex Budovski

Reputation: 18456

abs or fabs?

Upvotes: 0

Andreas Brinck
Andreas Brinck

Reputation: 52549

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)

Upvotes: 11

Related Questions