Skip to content

Stdlib: floor()/ceil()/round()/fmod() — numeric-string scalar coercion (ext/standard/math.c parity) #4350

Description

@PurHur

Category

stdlib

Problem

Math builtins reject numeric strings where Zend coerces via _convert_to_double(). Example: floor("3.7") returns 3.0 in Zend but throws LogicException here.

php-src reference

  • ext/standard/math.cPHP_FUNCTION(floor), PHP_FUNCTION(ceil), PHP_FUNCTION(round), PHP_FUNCTION(fmod)
  • Zend/zend_operators.c_convert_to_double() / numeric-string rules

Repro

<?php
declare(strict_types=1);

foreach (['floor', 'ceil', 'round'] as $fn) {
    echo $fn, '("3.7")=', $fn('3.7'), "\n";
}
echo 'fmod("5","2")=', fmod('5', '2'), "\n";
HARNESS_DOCKER_RUN_OPTS='--memory=8g --cpus=2' ./script/docker-exec.sh -- bash -lc '
  source script/php-env.sh
  php repro.php
  php bin/vm.php repro.php
'
Call Zend VM
floor("3.7") 3.0 LogicException: floor() only supports integers and floats
ceil("3.1") 4.0 same pattern
round("3.5") 4.0 same pattern
fmod("5","2") 1.0 same pattern

Scope

  • ext/standard/floor.php, ceil.php, round.php, fmod.php — use shared numeric coercion helper (see VmReflection::numericArg() / _convert_to_double parity used elsewhere)
  • Matching JIT stubs — JitLongArg / float lowering after coercion
  • Tests: test/compliance/cases/stdlib/math_numeric_string.phpt

Done when

  • Repro output matches Zend under bin/vm.php
  • Non-numeric strings still throw TypeError like Zend (not silent 0)
  • ./script/ci-fast.sh --filter math_numeric_string green

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions