Reputation: 2858
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.
Upvotes: 52
Views: 343443
Reputation: 1
#include <iostream>
using namespace std;
int makePositiveSUM(int a,int b);
int main() {
int t;
cin>>t;
while(t--){
int x,y;
cin>>x>>y;
cout<<makePositiveSUM(x,y)<<endl;
}
return 0;
}
int makePositiveSUM(int a,int b){
if(a>b){
return a-b;
}
else {
return b-a;
}
}
Upvotes: 0
Reputation: 16318
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;
}
Upvotes: 51
Reputation: 11
Another way is to simply subtract the number from zero to convert the sign.
Number = -5:
0 - -5 = 5
Number = 64.3:
0 - 64.3 = -64.3
If you are writing for an embedded application on a processor without multiplication capability in its ALU, it save cycles and code space.
Upvotes: 1
Reputation: 1
this should work whatever sign a has, subject the power fits the int boundaries: a=sqrt(a*a)
Upvotes: 0
Reputation: 1
this is the only way i can think of doing it.
//positive to minus
int a = 5; // starting with 5 to become -5
int b = int a * 2; // b = 10
int c = a - b; // c = - 5;
std::cout << c << endl;
//outputs - 5
//minus to positive
int a = -5; starting with -5 to become 5
int b = a * 2;
// b = -10
int c = a + b
// c = 5
std::cout << c << endl;
//outputs 5
Function examples
int b = 0;
int c = 0;
int positiveToNegative (int a) {
int b = a * 2;
int c = a - b;
return c;
}
int negativeToPositive (int a) {
int b = a * 2;
int c = a + b;
return c;
}
Upvotes: -1
Reputation: 9
floor a;
floor b;
a = -0.340515;
so what to do?
b = 65565 +a;
a = 65565 -b;
or
if(a < 0){
a = 65565-(65565+a);}
Upvotes: 0
Reputation: 2985
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;
Upvotes: 5
Reputation: 29
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.
Upvotes: 2
Reputation: 561
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.
Upvotes: 20
Reputation: 11
Why do you want to use strange hard commands, when you can use:
if(a < 0)
a -= 2a;
The if statement obviously only applies when you aren't sure if the number will be positive or negative.
Otherwise you'll have to use this code:
a = abs(a) // a is an integer
a = fabs(a) // a is declared as a double
a = fabsf(a) // a is declared as a float (C++ 11 is able to use fabs(a) for floats instead of fabs)
To activate C++ 11 (if you are using Code::Blocks, you have to:
After following these steps, you should be able to use fabs(a) for floats instead of fabsf(a), which was used only for C99 or less! (Even C++ 98 could allow you to use fabs instead of fabsf :P)
Upvotes: -2
Reputation: 78393
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)
Upvotes: 101