php / php/php-src

max/min return wrong value if signed (negative) zero

Open
#20,221 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.2k
Avg merge
2d 15h
Merged PRs (30d)
103

Description

Description

The following code:

https://3v4l.org/JXU4T#v8.4.13

<?php
var_dump( min( round( -0.01 / 2, 0 ), 0 ) );
var_dump( max( round( -0.01 / 2, 0 ), 0 ) );

Resulted in this output:

int(0)
float(-0)

But I expected this output instead:

float(-0)
int(0)

For 99.9% of applications, this output is actually what people would expect however: (instead of -0)

float(0)
int(0)

so I guess it would make sense to also change that.

This does not have any impact on calculations or comparisons, this is purely visual but can lead to unexpected behavior if the returned number is used as a string (e.g. substr, strtok,...) since there suddenly is an additional character in front of the numbers.
This is especially unexpected, when the float is the result of float arithmetics, which can minimally negative numbers in some cases

PHP Version
8.4
Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the linked 3v4l example on PHP 8.4, then trace the min() and max() entry points that handle signed zero. Determine whether the intended fix preserves -0 or normalizes it to 0, and verify the chosen behavior with regression coverage for the reported calls.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.