4
$\begingroup$

In previous versions of Mathematica Missing[]- Missing[] would output Missing[]- Missing[]. In the current version is outputs 0. Is there a way to get Mathematica to revert back to the old output? The new implementation is reeking havoc with my archival code.

$\endgroup$
5
  • 1
    $\begingroup$ Missing was introduced in 10.0. I just verified that in 10.0.2, Missing[]-Missing[] evaluates to 0. Missing[] + Missing[] evaluates to 2 Missing[]. I have not used Missing much, but as I remember it never behaved specially with arithmetic (like Indeterminate and Undefined do). $\endgroup$
    – Szabolcs
    Commented Jan 18, 2018 at 20:29
  • $\begingroup$ Perhaps what you mean is that Dataset handles Missing specially, and causes the arithmetic results you are referring to. Missing[] itself doesn't have special properties, and doesn't evaluate specially outside of Dataset. If you see changed behaviour with Dataset, please show an example in the question. $\endgroup$
    – Szabolcs
    Commented Jan 18, 2018 at 20:32
  • 1
    $\begingroup$ For example, in 11.2, ds = Dataset[{1, Missing[], 2}]; ds[# - #&] // Normal results in {0, Missing["Indeterminate"], 0} and not {0, 0, 0}. I.e. the special behaviour inside Dataset is still present and works as before. $\endgroup$
    – Szabolcs
    Commented Jan 18, 2018 at 20:33
  • $\begingroup$ Replacing Missing with Indeterminate solve a whole bunch of issues. $\endgroup$
    – Wintermute
    Commented Jan 19, 2018 at 19:24
  • $\begingroup$ Comments are not for extended discussion; this conversation has been moved to chat. $\endgroup$
    – Kuba
    Commented Jan 23, 2018 at 21:27

1 Answer 1

2
$\begingroup$

You could try something like:

Unprotect[Missing];
Missing /: Plus[a_Missing, b__] := Indeterminate
Protect[Missing];

Your example:

Missing[] - Missing[]

Indeterminate

Addendum

In addition to the approaches in the comments, you could also Block Missing when performing computations. For example:

SetAttributes[missingArithmetic, HoldFirst];

missingArithmetic[expr_] := Block[{Missing = Indeterminate&},
    expr
]

Examples:

missingArithmetic[Missing[]-Missing[]]
missingArithmetic[1-Missing[]]
missingArithmetic[Sin[Missing[]]/Sin[Missing[]]]

Indeterminate

Indeterminate

Indeterminate

$\endgroup$
5
  • $\begingroup$ But what about 1 - Missing[]? Sorry, couldn't resist :-) $\endgroup$
    – Szabolcs
    Commented Jan 18, 2018 at 21:10
  • $\begingroup$ You're right, that's a case where UpValues won't work. $\endgroup$
    – Carl Woll
    Commented Jan 18, 2018 at 21:15
  • $\begingroup$ But you can add an UpValue to Times. However, it gets messy quickly, as it is unclear what set of functions should be considered. $\endgroup$
    – Szabolcs
    Commented Jan 18, 2018 at 21:23
  • 1
    $\begingroup$ How about Missing /: head_Symbol[___, _Missing, ___] := Indeterminate /; MemberQ[Attributes[head], NumericFunction]? It does feel dangerous and I imagine it might have a noticeable performance impact ... I haven't tested. But it does work even on Sin[Missing[]]. $\endgroup$
    – Szabolcs
    Commented Jan 18, 2018 at 21:27
  • $\begingroup$ or Missing /: (_?System`Dump`HeldNumericFunctionQ)[___, _Missing, ___] := Indeterminate? $\endgroup$
    – kglr
    Commented Jan 18, 2018 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.