32

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?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Whitequill Riclo
  • 788
  • 2
  • 8
  • 19
  • 6
    um, 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... – Marcus Müller Jan 10 '15 at 02:30
  • I think you may want sympy for this. – tox123 Jan 10 '15 at 02:30
  • @MarcusMüller he means as in when you do a square root, it has a negative and positive value simultaneously. – tox123 Jan 10 '15 at 02:32
  • I think the only way for this is `if` blocks. –  Jan 10 '15 at 02:32
  • 1
    @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". – Marcus Müller Jan 10 '15 at 02:33
  • 2
    @tox123: I think you're right about needing sympy. I just found this [plus-minus object issue](https://code.google.com/p/sympy/issues/detail?id=2206) – Whitequill Riclo Jan 10 '15 at 02:37

8 Answers8

51

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

print(u"\u00B1")
mimoralea
  • 9,590
  • 7
  • 58
  • 59
21

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!
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • This 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? –  Jan 10 '15 at 02:47
  • This is binary +/- but the OP is looking for unary +/-. But thanks for this neat recommendation anyway. – smci Jul 11 '18 at 23:55
  • ` the specified error is not a tolerance but a standard-deviation value` ... that would be fine, but... their documentation page shows tolerances. Bad documentation? https://pythonhosted.org/uncertainties/ – cowlinator Nov 17 '22 at 22:56
9

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)
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
  • Nice! I posted [my own answer](https://stackoverflow.com/a/63660407/4518341) based on this, that uses a generator expression instead of NumPy. – wjandrea Aug 30 '20 at 17:50
8

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.

Orange Pukeko
  • 157
  • 1
  • 2
  • 8
  • 1
    If 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. –  May 02 '18 at 08:10
2

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 if statement and find each multiplier.

1

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

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.

asmeurer
  • 86,894
  • 26
  • 169
  • 240
0

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

r = 10.0*sqrt(19)
s1, s2 = (sqrt((125.0 + i) / 366.0) for i in (r, -r))

This is based on Nico's answer, but using a generator expression instead of NumPy

wjandrea
  • 28,235
  • 9
  • 60
  • 81
0

A plus/minus tolerance test can be done using a difference and absolute against the tolerance you wish to test for. Something like:

tst_data = Number you wish to test
norm = Target number
tolerance = Whatever the allowed tolerance is.

if abs(tst_data - norm) <= tolerance:
do stuff

Using the abs function allows the test to return a +/- within tolerance as True

atc_ceedee
  • 113
  • 6