Use integers in addition and subtractions

Right now they're always returned as float values, even stuff like 1+1.
With these patch the results will have the same type as they would with
pure PHP calculation. Added a method to convert numbers to int/float
depending on their type.

Bug: T191688
Change-Id: I1140900cdda63eed292d9f20aefd721ef9247fcd
This commit is contained in:
Daimona Eaytoy 2018-04-07 12:55:07 +02:00
parent efde52e4b6
commit be076eb97e

View file

@ -405,7 +405,12 @@ class AFPData {
} elseif ( $a->type == self::DLIST && $b->type == self::DLIST ) {
return new AFPData( self::DLIST, array_merge( $a->toList(), $b->toList() ) );
} else {
return new AFPData( self::DFLOAT, $a->toFloat() + $b->toFloat() );
$res = $a->toNumber() + $b->toNumber();
if ( $res === (int)$res ) {
return new AFPData( self::DINT, $res );
} else {
return new AFPData( self::DFLOAT, $res );
}
}
}
@ -415,7 +420,12 @@ class AFPData {
* @return AFPData
*/
public static function sub( $a, $b ) {
return new AFPData( self::DFLOAT, $a->toFloat() - $b->toFloat() );
$res = $a->toNumber() - $b->toNumber();
if ( $res === (int)$res ) {
return new AFPData( self::DINT, $res );
} else {
return new AFPData( self::DFLOAT, $res );
}
}
/** Convert shorteners */
@ -477,6 +487,13 @@ class AFPData {
return self::castTypes( $this, self::DINT )->data;
}
/**
* @return int|float
*/
public function toNumber() {
return $this->type == self::DINT ? $this->toInt() : $this->toFloat();
}
public function toList() {
return self::castTypes( $this, self::DLIST )->data;
}