In mathematics, a binary operation is commutative if changing the order of the operands does not change the result. It is a fundamental property of many binary operations, and many mathematical proofs depend on it. The name is needed because there are operations, such as division and subtraction, that do not have it (for example, "3 − 5 ≠ 5 − 3"); such operations are not commutative, and so are referred to as noncommutative operations. The idea that simple operations such as the multiplication and addition of numbers are commutative, was for many years implicitly assumed.
Questions tagged [commutativity]
61 questions
48
votes
4 answers
Is floating point addition commutative in C++?
For floating point values, is it guaranteed that a + b is the same as1 b + a?
I believe this is guaranteed in IEEE754, however the C++ standard does not specify that IEEE754 must be used. The only relevant text seems to be from [expr.add]#3:
The…

M.M
- 138,810
- 21
- 208
- 365
24
votes
3 answers
Relax ordering constraints in monadic computation
here is some food for thought.
When I write monadic code, the monad imposes ordering on the operations done. For example, If I write in the IO monad:
do a <- doSomething
b <- doSomethingElse
return (a + b)
I know doSomething will be executed…

Edward Z. Yang
- 26,325
- 16
- 80
- 110
22
votes
4 answers
3 Equals or Case Equality operator
In Ruby Integer === 5 returns true. Similarly String === "karthik" returns true.
However, 5 === Integer returns false. And "karthik" === String.
Why is the operator not commutative?

karthiks
- 7,049
- 7
- 47
- 62
21
votes
2 answers
Argument order for '==' with Nullable
The following two C# functions differ only in swapping the left/right order of arguments to the equals operator, ==. (The type of IsInitialized is bool). Using C# 7.1 and .NET 4.7.
static void A(ISupportInitialize x)
{
if ((x as…

Glenn Slayden
- 17,543
- 3
- 114
- 108
18
votes
2 answers
How do I find out whether a monad is commutative?
The documentation for Control.Monad.List.ListT states that it "does not yield a monad unless the argument monad is commutative."
How do I find out whether a monad is commutative? Is there a CommutativeMonad typeclass? Should there be?
In…

dave4420
- 46,404
- 6
- 118
- 152
18
votes
2 answers
Babel plugins run order
TL;DR: Is there a way how to specify the order in which the Babel plugins are supposed to be run? How does Babel determine this order? Is there any spec how this works apart from diving into Babel sources?
I'm developing my own Babel plugin. I…

Tomas Kulich
- 14,388
- 4
- 30
- 35
12
votes
2 answers
Commutative Property for Haskell Operators?
Is there a way to state that an operator is commutative, so that I don't have to give identical definitions for both directions? For instance:
data Nat = Zero | Succ Nat
(+) :: Nat -> Nat -> Nat
Zero + x = x
x + Zero = x
...
Here, is there a way…

Mark Anastos
- 365
- 3
- 14
11
votes
2 answers
Addition of NA and expression that evaluates to NaN return different results depending on order, violation of the commutative property?
I am investigating corner cases of numeric operations in R. I came across the following particular case involving zero divided by zero:
(0/0)+NA
#> [1] NaN
NA+(0/0)
#> [1] NA
Created on 2021-07-10 by the reprex package (v2.0.0)
Session…

Paul Gourdin
- 111
- 2
10
votes
4 answers
How can I specify that two operations commute in a typeclass?
I started reading this paper on CRDTs, which is a way of sharing modifiable data concurrently by ensuring that the operations that modify the data are commutative. It seemed to me that this would be a good candidate for abstraction in Haskell -…

rampion
- 87,131
- 49
- 199
- 315
8
votes
2 answers
Automatically and deterministicly testing a function for associativity, commutativity etc
Is it possible to construct a higher order function isAssociative that takes another function of two arguments and determines whether that function is associative?
Similar question but for other properties such as commutativity as well.
If this is…

TheIronKnuckle
- 7,224
- 4
- 33
- 56
8
votes
3 answers
Commutative operator overloading + of 2 different objects
I have 2 classes which represent a matrix:
1. RegularMatrix - O(n^2) representation
2. SparseMatrix - a matrix that is represented as linked list (without zeros).
lets say i have:
RegularMatrix a;
SparseMatrix b;
i want to be able to do:
a+b;
and…

Asher Saban
- 4,673
- 13
- 47
- 60
8
votes
2 answers
Alternative to express "Commutativity" in Prolog?
as a beginner to Prolog, I found the commutative expression in Prolog are quite not intuitive.
for example if I want to express X and Y are in one family, like:
family(X,Y) :-
married(X,Y);
relative(X,Y);
father_son(X,Y).
I should…

Matt
- 741
- 1
- 6
- 17
6
votes
1 answer
Truly unordered foldable bag
I want a Bag container which hides its 'real' order from its clients.
It also must be fully polymorphic, that is shouldn't require any constraints over its element type.
I found at least three implementations of bags: Bag module from ghc package,…

nponeccop
- 13,527
- 1
- 44
- 106
5
votes
3 answers
Is groupby from pandas commutative?
I would like to know if the rows selected by:
groupby(['a', 'b'])
are the same as the rows selected by:
groupby(['b', 'a'])
In this case the order of the rows doesn't matter.
Is there any case in which groupby does not fulfill the commutative…

cristian hantig
- 1,110
- 1
- 12
- 16
5
votes
2 answers
Non-commutative sympify (or simplify)
I would like to be able to simplify mathematical expressions from a string in Python.
There are several "commutative" ways of doing it.
Is there a non-commutative function for that?
I know that sympify from sympy can do some non-commutative jobs,…

m_gnacik
- 153
- 1
- 5