Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
4
 
5
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
6
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
7
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
8
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
9
 
10
class Operations
11
{
12
    use ArrayEnabled;
13
 
14
    /**
15
     * MOD.
16
     *
17
     * @param mixed $dividend Dividend
18
     *                      Or can be an array of values
19
     * @param mixed $divisor Divisor
20
     *                      Or can be an array of values
21
     *
22
     * @return array|float|string Remainder, or a string containing an error
23
     *         If an array of numbers is passed as an argument, then the returned result will also be an array
24
     *            with the same dimensions
25
     */
26
    public static function mod(mixed $dividend, mixed $divisor): array|string|float
27
    {
28
        if (is_array($dividend) || is_array($divisor)) {
29
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $dividend, $divisor);
30
        }
31
 
32
        try {
33
            $dividend = Helpers::validateNumericNullBool($dividend);
34
            $divisor = Helpers::validateNumericNullBool($divisor);
35
            Helpers::validateNotZero($divisor);
36
        } catch (Exception $e) {
37
            return $e->getMessage();
38
        }
39
 
40
        if (($dividend < 0.0) && ($divisor > 0.0)) {
41
            return $divisor - fmod(abs($dividend), $divisor);
42
        }
43
        if (($dividend > 0.0) && ($divisor < 0.0)) {
44
            return $divisor + fmod($dividend, abs($divisor));
45
        }
46
 
47
        return fmod($dividend, $divisor);
48
    }
49
 
50
    /**
51
     * POWER.
52
     *
53
     * Computes x raised to the power y.
54
     *
55
     * @param null|array|bool|float|int|string $x Or can be an array of values
56
     * @param null|array|bool|float|int|string $y Or can be an array of values
57
     *
58
     * @return array|float|int|string The result, or a string containing an error
59
     *         If an array of numbers is passed as an argument, then the returned result will also be an array
60
     *            with the same dimensions
61
     */
62
    public static function power(null|array|bool|float|int|string $x, null|array|bool|float|int|string $y): array|float|int|string
63
    {
64
        if (is_array($x) || is_array($y)) {
65
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $x, $y);
66
        }
67
 
68
        try {
69
            $x = Helpers::validateNumericNullBool($x);
70
            $y = Helpers::validateNumericNullBool($y);
71
        } catch (Exception $e) {
72
            return $e->getMessage();
73
        }
74
 
75
        // Validate parameters
76
        if (!$x && !$y) {
77
            return ExcelError::NAN();
78
        }
79
        if (!$x && $y < 0.0) {
80
            return ExcelError::DIV0();
81
        }
82
 
83
        // Return
84
        $result = $x ** $y;
85
 
86
        return Helpers::numberOrNan($result);
87
    }
88
 
89
    /**
90
     * PRODUCT.
91
     *
92
     * PRODUCT returns the product of all the values and cells referenced in the argument list.
93
     *
94
     * Excel Function:
95
     *        PRODUCT(value1[,value2[, ...]])
96
     *
97
     * @param mixed ...$args Data values
98
     */
99
    public static function product(mixed ...$args): string|float
100
    {
101
        $args = array_filter(
102
            Functions::flattenArray($args),
103
            fn ($value): bool => $value !== null
104
        );
105
 
106
        // Return value
107
        $returnValue = (count($args) === 0) ? 0.0 : 1.0;
108
 
109
        // Loop through arguments
110
        foreach ($args as $arg) {
111
            // Is it a numeric value?
112
            if (is_numeric($arg)) {
113
                $returnValue *= $arg;
114
            } else {
115
                return ExcelError::throwError($arg);
116
            }
117
        }
118
 
119
        return (float) $returnValue;
120
    }
121
 
122
    /**
123
     * QUOTIENT.
124
     *
125
     * QUOTIENT function returns the integer portion of a division. Numerator is the divided number
126
     *        and denominator is the divisor.
127
     *
128
     * Excel Function:
129
     *        QUOTIENT(value1,value2)
130
     *
131
     * @param mixed $numerator Expect float|int
132
     *                      Or can be an array of values
133
     * @param mixed $denominator Expect float|int
134
     *                      Or can be an array of values
135
     *
136
     * @return array|int|string If an array of numbers is passed as an argument, then the returned result will also be an array
137
     *            with the same dimensions
138
     */
139
    public static function quotient(mixed $numerator, mixed $denominator): array|string|int
140
    {
141
        if (is_array($numerator) || is_array($denominator)) {
142
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $numerator, $denominator);
143
        }
144
 
145
        try {
146
            $numerator = Helpers::validateNumericNullSubstitution($numerator, 0);
147
            $denominator = Helpers::validateNumericNullSubstitution($denominator, 0);
148
            Helpers::validateNotZero($denominator);
149
        } catch (Exception $e) {
150
            return $e->getMessage();
151
        }
152
 
153
        return (int) ($numerator / $denominator);
154
    }
155
}