Implementation of the POW (**) Operator#392
Conversation
|
Hi @tedpatrick, just from a quick glance I noticed you didn't add the operator for the One other note is the inclusion of the guide to add operators doesn't quite fit the purpose of the docs section in my opinion. I'm sure @antocuni will confirm one or another for that one. |
C layer:
Python layer:
Tests:
|
…p condition for unsigned types
… lambda expressions and improve formatting
… int and float types
|
Closes #175 |
Viriathus1
left a comment
There was a problem hiding this comment.
Thanks for the changes on this one. There's a few minor things that still need to be rectified. It needs to conform to Python specs more broadly as described here. The following is currently unaccounted for:
- For int operands, the result must be a float if the exponent arg is negative.
- Zero with a negative exponent raises a ZeroDivisionError exception.
- Negative base with fractional exponent returns a complex number - eg:
(-5) ** 0.5 == (1.3691967456605067e-16+2.23606797749979j)
…e cases for zero and negative exponents
|
Hi @tedpatrick, thanks for the changes. I see that you widen the result to What do you think @antocuni? Union types are probably required here which have yet to be implemented. |
This pull request adds support for the power operator (
**) across the SPy virtual machine and its standard numeric types, including both integer and floating-point types. It introduces new operator implementations, updates the operator dispatch tables, and adds comprehensive tests to ensure correct behavior for power operations. Additionally, it improves the atomicity of cache file writes to prevent issues with concurrent access.Power operator (
**) support**operator to the operator dispatch tables for all relevant numeric types (i8,u8,i32,u32,f32,f64) inspy/vm/modules/operator/binop.pyand registered the corresponding implementations. [1] [2] [3] [4] [5] [6] [7] [8] [9]w_POWoperator dispatch function, which handles the power operation and falls back to__pow__if available.w_f32_powinopimpl_f32.py,w_f64_powinopimpl_f64.py, and generic integerw_powinopimpl_int.py. [1] [2] [3]spy/libspy/include/spy/operator.handspy/libspy/src/operator.c. [1] [2] [3]Testing
**operator for both integer and floating-point types, including tests for negative bases, zero exponents, and fractional exponents, inspy/tests/compiler/test_int.pyandspy/tests/compiler/test_float.py. [1] [2]Reliability improvements
spy/analyze/importing.pyby writing to a temporary file and renaming it, preventing partial writes in concurrent environments.