Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
   4  
   5  use Complex\Complex as ComplexObject;
   6  use Complex\Exception as ComplexException;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   8  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   9  
  10  class Complex
  11  {
  12      /**
  13       * COMPLEX.
  14       *
  15       * Converts real and imaginary coefficients into a complex number of the form x +/- yi or x +/- yj.
  16       *
  17       * Excel Function:
  18       *        COMPLEX(realNumber,imaginary[,suffix])
  19       *
  20       * @param mixed $realNumber the real float coefficient of the complex number
  21       * @param mixed $imaginary the imaginary float coefficient of the complex number
  22       * @param mixed $suffix The character suffix for the imaginary component of the complex number.
  23       *                          If omitted, the suffix is assumed to be "i".
  24       *
  25       * @return string
  26       */
  27      public static function COMPLEX($realNumber = 0.0, $imaginary = 0.0, $suffix = 'i')
  28      {
  29          $realNumber = ($realNumber === null) ? 0.0 : Functions::flattenSingleValue($realNumber);
  30          $imaginary = ($imaginary === null) ? 0.0 : Functions::flattenSingleValue($imaginary);
  31          $suffix = ($suffix === null) ? 'i' : Functions::flattenSingleValue($suffix);
  32  
  33          try {
  34              $realNumber = EngineeringValidations::validateFloat($realNumber);
  35              $imaginary = EngineeringValidations::validateFloat($imaginary);
  36          } catch (Exception $e) {
  37              return $e->getMessage();
  38          }
  39  
  40          if (($suffix == 'i') || ($suffix == 'j') || ($suffix == '')) {
  41              $complex = new ComplexObject($realNumber, $imaginary, $suffix);
  42  
  43              return (string) $complex;
  44          }
  45  
  46          return Functions::VALUE();
  47      }
  48  
  49      /**
  50       * IMAGINARY.
  51       *
  52       * Returns the imaginary coefficient of a complex number in x + yi or x + yj text format.
  53       *
  54       * Excel Function:
  55       *        IMAGINARY(complexNumber)
  56       *
  57       * @param string $complexNumber the complex number for which you want the imaginary
  58       *                                         coefficient
  59       *
  60       * @return float|string
  61       */
  62      public static function IMAGINARY($complexNumber)
  63      {
  64          $complexNumber = Functions::flattenSingleValue($complexNumber);
  65  
  66          try {
  67              $complex = new ComplexObject($complexNumber);
  68          } catch (ComplexException $e) {
  69              return Functions::NAN();
  70          }
  71  
  72          return $complex->getImaginary();
  73      }
  74  
  75      /**
  76       * IMREAL.
  77       *
  78       * Returns the real coefficient of a complex number in x + yi or x + yj text format.
  79       *
  80       * Excel Function:
  81       *        IMREAL(complexNumber)
  82       *
  83       * @param string $complexNumber the complex number for which you want the real coefficient
  84       *
  85       * @return float|string
  86       */
  87      public static function IMREAL($complexNumber)
  88      {
  89          $complexNumber = Functions::flattenSingleValue($complexNumber);
  90  
  91          try {
  92              $complex = new ComplexObject($complexNumber);
  93          } catch (ComplexException $e) {
  94              return Functions::NAN();
  95          }
  96  
  97          return $complex->getReal();
  98      }
  99  }