#42294 [Fbk]: round will not use PHP_ROUND_FUZZ on 64bit CPUs

From: Date: Thu, 16 Aug 2007 10:30:57 +0000
Subject: #42294 [Fbk]: round will not use PHP_ROUND_FUZZ on 64bit CPUs
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-116162@lists.php.net to get a copy of this message
 ID:               42294
 Updated by:       jani@php.net
 Reported By:      oliver at teqneers dot de
 Status:           Feedback
 Bug Type:         *Configuration Issues
 Operating System: OpenSuSE 10.2
 PHP Version:      5.2.4RC1
 New Comment:

Output for me on a 32bit system:
0.285 - 0.28
1.285 - 1.29
1.255 - 1.25



Previous Comments:
------------------------------------------------------------------------

[2007-08-16 10:29:52] jani@php.net

Those results you get are NOT incorrect. If you round using 2 decimals,
of course that's what you get.

------------------------------------------------------------------------

[2007-08-15 11:25:09] oliver at teqneers dot de

this is an example of the comment block of the round documentation
(http://de.php.net/manual/en/function.round.php):

<?php

printf("0.285 - %s <br> ",round(0.285,2));      
// incorrect result 0.28

printf("1.285 - %s <br> ",round(1.285,2));      
// correct result 1.29

printf("1.255 - %s <br><br>",round(1.255,2));   
// incorrect result 1.25

?>

------------------------------------------------------------------------

[2007-08-15 08:47:36] jani@php.net

Can you please provide a short PHP code example which fails because of
this..?

------------------------------------------------------------------------

[2007-08-14 10:46:19] oliver at teqneers dot de

Description:
------------
Because of the nature of floats to be not exact enough in binary
system, PHP uses a kind of fuzzy login to round more precisely turning
on PHP_ROUND_FUZZ in some cases.

The problem is the "configure" check. The check does not work for AMD
nor INTEL 64bit CPUs.

The number to test against is not sufficient. So maybe it is possible
to add or change it???

Reproduce code:
---------------
#include <math.h>
  /* keep this out-of-line to prevent use of gcc inline floor() */
  double somefn(double n) {
    return floor(n*pow(10,2) + 0.5);
  }
  int main() {
    return somefn(0.045)/10.0 != 0.5;
  }

this will return 1 on a 32bit but 0 on a 64bit engine.

Expected result:
----------------
it should also fail on a 64bit and return 1. The following code would
actually work on 64bit, because it will test 0.45 as well as 0.285:


#include <math.h>
  /* keep this out-of-line to prevent use of gcc inline floor() */
  double somefn(double n) {
    return floor(n*pow(10,2) + 0.5);
  }
  int main() {
    return (somefn(0.285)/100.0 != 0.29) || (somefn(0.045) / 10.0 !=
0.5);
  }

Actual result:
--------------
Instead of using the fuzzy round, PHP just takes 0.5 and
adds/substracts it from the given float (math.c)


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=42294&edit=1


Thread (15 messages)

« previous php.bugs (#116162) next »