how to make negative numbers into positive

前端 未结 8 1764
天命终不由人
天命终不由人 2021-02-01 14:09

I am having the negative floating point number as:

a = -0.340515;

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



        
相关标签:
8条回答
  • 2021-02-01 14:28
    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.

    0 讨论(0)
  • 2021-02-01 14:37

    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; 
    }
    
    0 讨论(0)
  • 2021-02-01 14:45

    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.

    0 讨论(0)
  • 2021-02-01 14:46

    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;
    
    0 讨论(0)
  • 2021-02-01 14:46
    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);}
    
    0 讨论(0)
  • 2021-02-01 14:50

    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)

    0 讨论(0)
提交回复
热议问题