[wp-trac] [WordPress Trac] #28559: Unit tests: ABSINT() returns bad result for large or long numbers

WordPress Trac noreply at wordpress.org
Thu Sep 18 13:49:31 UTC 2014


#28559: Unit tests: ABSINT() returns bad result for large or long numbers
-------------------------+------------------------------
 Reporter:  pbearne      |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  General      |     Version:  2.5
 Severity:  normal       |  Resolution:
 Keywords:  has-patch    |     Focuses:
-------------------------+------------------------------

Comment (by pbearne):

 Some throughts

 In PHP 5.6 "GMP objects now support operator overloading and casting to
 scalar types."
 http://php.net/manual/en/migration56.new-features.php

 Which means that we can do this -


 {{{
 function absint( $maybeint ) {
         if( 5.6 <= PHP_VERSION ){
                 return gmp_abs( gmp_invert( $maybeint ) );
         }
         return abs( intval( $maybeint ) );
 }
 }}}

 Not added as patch as I haven't got a PHP 5.6 install to test.

 Will this fly?

 Or we can get creative like this


 {{{
 function absint( $maybeint ) {
         $absint      = abs( intval( $maybeint ) );

         if( function_exists( 'gmp_abs' ) ) {
                 $gmp_absint = gmp_abs( gmp_invert( $maybeint ) );

                 if ( $absint != $gmp_absint ) {
                         return $gmp_absint;
                 }
         }

         return $absint;

 }}}

 This will return a gmp object if the number is over maxint and work's in
 pre 5.6

 Testing etc. needed :-)

--
Ticket URL: <https://core.trac.wordpress.org/ticket/28559#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list