ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
subtract.php
Go to the documentation of this file.
1 <?php
2 
10 namespace Complex;
11 
18 if (!function_exists(__NAMESPACE__ . '\\subtract')) {
19  function subtract(...$complexValues): Complex
20  {
21  if (count($complexValues) < 2) {
22  throw new \Exception('This function requires at least 2 arguments');
23  }
24 
25  $base = array_shift($complexValues);
26  $result = clone Complex::validateComplexArgument($base);
27 
28  foreach ($complexValues as $complex) {
29  $complex = Complex::validateComplexArgument($complex);
30 
31  if ($result->isComplex() && $complex->isComplex() &&
32  $result->getSuffix() !== $complex->getSuffix()) {
33  throw new Exception('Suffix Mismatch');
34  }
35 
36  $real = $result->getReal() - $complex->getReal();
37  $imaginary = $result->getImaginary() - $complex->getImaginary();
38 
39  $result = new Complex(
40  $real,
41  $imaginary,
42  ($imaginary == 0.0) ? null : max($result->getSuffix(), $complex->getSuffix())
43  );
44  }
45 
46  return $result;
47  }
48 }
$result
$base
Definition: index.php:4