From 2dda2e381c5a67de48d17973ec8f5c3a2c7324a9 Mon Sep 17 00:00:00 2001 From: Daimona Eaytoy Date: Sat, 7 Apr 2018 13:03:41 +0200 Subject: [PATCH] Convert division/multiplication/modulo results after calculation So that type and value will be identical to PHP's ones. Bug: T191688 Depends-On: I1140900cdda63eed292d9f20aefd721ef9247fcd Change-Id: I398c9a972b7e9fcb27d055d23939be2b8bb68244 --- includes/parser/AFPData.php | 18 +++++------------- tests/parserTests/float.t | 2 +- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/includes/parser/AFPData.php b/includes/parser/AFPData.php index a14d56689..16ef212be 100644 --- a/includes/parser/AFPData.php +++ b/includes/parser/AFPData.php @@ -357,18 +357,8 @@ class AFPData { * @throws AFPException */ public static function mulRel( $a, $b, $op, $pos ) { - // Figure out the type. - if ( $a->type == self::DFLOAT || $b->type == self::DFLOAT || - $a->toFloat() != $a->toString() || $b->toFloat() != $b->toString() - ) { - $type = self::DFLOAT; - $a = $a->toFloat(); - $b = $b->toFloat(); - } else { - $type = self::DINT; - $a = $a->toInt(); - $b = $b->toInt(); - } + $a = $a->toNumber(); + $b = $b->toNumber(); if ( $op != '*' && $b == 0 ) { throw new AFPUserVisibleException( 'dividebyzero', $pos, [ $a ] ); @@ -385,10 +375,12 @@ class AFPData { throw new AFPException( "Invalid multiplication-related operation: {$op}" ); } - if ( $type == self::DINT ) { + if ( $data === (int)$data ) { $data = intval( $data ); + $type = self::DINT; } else { $data = floatval( $data ); + $type = self::DFLOAT; } return new AFPData( $type, $data ); diff --git a/tests/parserTests/float.t b/tests/parserTests/float.t index 9ce8c9194..792f19d65 100644 --- a/tests/parserTests/float.t +++ b/tests/parserTests/float.t @@ -1 +1 @@ -(5 / 2 = 2) & (5. / 2 = 2.5) & (5 / 2. = 2.5) & (int(.5) = 0) +(5 / 2 = 2.5) & (5. / 2 = 2.5) & (5 / 2. = 2.5) & (int(.5) = 0)