# Adding numbers with defined precision produces incorrect result

I've tracked a bug in my code down to the problem of adding two numbers together, with the left argument having machine precision, e.g by 3 . The issue is, if there is no space between the two numbers, then the addition acts on the precision of the number and not the number itself. For example,

3+2 produces  3.0 instead of  5..

additionally, if we specify a numerical value for the precision, we get the correct result with specified precision:

32+2 produces 5.00

There is no space between the two arguments and the addition sign. If we add spaces:

3+ 2 produces 5.

3 + 2 produces 5.

This possible bug caused me a big headache in my code. Is there a way to globally remove this from the notebook so that I get the expected result from, e.g. 3+2=5.? I couldnt find anything in the documentation regarding this issue.

Some possibly relevant information:

My mathematica version is Version 13.0.1 for Linux x86 (64-bit)

• For information about operator/input precedence see this answer. Note that Precedence[Precision]==670. and Precedence[Plus]==310., so an ambiguously entered input expression gives precedence to the backtick precedence rather than the +. Jul 1 at 8:38
• See reference.wolfram.com/language/tutorial/InputSyntax.html#7977 for the definition of the input numbers where s may be a decimal real number or integer. Jul 1 at 16:57

This is a well-known problem of parsing of ambiguous input in Mathematica.

If you enter 3+2, it is correctly parsed as arbitrary precision number 3 with precision 2:

3+2 // InputForm

3.2.


If you include a space between the backtick and the plus sign, it will be interpreted as an addition of machine precision number 3 with exact number 2:

3 + 2

5.


Alternatively, you can specify that a number is a MachinePrecision number just by placing the dot right after it (in this case the presence or absence of the spaces between the dot and the plus sign does not matter):

3. + 2

5.


Is there a way to globally remove this from the notebook so that I get the expected result from, e.g. 3+2=5.?

If I type in the "Find and Replace" dialog + (without a space) in the "Find:" field, and  + (with the space character between the backtick and the plus sign) in the "Replace with:" field, I'm able to replace all the occurences, and obtan the result you wish (I'm on Mathematica 13.1.0). Here I have a different take from @AlexeyPopkov (+1), as I think taking care of the number of spaces in an expression is not a desirable programming strategy for Mathematica, so our analysis is equivalent but our solutions or advice are different.

# Analysis

3+2


Has two operators competing, SetPrecision and Plus and

Precedence[SetPrecision]>Precedence[Plus]
(* True *)


So it gets interpreted as

SetPrecision[3,Plus]


and then

SetPrecision[3,2]


as Plus==2, so you get a low precision 3 as output. On the other hand, with a space

3 +2


is unambiguous, the space breaks the ambiguity, no need to check precedence, and therefore interpreted as

Plus[SetPrecision[3,MachinePrecision],2]


which is 5. with MachinePrecision.

# Solution

I would suggest avoiding potentially ambiguous and hard-to-debug expressions like

3+2


and instead, use FullForm syntax to input your code, as such is the most unambiguous way to get the behaviour you expect.

Plus[SetPrecision[3,MachinePrecision],2]


Taking care of the number of spaces in an expression is not a desirable programming strategy for Mathematica.

• I should stress that $MachinePrecision and MachinePrecision are completely diffrent things. The first forces the arbitrary precision arithmetics, while the second is the actual machine precision number. You can see with Precision returning MachinePrecision that it is a MachinePrecision number, not an arbitrary precision. But Precision[SetPrecision[3,$MachinePrecision]] returns 15.954589770191003 what means that with SetPrecision[3, \$MachinePrecision] you get an arbitrary precision number. Jul 1 at 15:58
• @AlexeyPopkov thanks for the comment, I amended my answer. Jul 1 at 16:06
• Further, addition of any number to a machine precision number gives a machine precision number. Jul 1 at 16:15
• Corrected again @AlexeyPopkov, thanks Jul 1 at 16:20