C语言 如何将负数变成正数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4745617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to make negative numbers into positive
提问by Monish Kumar
I am having the negative floating point number as:
我的负浮点数为:
a = -0.340515;
to convert this into positive number I used the abs() method as:
要将其转换为正数,我使用了 abs() 方法:
a = abs(a);
the result is a = 0.000000;
结果是 a = 0.000000;
But I need the result as 0.340515.
但我需要结果为0.340515.
Can anyone tell me how to do this.
谁能告诉我如何做到这一点。
回答by Jason Coco
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)
abs()仅适用于整数。对于浮点数,请使用fabs()(或fabs()具有正确精度的行之一,无论 a 实际是什么)
回答by Bhumit Mehta
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.
您必须使用:
abs() for int
fabs() for double
fabsf() for float
Above 函数也可以使用,但您也可以尝试类似的方法。
if(a<0)
{
a=-a;
}
回答by Gaurav
Use float fabsf (float n)for floatvalues.
使用float fabsf (float n)的float值。
Use double fabs (double n)for doublevalues.
使用double fabs (double n)的double值。
Use long double fabsl(long double)for long doublevalues.
使用long double fabsl(long double)的long double值。
Use abs(int)for intvalues.
使用abs(int)的int值。
回答by shamnad
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.
问题解决了。如果问题有一个较小的解决方案,那么你们为什么要寻求复杂的解决方案。请指导人们使用基本逻辑,因为只有人们才能训练他们的编程逻辑。
回答by Gabriel Lidenor
Well, in mathematics to convert a negative number to a positive number you just need to multiplethe negative number by -1;
那么,在数学负数转换为你只需要一个正数倍数乘以-1负数;
Then your solution could be like this:
那么你的解决方案可能是这样的:
a = a * -1;
or shorter:
或更短:
a *= -1;
回答by Neurotrin
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);}
回答by Nōmen nōn habeō
As stated above you would have to use a certain variant of abs() based on the integral type.
如上所述,您必须使用基于整数类型的 abs() 的某个变体。
However I would to define a macro like:
但是我想定义一个宏,如:
#define POSITIVE(n) ((n) < 0 ? 0 - (n) : (n))
so that I can pass any value, especially when not sure about the sign, and always get a positive.
这样我就可以传递任何值,尤其是在不确定符号的情况下,并且始终获得正值。
回答by VVI C And C 15
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.
if 语句显然仅在您不确定数字是正数还是负数时才适用。
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:
要激活 C++ 11(如果您使用的是 Code::Blocks,您必须:
- Open up Code::Blocks (recommended version: 13.12).
- Go to Settings -> Compiler.
- Make sure that the compiler you use is GNU GCC Compiler.
- Click Compiler Settings, and inside the tab opened click Compiler Flags
- 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.
- Restart Code::Blocks and then you are good to go!
- 打开 Code::Blocks(推荐版本:13.12)。
- 转到设置-> 编译器。
- 确保您使用的编译器是 GNU GCC Compiler。
- 单击编译器设置,然后在打开的选项卡内单击编译器标志
- 向下滚动直到找到:让 g++ 遵循 C++ 11 ISO C++ 语言标准 [-std=c++11]。检查,然后点击确定按钮。
- 重新启动 Code::Blocks,然后你就可以开始了!
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)
按照这些步骤操作后,您应该可以将 fabs(a) 用于浮点数,而不是 fabsf(a),后者仅用于 C99 或更低版本!(即使 C++ 98 也可以允许您使用 fabs 而不是 fabsf :P)

