Skip to content

Stop mod() and pow() from truncating float operands - #28

Merged
dereuromark merged 1 commit into
masterfrom
bugfix/float-operands
Oct 11, 2026
Merged

dereuromark merged 1 commit into
masterfrom
bugfix/float-operands

Conversation

@dereuromark

Copy link
Copy Markdown
Contributor

mod() and pow() typed their operand as self|string|int. For callers that do not declare strict types, PHP converts a float to an integer before the method runs:

Call Before Now
Decimal::create('7.0')->mod(2.5) 1.0 (calculated 7 mod 2) 2.0
Decimal::create('4')->pow(0.5) 1 (calculated 4 ** 0) ValueError, as for '0.5'
Decimal::create('4')->pow(2.0) 16 16

The only sign was a PHP deprecation notice ("Implicit conversion from float 2.5 to int loses precision"). Callers with strict types got a TypeError and 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.php has no strict_types on 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() or pow() with the old, narrower parameter type has to widen it as well.

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.
Copilot AI balanced review requested due to automatic review settings October 11, 2026 15:39
@dereuromark dereuromark added the bug Something isn't working label Oct 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Oct 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.53%. Comparing base (348c9b2) to head (d650bbf).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dereuromark
dereuromark merged commit cf005ef into master Oct 11, 2026
7 checks passed
@dereuromark
dereuromark deleted the bugfix/float-operands branch October 11, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants