Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make negative numbers into positive

Tags:

c

I am having the negative floating point number as:

a = -0.340515; 

to convert this into positive number I used the abs() method as:

a = abs(a); 

the result is a = 0.000000;

But I need the result as 0.340515.

Can anyone tell me how to do this.

like image 466
Monish Kumar Avatar asked Jan 20 '11 09:01

Monish Kumar


People also ask

How do I turn a negative number positive in Python?

In Python, positive numbers can be changed to negative numbers with the help of the in-built method provided in the Python library called abs (). When abs () is used, it converts negative numbers to positive.


2 Answers

abs() is for integers only. For floating point, use fabs() (or one of the fabs() line with the correct precision for whatever a actually is)

like image 141
Jason Coco Avatar answered Sep 21 '22 13:09

Jason Coco


You have to use:

abs() for int
fabs() for double
fabsf() for float

Above function will also work but you can also try something like this.

    if(a<0)     {          a=-a;     } 
like image 42
Bhumit Mehta Avatar answered Sep 19 '22 13:09

Bhumit Mehta