chengkun
2025-06-05 4080b5997b38ca84b3b203c7101dcadb97b76925
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
 
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
 
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
 
class Helpers
{
    /**
     * Many functions accept null/false/true argument treated as 0/0/1.
     *
     * @return float|string quotient or DIV0 if denominator is too small
     */
    public static function verySmallDenominator(float $numerator, float $denominator): string|float
    {
        return (abs($denominator) < 1.0E-12) ? ExcelError::DIV0() : ($numerator / $denominator);
    }
 
    /**
     * Many functions accept null/false/true argument treated as 0/0/1.
     */
    public static function validateNumericNullBool(mixed $number): int|float
    {
        $number = Functions::flattenSingleValue($number);
        if ($number === null) {
            return 0;
        }
        if (is_bool($number)) {
            return (int) $number;
        }
        if (is_numeric($number)) {
            return 0 + $number;
        }
 
        throw new Exception(ExcelError::throwError($number));
    }
 
    /**
     * Validate numeric, but allow substitute for null.
     */
    public static function validateNumericNullSubstitution(mixed $number, null|float|int $substitute): float|int
    {
        $number = Functions::flattenSingleValue($number);
        if ($number === null && $substitute !== null) {
            return $substitute;
        }
        if (is_numeric($number)) {
            return 0 + $number;
        }
 
        throw new Exception(ExcelError::throwError($number));
    }
 
    /**
     * Confirm number >= 0.
     */
    public static function validateNotNegative(float|int $number, ?string $except = null): void
    {
        if ($number >= 0) {
            return;
        }
 
        throw new Exception($except ?? ExcelError::NAN());
    }
 
    /**
     * Confirm number > 0.
     */
    public static function validatePositive(float|int $number, ?string $except = null): void
    {
        if ($number > 0) {
            return;
        }
 
        throw new Exception($except ?? ExcelError::NAN());
    }
 
    /**
     * Confirm number != 0.
     */
    public static function validateNotZero(float|int $number): void
    {
        if ($number) {
            return;
        }
 
        throw new Exception(ExcelError::DIV0());
    }
 
    public static function returnSign(float $number): int
    {
        return $number ? (($number > 0) ? 1 : -1) : 0;
    }
 
    public static function getEven(float $number): float
    {
        $significance = 2 * self::returnSign($number);
 
        return $significance ? (ceil($number / $significance) * $significance) : 0;
    }
 
    /**
     * Return NAN or value depending on argument.
     */
    public static function numberOrNan(float $result): float|string
    {
        return is_nan($result) ? ExcelError::NAN() : $result;
    }
}