Repository navigation
Stop mod() and pow() from truncating float operands - #28
Merged
Merged
Conversation
Both methods typed their operand as self|string|int. For a caller that does not declare strict types, PHP converts a float to an integer before the method runs, so mod(2.5) calculated mod 2 and pow(0.5) calculated the power of 0. The only sign was a PHP deprecation notice. Callers with strict types got a TypeError instead. The types now list float, like every other operation. mod() handles it as the value it is, and pow() reports a fractional exponent with the same error a string such as '0.5' already produced. tests/data/float_operands.php has no strict_types on purpose, to exercise the call the way an affected caller makes it.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #28 +/- ##
=========================================
Coverage 97.53% 97.53%
Complexity 195 195
=========================================
Files 1 1
Lines 406 406
=========================================
Hits 396 396
Misses 10 10 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
mod()andpow()typed their operand asself|string|int. For callers that do not declare strict types, PHP converts a float to an integer before the method runs:Decimal::create('7.0')->mod(2.5)1.0(calculated7 mod 2)2.0Decimal::create('4')->pow(0.5)1(calculated4 ** 0)ValueError, as for'0.5'Decimal::create('4')->pow(2.0)1616The only sign was a PHP deprecation notice ("Implicit conversion from float 2.5 to int loses precision"). Callers with strict types got a
TypeErrorand were not affected.Both types now list
float, like every other operation. The signatures are older than 1.4, so this affects all 1.x releases.tests/data/float_operands.phphas nostrict_typeson purpose, so one test makes the call the way an affected caller does. The four new tests fail without the change.A subclass that overrides
mod()orpow()with the old, narrower parameter type has to widen it as well.