# Differences of Quantities gives different result in different Mathematica version

In Mathematica 12.2

Differences[{Quantity[1, "mm"], Quantity[1, "mm"]}]


gives

{Quantity[0, "Millimeters"]}


In Mathematica 13.1, same expression gives

{0}


with no unit!

This introduces inconsistency. For example

StandardDeviation@
Differences[{Quantity[1, "mm"], Quantity[1, "mm"], Quantity[2, "mm"],
Quantity[3, "mm"]}]


prompts errors in 13.1, while no problem in 12.2

In the documentaion of Differences, it says

Differences[list] is equivalent to ListConvolve[{1,-1},list].

But it is not!

ListConvolve[{1, -1}, {Quantity[1, "mm"], Quantity[1, "mm"]}]


gives

{Quantity[0, "Millimeters"]}


What is changed for Differences in 13.1? It seems like a bug. Are there some other side effects of this potential change?

Update

A patch I can think of is

Unprotect[Differences]
Differences[x : {__Quantity}] := ListConvolve[{1, -1}, x];
Protect[Differences]


But I do not know if Unprotect and Protect a built-in function will have undesirable side effect or not. Anyone knows?

• As a workaround, Differences[list] /. {0 -> Quantity[0, QuantityUnit[list[]]]} Jul 21, 2022 at 15:21
• @BobHanlon Thank you for comment. I just add my workround in my post. But I do not know if it is proper or not Jul 21, 2022 at 16:50
• Looks like a bug to me. 0 mm is definitely not the same as 0, see here for an extended comment. Jul 21, 2022 at 17:00
• Minimal example: compare Quantity[1, "Millimeters"] - Quantity[1, "Millimeters"] with Subtract[Quantity[1, "Millimeters"], Quantity[1, "Millimeters"]] Jul 21, 2022 at 17:57
• @JasonB. So we need one more patch for Subtract. I even guess Subtract and Differences are related in the internal. Jul 22, 2022 at 1:22