Skip to content

Commit b5bba3a

Browse files
committed
fix mod
1 parent 72d9bb7 commit b5bba3a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

crates/luars/src/compiler/expr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,8 @@ fn compile_binary_expr_to(
10871087
BinaryOperator::OpMul => Some(left_val * right_val),
10881088
BinaryOperator::OpDiv => Some(left_val / right_val),
10891089
BinaryOperator::OpIDiv => Some((left_val / right_val).floor()),
1090-
BinaryOperator::OpMod => Some(left_val % right_val),
1090+
// Lua modulo: a % b = a - floor(a/b) * b (same sign as divisor)
1091+
BinaryOperator::OpMod => Some(left_val - (left_val / right_val).floor() * right_val),
10911092
BinaryOperator::OpPow => Some(left_val.powf(right_val)),
10921093
BinaryOperator::OpBAnd => Some((left_int & right_int) as f64),
10931094
BinaryOperator::OpBOr => Some((left_int | right_int) as f64),
@@ -1154,7 +1155,8 @@ fn compile_binary_expr_to(
11541155
BinaryOperator::OpMul => Some(left_val * right_val),
11551156
BinaryOperator::OpDiv => Some(left_val / right_val),
11561157
BinaryOperator::OpIDiv => Some((left_val / right_val).floor()),
1157-
BinaryOperator::OpMod => Some(left_val % right_val),
1158+
// Lua modulo: a % b = a - floor(a/b) * b (same sign as divisor)
1159+
BinaryOperator::OpMod => Some(left_val - (left_val / right_val).floor() * right_val),
11581160
BinaryOperator::OpPow => Some(left_val.powf(right_val)),
11591161
// Bitwise operations require integers
11601162
BinaryOperator::OpBAnd

0 commit comments

Comments
 (0)