how to make negative numbers into positive
Solution 1:
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)
Solution 2:
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;
}
Solution 3:
Use float fabsf (float n)
for float
values.
Use double fabs (double n)
for double
values.
Use long double fabsl(long double)
for long double
values.
Use abs(int)
for int
values.
Solution 4:
Well, in mathematics to convert a negative number to a positive number you just need to multiple the negative number by -1;
Then your solution could be like this:
a = a * -1;
or shorter:
a *= -1;
Solution 5:
a *= (-1);
problem solved. If there is a smaller solution for a problem, then why you guys going for a complex solution. Please direct people to use the base logic also because then only the people can train their programming logic.