Skip to content

use math.powi/pow for power operator instead of bitwise xor#36

Open
ylluminate wants to merge 1 commit intovlang:masterfrom
ylluminate:fix-power-operator
Open

use math.powi/pow for power operator instead of bitwise xor#36
ylluminate wants to merge 1 commit intovlang:masterfrom
ylluminate:fix-power-operator

Conversation

@ylluminate
Copy link
Contributor

@ylluminate ylluminate commented Feb 20, 2026

Python's ** operator was mapped to V's ^ which is bitwise XOR, not exponentiation. 2 ** 10 produced 2 ^ 10 = 8 instead of 1024.

Uses math.powi() for integer operands (preserves int type) and math.pow() for float operands or negative exponents (Python auto-promotes 2**-1 to 0.5). Also handles **= augmented assignment by expanding to assignment form since V has no **= operator.

Changes

  • visit_binop() in transpiler.v: Pow handler with type-aware dispatch to math.powi (int) or math.pow (float/negative exponent)
  • visit_aug_assign() in transpiler.v: Pow special-case expands a **= b to a = math.powi(a, b) or a = math.pow(a, b)
  • Negative exponent detection: UnaryOp(USub, ...) forces float path
  • op_to_symbol() in types.v: Pow maps to ** (not a V operator)
  • Test cases added for **= augmented assignment and negative exponents
  • 3 test expected outputs updated (numeric_ops, math_func)

All 110 tests pass.

Known limitations (follow-up)

  • math.powi returns i64, may need cast in typed contexts
  • Variable negative exponents (x = -1; 2 ** x) not detected at transpile time

@ylluminate
Copy link
Contributor Author

Closing for revision — will resubmit after review.

@ylluminate ylluminate closed this Feb 20, 2026
@ylluminate ylluminate reopened this Feb 20, 2026
@ylluminate ylluminate changed the title fix: use math.pow() for power operator instead of ^ (XOR) use math.pow for power operator instead of bitwise xor Feb 20, 2026
@ylluminate ylluminate force-pushed the fix-power-operator branch 2 times, most recently from fb9d987 to 6d2129b Compare February 20, 2026 05:54
@ylluminate ylluminate changed the title use math.pow for power operator instead of bitwise xor use math.powi/pow for power operator instead of bitwise xor Feb 20, 2026
@ylluminate ylluminate force-pushed the fix-power-operator branch 4 times, most recently from e571ef7 to 99705d4 Compare February 20, 2026 20:53
python's ** operator was mapped to V's ^ which is bitwise XOR, not
exponentiation. 2 ** 10 produced 2 ^ 10 = 8 instead of 1024.

use math.powi() for integer operands and math.pow() for float operands
or negative exponents (python auto-promotes 2**-1 to 0.5). also handle
**= augmented assignment by expanding to assignment form since V has
no **= operator.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant