2
2
3
3
namespace Litipk \BigNumbers ;
4
4
5
+ use Litipk \BigNumbers \DecimalConstants as DecimalConstants ;
5
6
use Litipk \BigNumbers \InfiniteDecimal as InfiniteDecimal ;
6
7
7
8
use Litipk \Exceptions \NotImplementedException as NotImplementedException ;
@@ -66,8 +67,9 @@ public static function getNegativeInfinite()
66
67
/**
67
68
* Decimal "constructor".
68
69
*
69
- * @param mixed $value
70
- * @param integer $scale
70
+ * @param mixed $value
71
+ * @param integer $scale
72
+ * @return Decimal
71
73
*/
72
74
public static function create ($ value , $ scale = null )
73
75
{
@@ -286,7 +288,7 @@ public function mul(Decimal $b, $scale = null)
286
288
if ($ b ->isInfinite ()) {
287
289
return $ b ->mul ($ this );
288
290
} elseif ($ b ->isZero ()) {
289
- return Decimal:: fromInteger ( 0 , $ scale );
291
+ return DecimalConstants:: Zero ( );
290
292
}
291
293
292
294
return self ::fromString (
@@ -311,10 +313,8 @@ public function div(Decimal $b, $scale = null)
311
313
312
314
if ($ b ->isZero ()) {
313
315
throw new \DomainException ("Division by zero is not allowed. " );
314
- } elseif ($ this ->isZero ()) {
315
- return self ::fromDecimal ($ this , $ scale );
316
- } elseif ($ b ->isInfinite ()) {
317
- return Decimal::fromInteger (0 , $ scale );
316
+ } elseif ($ this ->isZero () || $ b ->isInfinite ()) {
317
+ return DecimalConstants::Zero ();
318
318
} else {
319
319
if ($ scale !== null ) {
320
320
$ divscale = $ scale ;
@@ -356,7 +356,7 @@ public function sqrt($scale = null)
356
356
"Decimal can't handle square roots of negative numbers (it's only for real numbers). "
357
357
);
358
358
} elseif ($ this ->isZero ()) {
359
- return Decimal:: fromDecimal ( $ this , $ scale );
359
+ return DecimalConstants:: Zero ( );
360
360
}
361
361
362
362
$ sqrt_scale = ($ scale !== null ? $ scale : $ this ->scale );
@@ -385,7 +385,7 @@ public function pow(Decimal $b, $scale = null)
385
385
);
386
386
}
387
387
} elseif ($ b ->isZero ()) {
388
- return Decimal:: fromInteger ( 1 , $ scale );
388
+ return DecimalConstants:: One ( );
389
389
} elseif ($ b ->scale == 0 ) {
390
390
$ pow_scale = $ scale === null ?
391
391
max ($ this ->scale , $ b ->scale ) : max ($ this ->scale , $ b ->scale , $ scale );
@@ -445,6 +445,7 @@ public function log10($scale = null)
445
445
}
446
446
447
447
/**
448
+ * @param integer $scale
448
449
* @return boolean
449
450
*/
450
451
public function isZero ($ scale = null )
@@ -470,6 +471,14 @@ public function isNegative()
470
471
return ($ this ->value [0 ] === '- ' );
471
472
}
472
473
474
+ /**
475
+ * @return boolean
476
+ */
477
+ public function isInteger ()
478
+ {
479
+ return (preg_match ('/^[+\-]?[0-9]+(\.0+)?$/ ' , $ this ->value , $ captures ) === 1 );
480
+ }
481
+
473
482
/**
474
483
* @return boolean
475
484
*/
@@ -509,6 +518,7 @@ public function equals(Decimal $b, $scale = null)
509
518
* $this > $b : returns 1 , $this < $b : returns -1 , $this == $b : returns 0
510
519
*
511
520
* @param Decimal $b
521
+ * @param integer $scale
512
522
* @return integer
513
523
*/
514
524
public function comp (Decimal $ b , $ scale = null )
@@ -661,9 +671,9 @@ public function sin($scale = null) {
661
671
662
672
// Next use Maclaurin's theorem to approximate sin with high enough accuracy
663
673
// note that the accuracy is depended on the accuracy of the given PI constant
664
- $ faculty = Decimal:: fromString ( " 1 " ); // Calculates the faculty under the sign
665
- $ xPowerN = Decimal:: fromString ( " 1 " ); // Calculates x^n
666
- $ approx = Decimal:: fromString ( " 0 " ); // keeps track of our approximation for sin(x)
674
+ $ faculty = DecimalConstants:: One ( ); // Calculates the faculty under the sign
675
+ $ xPowerN = DecimalConstants:: One ( ); // Calculates x^n
676
+ $ approx = DecimalConstants:: Zero ( ); // keeps track of our approximation for sin(x)
667
677
668
678
$ change = InfiniteDecimal::getPositiveInfinite ();
669
679
@@ -706,9 +716,9 @@ public function cos($scale = null) {
706
716
707
717
// Next use Maclaurin's theorem to approximate sin with high enough accuracy
708
718
// note that the accuracy is depended on the accuracy of the given PI constant
709
- $ faculty = Decimal:: fromString ( " 1 " ); // Calculates the faculty under the sign
710
- $ xPowerN = Decimal:: fromString ( " 1 " ); // Calculates x^n
711
- $ approx = Decimal:: fromString ( " 1 " ); // keeps track of our approximation for sin(x)
719
+ $ faculty = DecimalConstants:: One ( ); // Calculates the faculty under the sign
720
+ $ xPowerN = DecimalConstants:: One ( ); // Calculates x^n
721
+ $ approx = DecimalConstants:: One ( ); // keeps track of our approximation for sin(x)
712
722
713
723
$ change = InfiniteDecimal::getPositiveInfinite ();
714
724
@@ -980,10 +990,8 @@ private static function normalizeSign($sign)
980
990
*/
981
991
private static function countSignificativeDigits (Decimal $ val , Decimal $ abs )
982
992
{
983
- $ one = Decimal::fromInteger (1 );
984
-
985
993
return strlen ($ val ->value ) - (
986
- ($ abs ->comp ($ one ) === -1 ) ? 2 : max ($ val ->scale , 1 )
994
+ ($ abs ->comp (DecimalConstants:: One () ) === -1 ) ? 2 : max ($ val ->scale , 1 )
987
995
) - ($ val ->isNegative () ? 1 : 0 );
988
996
}
989
997
}
0 commit comments