Skip to content

Instantly share code, notes, and snippets.

@lcn2
Created October 10, 2023 14:53
Show Gist options
  • Save lcn2/94030138b0f52a4e6e4d1551409d7de4 to your computer and use it in GitHub Desktop.
Save lcn2/94030138b0f52a4e6e4d1551409d7de4 to your computer and use it in GitHub Desktop.
calc unexpected v2.15.0.1 output
calc always evaluates terms from left to right
==============================================
Calc has a definite order for evaluation of terms (adds in a
sum, factors in a product, arguments for a function or a matrix,
etc.). This order is always from left to right. but skipping of
terms may occur for ||, && and ? : .
Consider, for example:
A * B + C * D
In calc above expression is evaluated in the following order:
A
B
A * B
C
D
C * D
A * B + C * D
This order of evaluation is significant if evaluation of a
term changes a variable on which a later term depends. For example:
x++ * x++ + x++ * x++
in calc returns the value:
x * (x + 1) + (x + 2) * (x + 3)
and increments x as if by x += 4. Similarly, for functions f, g,
the expression:
f(x++, x++) + g(x++)
evaluates to:
f(x, x + 1) + g(x + 2)
and increments x three times.
In an other example, this expression:
1<<8/2
evaluates to 128, not 16, because <<8 is performed before the /2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment