python的加/减运算符±

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27872250/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:23:36  来源:igfitidea点击:

plus/minus operator for python ±

pythonnumpyscipyipythonsympy

提问by Whitequill Riclo

I am looking for a way to do a plus/minus operation in python 2 or 3. I do not know the command or operator, and I cannot find a command or operator to do this.

我正在寻找一种在 python 2 或 3 中进行加/减运算的方法。我不知道命令或运算符,也找不到执行此操作的命令或运算符。

Am I missing something?

我错过了什么吗?

采纳答案by Hugh Bothwell

Another possibility: uncertaintiesis a module for doing calculations with error tolerances, ie

另一种可能性:不确定性是一个用于进行容错计算的模块,即

(2.1 +/- 0.05) + (0.6 +/- 0.05)    # => (2.7 +/- 0.1)

which would be written as

这将被写为

from uncertainties import ufloat

ufloat(2.1, 0.05) + ufloat(0.6, 0.05)

Edit:I was getting some odd results, and after a bit more playing with this I figured out why: the specified error is not a tolerance (hard additive limits as in engineering blueprints) but a standard-deviation value - which is why the above calculation results in

编辑:我得到了一些奇怪的结果,经过更多的尝试,我找到了原因:指定的错误不是容差(工程蓝图中的硬附加限制),而是标准偏差值 - 这就是上述原因计算结果为

ufloat(2.7, 0.07071)    # not 0.1 as I expected!

回答by Whitequill Riclo

I think you want that for an equation like this;

我想你想要这样的等式;

enter image description here

在此处输入图片说明

Well there is no operator for that unless you don't use SymPy, only you can do is make an ifstatement and find each multiplier.

好吧,除非您不使用SymPy,否则没有操作符,只有您可以做一个if声明并找到每个乘数。

回答by asmeurer

There is no such object in SymPy yet (as you saw, there is an issue suggesting one https://github.com/sympy/sympy/issues/5305). It's not hard to emulate, though. Just create a Symbol, and swap it out with +1 and -1 separately at the end. Like

SymPy 中还没有这样的对象(如您所见,有一个问题表明https://github.com/sympy/sympy/issues/5305)。不过,模仿起来并不难。只需创建一个符号,并在最后分别用 +1 和 -1 交换它。喜欢

pm = Symbol(u'±') # The u is not needed in Python 3. I used ± just for pretty printing purposes. It has no special meaning.
expr = 1 + pm*x # Or whatever
# Do some stuff
exprpos = expr.subs(pm, 1)
exprneg = expr.subs(pm, -1)

You could also just keep track of two equations from the start.

您也可以从一开始就跟踪两个方程。

回答by Orange Pukeko

If you happen to be using matplotlib, you can print mathematical expressions similar as one would with Latex. For the +/- symbol, you would use:

如果您碰巧使用 matplotlib,您可以打印类似于使用 Latex 的数学表达式。对于 +/- 符号,您将使用:

print( r"value $\pm$ error" )

Where the r converts the string to a raw format and the $-signs are around the part of the string that is a mathematical equation. Any words that are in this part will be in a different font and will have no whitespace between them unless explicitly noted with the correct code. This can be found on the relavent page of the matplotlib documentation.

其中 r 将字符串转换为原始格式,$ 符号围绕字符串中的数学方程部分。这部分中的任何单词都将使用不同的字体,并且除非用正确的代码明确指出,否则它们之间不会有空格。这可以在matplotlib 文档的相关页面上找到。

Sorry if this is too niche, but I stumbeled across this question trying to find this very answer.

对不起,如果这太小众了,但我偶然发现了这个问题,试图找到这个答案。

回答by Nico Schl?mer

Instead of computing expressions like

而不是像这样计算表达式

s1 = sqrt((125.0 + 10.0*sqrt(19)) / 366.0)
s2 = sqrt((125.0 - 10.0*sqrt(19)) / 366.0)

you could use

你可以用

pm = numpy.array([+1, -1])
s1, s2 = sqrt((125.0 + pm * 10.0*sqrt(19)) / 366.0)

回答by mimoralea

If you are looking to print the ± symbol, just use:

如果您要打印 ± 符号,只需使用:

print(u"\u00B1")