[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/spomky-labs/cbor-php/src/OtherObject/ -> DoublePrecisionFloatObject.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  /*
   6   * The MIT License (MIT)
   7   *
   8   * Copyright (c) 2018-2020 Spomky-Labs
   9   *
  10   * This software may be modified and distributed under the terms
  11   * of the MIT license.  See the LICENSE file for details.
  12   */
  13  
  14  namespace CBOR\OtherObject;
  15  
  16  use Assert\Assertion;
  17  use Brick\Math\BigInteger;
  18  use CBOR\OtherObject as Base;
  19  use CBOR\Utils;
  20  use InvalidArgumentException;
  21  
  22  final class DoublePrecisionFloatObject extends Base
  23  {
  24      public static function supportedAdditionalInformation(): array
  25      {
  26          return [27];
  27      }
  28  
  29      public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
  30      {
  31          return new self($additionalInformation, $data);
  32      }
  33  
  34      /**
  35       * @return DoublePrecisionFloatObject
  36       */
  37      public static function create(string $value): self
  38      {
  39          if (8 !== mb_strlen($value, '8bit')) {
  40              throw new InvalidArgumentException('The value is not a valid double precision floating point');
  41          }
  42  
  43          return new self(27, $value);
  44      }
  45  
  46      public function getNormalizedData(bool $ignoreTags = false)
  47      {
  48          $exp = $this->getExponent();
  49          $mant = $this->getMantissa();
  50          $sign = $this->getSign();
  51  
  52          if (0 === $exp) {
  53              $val = $mant * 2 ** (-(1022 + 52));
  54          } elseif (0b11111111111 !== $exp) {
  55              $val = ($mant + (1 << 52)) * 2 ** ($exp - (1023 + 52));
  56          } else {
  57              $val = 0 === $mant ? INF : NAN;
  58          }
  59  
  60          return $sign * $val;
  61      }
  62  
  63      public function getExponent(): int
  64      {
  65          $data = $this->data;
  66          Assertion::string($data, 'Invalid data');
  67  
  68          return Utils::binToBigInteger($data)->shiftedRight(52)->and(Utils::hexToBigInteger('7ff'))->toInt();
  69      }
  70  
  71      public function getMantissa(): int
  72      {
  73          $data = $this->data;
  74          Assertion::string($data, 'Invalid data');
  75  
  76          return Utils::binToBigInteger($data)->and(Utils::hexToBigInteger('fffffffffffff'))->toInt();
  77      }
  78  
  79      public function getSign(): int
  80      {
  81          $data = $this->data;
  82          Assertion::string($data, 'Invalid data');
  83          $sign = Utils::binToBigInteger($data)->shiftedRight(63);
  84  
  85          return $sign->isEqualTo(BigInteger::one()) ? -1 : 1;
  86      }
  87  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer