Skip to content

Commit

Permalink
libfpvm: use C operator precedence
Browse files Browse the repository at this point in the history
Placing each operator at its own precedence level subtly alters the
evaluation order for +, -, *, and /, which can produce unexpected
results where rounding errors are considered.

More significantly, the precendence of % differs from that used in
C (and various other languages). For example,

2 * 3.1 % 2  with C precedence would be
(2 * 3.1) % 2 = 6.2 % 2 = 0.2   (unlike C's %, our % if really fmod)

By giving % higher precedence than *, we get
2 * (3.1 % 2) = 2 * 1.1 = 2.2  instead.

I don't know if this difference is intentional, e.g., required for
compatibility with Milkdrop. But it does at least look suspicious.

- Werner
  • Loading branch information
wpwrak authored and Sebastien Bourdeauducq committed Dec 3, 2011
1 parent 72005cf commit 36b1e07
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions software/libfpvm/parser.y
Expand Up @@ -53,11 +53,8 @@ node(N) ::= TOK_IDENT(I). {
N->contents.branches.c = NULL;
}

%left TOK_PLUS.
%left TOK_MINUS.
%left TOK_MULTIPLY.
%left TOK_DIVIDE.
%left TOK_PERCENT.
%left TOK_PLUS TOK_MINUS.
%left TOK_MULTIPLY TOK_DIVIDE TOK_PERCENT.
%left TOK_NOT.

node(N) ::= node(A) TOK_PLUS node(B). {
Expand Down

0 comments on commit 36b1e07

Please sign in to comment.