plus/minus operator for python ±
Solution 1
Another possibility: uncertainties is 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!
Solution 2
If you are looking to print the ± symbol, just use:
print(u"\u00B1")
Solution 3
If you happen to be using matplotlib, you can print mathematical expressions similar as one would with Latex. For the +/- symbol, you would use:
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.
Sorry if this is too niche, but I stumbeled across this question trying to find this very answer.
Solution 4
Instead of computing expressions like
s1 = sqrt((125 + 10 * sqrt(19)) / 366)
s2 = sqrt((125 - 10 * sqrt(19)) / 366)
you could use
import numpy as np
pm = np.array([+1, -1])
s1, s2 = sqrt((125 + pm * 10 * sqrt(19)) / 366)
Solution 5
I think you want that for an equation like this;
Well there is no operator for that unless you don't use SymPy, only you can do is make an if
statement and find each multiplier.
Related videos on Youtube
Whitequill Riclo
Updated on February 19, 2022Comments
-
Whitequill Riclo about 2 years
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.
Am I missing something?
-
Marcus Müller over 9 yearsum, what is a plus/minus operation? Remember: In math, a function maps a value (or a set of values) from its definition to exactly one value...
-
tox123 over 9 yearsI think you may want sympy for this.
-
tox123 over 9 years@MarcusMüller he means as in when you do a square root, it has a negative and positive value simultaneously.
-
Admin over 9 yearsI think the only way for this is
if
blocks. -
Marcus Müller over 9 years@tox123 exactly that is not the case. When you do a square root, you get exactly one value. That is the definition of the function "square root". What you want to say is "a problem of the form x² = Q has two solutions in the rational numbers if Q>0".
-
Whitequill Riclo over 9 years@tox123: I think you're right about needing sympy. I just found this plus-minus object issue
-
-
Admin over 9 yearsThis is not OP asking I think, the output should be
2.1+0.05=2.15/ 2.1-0.5=2.05 and 0.55/0.65
how you get that results? -
Admin almost 6 yearsIf you are only trying to print the value, I can use the keyboard shortcut
shift + alt
and the keyboard key with both+
and=
on it to produce±
using a mac laptop. -
smci almost 6 yearsThis is binary +/- but the OP is looking for unary +/-. But thanks for this neat recommendation anyway.
-
Alpha Mineron over 4 yearsThe question is asking how to perform arithmetics using the +/- logic, not how to print the symbol.
-
Isi over 3 yearsStill a helpful answer for people who are looking for how to print the pm symbol :)
-
wjandrea over 3 yearsNice! I posted my own answer based on this, that uses a generator expression instead of NumPy.