50

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.

dandan78
  • 13,328
  • 13
  • 64
  • 78
Monish Kumar
  • 2,788
  • 4
  • 38
  • 54
  • Why is this tagged Objective C when it's about C++? – Catfish_Man Jan 20 '11 at 09:57
  • 11
    @Catfish_Man: it's actually about C. – JeremyP Jan 20 '11 at 11:29
  • 1
    multiply by -1 maybe? – Rezo Megrelidze Jan 08 '15 at 11:44
  • 2
    @RezoMegrelidze's suggestion is 100% okay in the event you don't want to include an entire library just for a single purpose. Granted, you must be 100% sure that you're dealing with a negative number: `t1 = t1 < 0 ? -1*t1 : t1;` I'd assume that's all that `abs `family does in this scenario. | Alternatively, you can subtract the negative value from 0: `t1 = t1 < 0 ? 0-t1 : t1;` Given that, the standard solution is still favorable as it's more likely to be immediately clear. Do as you please, but keep that in mind. – Super Cat Aug 27 '15 at 03:28

9 Answers9

98

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)

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Jason Coco
  • 77,985
  • 20
  • 184
  • 180
48

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;
    }
Bhumit Mehta
  • 16,278
  • 11
  • 50
  • 64
19

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.

Gaurav
  • 553
  • 5
  • 9
5

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;
Gabriel Lidenor
  • 2,905
  • 2
  • 25
  • 26
2
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.

shamnad
  • 29
  • 3
  • because `abs()` is more useful in some cases, like when you have a number that can be both positive and negative, in this case multipling by -1 will not work. – TheGoldenTree May 23 '22 at 14:36
0
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);}
Neurotrin
  • 9
  • 4
0
    #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;
    }
}

    enter code here
    enter code here
    enter code here
Aisha
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 09 '23 at 13:13
-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; 
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
-2

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:

  1. Open up Code::Blocks (recommended version: 13.12).
  2. Go to Settings -> Compiler.
  3. Make sure that the compiler you use is GNU GCC Compiler.
  4. Click Compiler Settings, and inside the tab opened click Compiler Flags
  5. Scroll down until you find: Have g++ follow the C++ 11 ISO C++ language standard [-std=c++11]. Check that and then hit OK button.
  6. Restart Code::Blocks and then you are good to go!

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)

Ian
  • 25
  • 7
  • 1
    `if(a < 0) a -= a;` urhm.... wouldnt this end in `if (a < 0) a*=2;`? What is not what OP trys to acv – dhein Apr 13 '15 at 09:59