Normalize

Removes leading zeros and truncates (if necessary) to maintain the appropriate precision

@param \phpseclib\Math\BigInteger
@return \phpseclib\Math\BigInteger
@see self::_trim()
@access private
762935
function _normalize($result)
    {
        $result->precision = $this->precision;
        $result->bitmask = $this->bitmask;

        switch (MATH_BIGINTEGER_MODE) {
            case self::MODE_GMP:
                if ($this->bitmask !== false) {
                    $result->value = gmp_and($result->value, $result->bitmask->value);
                }

                return $result;
            case self::MODE_BCMATH:
                if (!empty($result->bitmask->value)) {
                    $result->value = bcmod($result->value, $result->bitmask->value);
                }

                return $result;
        }

        $value = &$result->value;

        if (!count($value)) {
            return $result;
        }

        $value = $this->_trim($value);

        if (!empty($result->bitmask->value)) {
            $length = min(count($value), count($this->bitmask->value));
            $value = array_slice($value, 0, $length);

            for ($i = 0; $i < $length; ++$i) {
                $value[$i] = $value[$i] & $this->bitmask->value[$i];
            }
        }

        return $result;
    }
1318490
public function normalize()
    {
        $data = $this->get();
        if (is_array($data)) {
            $data = implode($this->separator, $data);
        }
        $data = trim($data, $this->separator);
        if (!$data) {
            $data = null;
        }

        if (get_magic_quotes_gpc()) {
            $this->set(stripslashes($data));
        } else {
            $this->set($data);
        }

        return parent::normalize();
    }
650688
function bitwise_leftShift($shift)
    {
        $temp = new Math_BigInteger();

        switch (MATH_BIGINTEGER_MODE) {
            case MATH_BIGINTEGER_MODE_GMP:
                static $two;

                if (!isset($two)) {
                    $two = gmp_init('2');
                }

                $temp->value = gmp_mul($this->value, gmp_pow($two, $shift));

                break;
            case MATH_BIGINTEGER_MODE_BCMATH:
                $temp->value = bcmul($this->value, bcpow('2', $shift, 0), 0);

                break;
            default: // could just replace _rshift with this, but then all _lshift() calls would need to be rewritten
                     // and I don't want to do that...
                $temp->value = $this->value;
                $temp->_lshift($shift);
        }

        return $this->_normalize($temp);
    }
70451
public double normalize() {
        double propSum = this.getSum();
        if (propSum == s.zero()) {
            this.fill(s.divide(s.one(), s.fromReal(this.size())));
        } else if (propSum == s.posInf()) {
            int count = count(s.posInf());
            if (count == 0) {
                throw new RuntimeException("Unable to normalize since sum is infinite but contains no infinities: " + this.toString());
            }
            double constant = s.divide(s.one(), s.fromReal(count));
            for (int d=0; d<this.size(); d++) {
                if (this.getValue(d) == s.posInf()) {
                    this.setValue(d, constant);
                } else {
                    this.setValue(d, s.zero());
                }
            }
        } else {
            this.divide(propSum);
            assert !this.containsNaN();
        }
        return propSum;
    }
39536
private function normalize($value)
    {
        // Normalize array values
        if (is_array($value)) {
            foreach ($value as &$subValue) {
                $subValue = $this->normalize($subValue);
            }

            return $value;
        }

        // Don't normalize non-string value
        if (!is_string($value)) {
            return $value;
        }

        // Normalize true boolean value
        if ($this->compareValues($value, ['true', 'on', 'yes'])) {
            return true;
        }

        // Normalize false boolean value
        if ($this->compareValues($value, ['false', 'off', 'no', 'none'])) {
            return false;
        }

        // Normalize null value
        if ($this->compareValues($value, ['null'])) {
            return;
        }

        // Normalize numeric value
        if (is_numeric($value)) {
            $numericValue = $value + 0;

            if ((is_int($numericValue) && (int) $value === $numericValue)
                || (is_float($numericValue) && (float) $value === $numericValue)
            ) {
                $value = $numericValue;
            }
        }

        return $value;
    }
259501
protected function normalizeNumericStringValue($value)
    {
        $separatorPosition = strrpos($value, '.');
        
        if ($separatorPosition !== false) {
            $integerPart = substr($value, 0, $separatorPosition);
            $fractionalPart = substr($value, $separatorPosition + 1);
        } else {
            $integerPart = $value;
            $fractionalPart = null;
        }
        
        // truncate insignificant zeros, keep minus
        $integerPart = preg_replace('/^\+?(-?)0*(\d+)$/', '$1$2', $integerPart);
        // for zeros only leave one zero, keep minus
        $integerPart = preg_replace('/^\+?(-?)0*$/', '${1}0', $integerPart);
        
        if ($fractionalPart !== null) {
            // truncate insignificant zeros
            $fractionalPart = rtrim($fractionalPart, '0');
        }
        
        $normalizedValue = $integerPart;
        if (!empty($fractionalPart)) {
            $normalizedValue .= '.' . $fractionalPart;
        } elseif ($normalizedValue === '-0') {
            $normalizedValue = '0';
        }
        
        return $normalizedValue;
    }
507352
public static BigDecimal trunc(BigDecimal number, int precision) {
        return number.setScale(precision, RoundingMode.DOWN);
    }
680312
function bitwise_rightShift($shift)
    {
        $temp = new Math_BigInteger();

        switch (MATH_BIGINTEGER_MODE) {
            case MATH_BIGINTEGER_MODE_GMP:
                static $two;

                if (!isset($two)) {
                    $two = gmp_init('2');
                }

                $temp->value = gmp_div_q($this->value, gmp_pow($two, $shift));

                break;
            case MATH_BIGINTEGER_MODE_BCMATH:
                $temp->value = bcdiv($this->value, bcpow('2', $shift, 0), 0);

                break;
            default: // could just replace _lshift with this, but then all _lshift() calls would need to be rewritten
                     // and I don't want to do that...
                $temp->value = $this->value;
                $temp->_rshift($shift);
        }

        return $this->_normalize($temp);
    }
536267
public function normalize($key)
    {
        if (strlen($key) > $this->length) {
            $checksum = dechex(crc32($key));
            return substr($key, 0, $this->length - strlen($checksum)) . $checksum;
        }

        return $key;
    }
64672
function normalize(arr) {
    const n = copy(arr);
    let len = 0;
    for (const v of n) len += v * v;
    if (len > 0) {
        const coeff = 1 / Math.sqrt(len);
        for (let k = 0; k < n.length; k++) {
            n[k] *= coeff;
        }
    }
    return n;
}
69676
def normalize!
      constant = 0
      hterms = {}
      @terms.each do |term|
        case term
        when Numeric
          constant += term
        when Ilp::Term
          v = term.var
          hterms[v] ||= Ilp::Term.new(v, 0)
          hterms[v].mult += term.mult
        end
      end
      reduced = hterms.map { |_, term| term unless term.mult.zero? }
      reduced.compact!
      @terms = [constant].concat reduced
      self
    end
1034442
private function normalize_int($size)
    {
        if (is_numeric($size)) {
            return (int)$size;
        }

        $letter = substr($size, -1);
        $size = (int)$size;

        switch ($letter) {
            case 'M': case 'm': return $size * 1048576;
            case 'K': case 'k': return $size * 1024;
            case 'G': case 'g': return $size * 1073741824;
        }
    }
833580
private function normalize()
    {
        $gcd = Math::gcd($this->p, $this->q);

        if ($gcd == 0) throw new DivisionByZeroException();

        $this->p = $this->p/$gcd;
        $this->q = $this->q/$gcd;

        if ($this->q < 0) {
            $this->p = -$this->p;
            $this->q = -$this->q;
        }
    }
715556
public function normalize($value)
    {
        if ($value instanceof Expression ||
            $value instanceof Expressionable) {
            return $value;
        }

        return parent::normalize($value);
    }
1150157
public function simplified() : BigRational
    {
        $gcd = $this->numerator->gcd($this->denominator);

        $numerator = $this->numerator->quotient($gcd);
        $denominator = $this->denominator->quotient($gcd);

        return new BigRational($numerator, $denominator, false);
    }
295002
private function normalizeCoefficients(DecimalNumber $a, DecimalNumber $b)
    {
        $exp1 = $a->getExponent();
        $exp2 = $b->getExponent();

        $coeff1 = $a->getCoefficient();
        $coeff2 = $b->getCoefficient();

        // add trailing zeroes if needed
        if ($exp1 > $exp2) {
            $coeff2 = str_pad($coeff2, strlen($coeff2) + $exp1 - $exp2, '0', STR_PAD_RIGHT);
        } elseif ($exp1 < $exp2) {
            $coeff1 = str_pad($coeff1, strlen($coeff1) + $exp2 - $exp1, '0', STR_PAD_RIGHT);
        }

        $len1 = strlen($coeff1);
        $len2 = strlen($coeff2);

        // add leading zeroes if needed
        if ($len1 > $len2) {
            $coeff2 = str_pad($coeff2, $len1, '0', STR_PAD_LEFT);
        } elseif ($len1 < $len2) {
            $coeff1 = str_pad($coeff1, $len2, '0', STR_PAD_LEFT);
        }

        return [$coeff1, $coeff2];
    }
586929
public function protectedMakeOdd()
    {
        switch (MATH_BIGINTEGER_MODE) {
            case MATH_BIGINTEGER_MODE_GMP:
                gmp_setbit($this->value, 0);
                break;
            case MATH_BIGINTEGER_MODE_BCMATH:
                if ($this->value[strlen($this->value) - 1] % 2 == 0) {
                    $this->value = bcadd($this->value, '1');
                }
                break;
            default:
                $this->value[0] |= 1;
        }
    }