php / php-src

The PHP Interpreter
https://www.php.net
Other
38.19k stars 7.75k forks source link

sprintf %.17g negative infinity bug #15964

Open divinity76 opened 1 month ago

divinity76 commented 1 month ago

Description

The following code:

<?php
var_dump(sprintf("%.17g", -INF));

Resulted in this output:

string(3) "INF"

But I expected this output instead:

string(4) "-INF"

PHP Version

PHP 8.3.11

Operating System

Ubuntu 24.04

divinity76 commented 1 month ago

duplicate of https://github.com/php/php-src/issues/9209

cmb69 commented 1 month ago

duplicate of #9209

Nope, not really. That is about truncation of INF due to very low precision, but the reported issue here is about loosing the minus sign.

cmb69 commented 1 month ago

Also note that the problem is not particularly related to the precision, see https://3v4l.org/3F8nm. The only case where the minus sign would properly be retained is for padding="0", e.g. printf("%06g", -INF);, but that apparently looses the "I". Having had more test cases could have helped.