@@ -833,11 +833,16 @@ public static function rshuffle(array $ar): array {
833
833
* Note: technically, the function accepts a float, because in some
834
834
* PHP versions, if one passes a float to a function that expectes an int,
835
835
* the float will be converted. We'd rather detect that and print an error.
836
+ * Although the returned value is an integer, we use the float type, because
837
+ * PHP_FLOAT_MAX is much larger than PHP_INT_MAX.
838
+ * Beware: Due to the limited precision of floats (~ 16 digits), factorials
839
+ * larger than 27! are not guaranteed to be accurate, or rather, they are
840
+ * guaranteed not to be accurate...
836
841
*
837
842
* @param float $n the number
838
- * @return int
843
+ * @return float
839
844
*/
840
- public static function fact (float $ n ): int {
845
+ public static function fact (float $ n ): float {
841
846
$ n = self ::assure_numeric (
842
847
$ n ,
843
848
get_string ('error_func_nnegint ' , 'qtype_formulas ' , 'fact() ' ),
@@ -848,7 +853,7 @@ public static function fact(float $n): int {
848
853
}
849
854
$ result = 1 ;
850
855
for ($ i = 1 ; $ i <= $ n ; $ i ++) {
851
- if ($ result > PHP_INT_MAX / $ i ) {
856
+ if ($ result > PHP_FLOAT_MAX / $ i ) {
852
857
self ::die ('error_fact_toolarge ' , $ n );
853
858
}
854
859
$ result *= $ i ;
0 commit comments