use math.powi/pow for power operator instead of bitwise xor#36
Open
ylluminate wants to merge 1 commit intovlang:masterfrom
Open
use math.powi/pow for power operator instead of bitwise xor#36ylluminate wants to merge 1 commit intovlang:masterfrom
ylluminate wants to merge 1 commit intovlang:masterfrom
Conversation
Contributor
Author
|
Closing for revision — will resubmit after review. |
a19e721 to
4a7db67
Compare
fb9d987 to
6d2129b
Compare
e571ef7 to
99705d4
Compare
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.
99705d4 to
3aff57f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Python's
**operator was mapped to V's^which is bitwise XOR, not exponentiation.2 ** 10produced2 ^ 10 = 8instead of1024.Uses
math.powi()for integer operands (preserves int type) andmath.pow()for float operands or negative exponents (Python auto-promotes2**-1to0.5). Also handles**=augmented assignment by expanding to assignment form since V has no**=operator.Changes
visit_binop()intranspiler.v: Pow handler with type-aware dispatch tomath.powi(int) ormath.pow(float/negative exponent)visit_aug_assign()intranspiler.v: Pow special-case expandsa **= btoa = math.powi(a, b)ora = math.pow(a, b)UnaryOp(USub, ...)forces float pathop_to_symbol()intypes.v: Pow maps to**(not a V operator)**=augmented assignment and negative exponentsAll 110 tests pass.
Known limitations (follow-up)
math.powireturns i64, may need cast in typed contextsx = -1; 2 ** x) not detected at transpile time