feat(phase-4): Type-Safe Arithmetic & Comparisons#118
Open
davydog187 wants to merge 1 commit intomainfrom
Open
feat(phase-4): Type-Safe Arithmetic & Comparisons#118davydog187 wants to merge 1 commit intomainfrom
davydog187 wants to merge 1 commit intomainfrom
Conversation
Improve float division by zero handling to approximate Lua 5.3 behavior. Instead of raising errors, float division by zero now returns very large numbers as approximations of infinity. Changes: - Update safe_divide to handle division by zero cases - Positive / 0 returns 1.0e308 (approximating +inf) - Negative / 0 returns -1.0e308 (approximating -inf) - 0 / 0 returns 0.0 (approximating NaN) - Update tests to reflect the approximation approach - Document limitation vs true Lua 5.3 infinity/NaN support Note: Elixir/Erlang's strict arithmetic makes true IEEE 754 infinity/NaN difficult to create. This is a known limitation. Arithmetic type checking (Phase 4A) was already complete: - All arithmetic operations use safe_* helpers with type checking - TypeError raised for non-numeric operands - String-to-number coercion follows Lua semantics Comparison safety (Phase 4C) was already complete: - == and != work on any types - <, <=, >, >= work on numbers or strings (same type) - TypeError raised for incompatible type comparisons - Lexicographic string comparison All 1,011 tests passing. Implements Phase 4 from plan.md
Contributor
Author
|
I'm not sure that we want to do this, I think we should potentially diverge from Lua 5.3 in this case |
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.
Type-Safe Arithmetic & Comparisons
Goal
Proper Lua 5.3 arithmetic semantics with clear error messages.
Implementation
Float Division by Zero: Updated to approximate Lua 5.3 behavior:
positive / 0returns1.0e308(approximating +inf)negative / 0returns-1.0e308(approximating -inf)0 / 0returns0.0(approximating NaN)Note: True IEEE 754 infinity/NaN are difficult to create in Elixir/Erlang due to strict arithmetic checks. This is a documented limitation.
Arithmetic Type Checking (already complete):
safe_*helpers with type checkingTypeErrorraised for non-numeric operands with clear messagesComparison Safety (already complete):
==and~=work on any types (return false for different types)<,<=,>,>=only work on numbers or strings (same type)TypeErrorraised for incompatible type comparisonsChanges
lib/lua/vm/executor.ex- Updatedsafe_divideto handle division by zerotest/lua/vm/arithmetic_test.exs- Updated division by zero testsVerification
mix formatcompletedmix compile --warnings-as-errorspassesmix test --exclude pendingpasses (1,011 tests)