Adds a constant to a fraction by: - collapsing the fraction to decimal if the constant is not an integer e.g. 5.3 + 1/2 -> 5.3 + 0.2 - turning the constant into a fraction with the same denominator if it is an integer, e.g. 5 + 1/2 -> 10/2 + 1/2
19758
function addConstantAndFraction(node) {
  if (!Node.Type.isOperator(node) || node.op !== '+' || node.args.length !== 2) {
    return Node.Status.noChange(node);
  }

  const firstArg = node.args[0];
  const secondArg = node.args[1];
  let constNode, fractionNode;
  if (Node.Type.isConstant(firstArg)) {
    if (Node.Type.isIntegerFraction(secondArg)) {
      constNode = firstArg;
      fractionNode = secondArg;
    }
    else {
      return Node.Status.noChange(node);
    }
  }
  else if (Node.Type.isConstant(secondArg)) {
    if (Node.Type.isIntegerFraction(firstArg)) {
      constNode = secondArg;
      fractionNode = firstArg;
    }
    else {
      return Node.Status.noChange(node);
    }
  }
  else {
    return Node.Status.noChange(node);
  }

  let newNode = clone(node);
  let substeps = [];
  // newConstNode and newFractionNode will end up both constants, or both
  // fractions. I'm naming them based on their original form so we can keep
  // track of which is which.
  let newConstNode, newFractionNode;
  let changeType;
  if (Number.isInteger(parseFloat(constNode.value))) {
    const denominatorNode = fractionNode.args[1];
    const denominatorValue = parseInt(denominatorNode);
    const constNodeValue = parseInt(constNode.value);
    const newNumeratorNode = Node.Creator.constant(
      constNodeValue * denominatorValue);
    newConstNode = Node.Creator.operator(
      '/', [newNumeratorNode, denominatorNode]);
    newFractionNode = fractionNode;
    changeType = ChangeTypes.CONVERT_INTEGER_TO_FRACTION;
  }
  else {
    // round to 4 decimal places
    let dividedValue = fractionNode.eval();
    if (dividedValue < 1) {
      dividedValue = parseFloat(dividedValue.toPrecision(4));
    }
    else {
      dividedValue = parseFloat(dividedValue.toFixed(4));
    }
    newFractionNode = Node.Creator.constant(dividedValue);
    newConstNode = constNode;
    changeType = ChangeTypes.DIVIDE_FRACTION_FOR_ADDITION;
  }

  if (Node.Type.isConstant(firstArg)) {
    newNode.args[0] = newConstNode;
    newNode.args[1] = newFractionNode;
  }
  else {
    newNode.args[0] = newFractionNode;
    newNode.args[1] = newConstNode;
  }

  substeps.push(Node.Status.nodeChanged(changeType, node, newNode));
  newNode = Node.Status.resetChangeGroups(newNode);

  // If we changed an integer to a fraction, we need to add the steps for
  // adding the fractions.
  if (changeType === ChangeTypes.CONVERT_INTEGER_TO_FRACTION) {
    const addFractionStatus = addConstantFractions(newNode);
    substeps = substeps.concat(addFractionStatus.substeps);
  }
  // Otherwise, add the two constants
  else {
    const evalNode = Node.Creator.constant(newNode.eval());
    substeps.push(Node.Status.nodeChanged(
      ChangeTypes.SIMPLIFY_ARITHMETIC, newNode, evalNode));
  }

  const lastStep = substeps[substeps.length - 1];
  newNode = Node.Status.resetChangeGroups(lastStep.newNode);

  return Node.Status.nodeChanged(
    ChangeTypes.SIMPLIFY_ARITHMETIC, node, newNode, true, substeps);
}
1205467
def _round_frac(x, precision):
    
    if not np.isfinite(x) or x == 0:
        return x
    else:
        frac, whole = np.modf(x)
        if whole == 0:
            digits = -int(np.floor(np.log10(abs(frac)))) - 1 + precision
        else:
            digits = precision
        return np.around(x, digits)
179393
void accept(final double value) {
		++_n;

		final double n = _n;
		final double d = value - _m1.value();
		final double dN = d/n;
		final double dN2 = dN*dN;
		final double t1 = d*dN*(n - 1.0);

		_m1.add(dN);
		_m4.add(t1*dN2*(n*n - 3.0*n + 3.0))
			.add(6.0*dN2*_m2.value() - 4.0*dN*_m3.value());
		_m3.add(t1*dN*(n - 2.0) - 3.0*dN*_m2.value());
		_m2.add(t1);
	}
157098
function(m) {

      if (m < 0) {
        return new Fraction(Math.pow(this['s'] * this["d"], -m), Math.pow(this["n"], -m));
      } else {
        return new Fraction(Math.pow(this['s'] * this["n"], m), Math.pow(this["d"], m));
      }
    }
1037884
def float_to_decimal(f):
    
    n, d = f.as_integer_ratio()
    numerator, denominator = Decimal(n), Decimal(d)
    return DECIMAL_CONTEXT.divide(numerator, denominator)
392798
function(dec) {
        if(!dec){
            dec = 2;
        }
        return '{x: ' + this.x.toFixed(dec) + ', y: ' + this.y.toFixed(dec)  + ', z: ' + this.z.toFixed(dec)  + '}';
    }
334982
function resolveFraction(frac) {
  frac = `${frac}`;
  if (Patterns.Percentage.test(frac)) {
    return parseFloat(frac) / 100;
  } else if (Patterns.Number.test(frac)) {
    return parseFloat(frac);
  } else
    throw new Error('Unknown fraction format: ', inp);
}
991416
function roundNumber(parsedNumber, fractionSize, minFrac, maxFrac) {
    var digits = parsedNumber.d;
    var fractionLen = digits.length - parsedNumber.i;

    // determine fractionSize if it is not specified; `+fractionSize` converts it to a number
    fractionSize = (isUndefined(fractionSize)) ? Math.min(Math.max(minFrac, fractionLen), maxFrac) : +fractionSize;

    // The index of the digit to where rounding is to occur
    var roundAt = fractionSize + parsedNumber.i;
    var digit = digits[roundAt];

    if (roundAt > 0) {
      // Drop fractional digits beyond `roundAt`
      digits.splice(Math.max(parsedNumber.i, roundAt));

      // Set non-fractional digits beyond `roundAt` to 0
      for (var j = roundAt; j < digits.length; j++) {
        digits[j] = 0;
      }
    } else {
      // We rounded to zero so reset the parsedNumber
      fractionLen = Math.max(0, fractionLen);
      parsedNumber.i = 1;
      digits.length = Math.max(1, roundAt = fractionSize + 1);
      digits[0] = 0;
      for (var i = 1; i < roundAt; i++) digits[i] = 0;
    }

    if (digit >= 5) {
      if (roundAt - 1 < 0) {
        for (var k = 0; k > roundAt; k--) {
          digits.unshift(0);
          parsedNumber.i++;
        }
        digits.unshift(1);
        parsedNumber.i++;
      } else {
        digits[roundAt - 1]++;
      }
    }

    // Pad out with zeros to get the required fraction length
    for (; fractionLen < Math.max(0, fractionSize); fractionLen++) digits.push(0);


    // Do any carrying, e.g. a digit was rounded up to 10
    var carry = digits.reduceRight(function(carry, d, i, digits) {
      d = d + carry;
      digits[i] = d % 10;
      return Math.floor(d / 10);
    }, 0);
    if (carry) {
      digits.unshift(carry);
      parsedNumber.i++;
    }
}
318560
function simplifySigns(fraction) {
  if (!Node.Type.isOperator(fraction) || fraction.op !== '/') {
    return Node.Status.noChange(fraction);
  }
  const oldFraction = clone(fraction);
  let numerator = fraction.args[0];
  let denominator = fraction.args[1];
  // The denominator should never be negative.
  if (Negative.isNegative(denominator)) {
    denominator = Negative.negate(denominator);
    const changeType = Negative.isNegative(numerator) ?
      ChangeTypes.CANCEL_MINUSES :
      ChangeTypes.SIMPLIFY_SIGNS;
    numerator = Negative.negate(numerator);
    const newFraction = Node.Creator.operator('/', [numerator, denominator]);
    return Node.Status.nodeChanged(changeType, oldFraction, newFraction);
  }
  else {
    return Node.Status.noChange(fraction);
  }
}
468352
def decimal_to_alpha(dec):
    
    dec /= 100.0
    alpha =  hex(int(dec*65535))[2:]
    while len(alpha) < 4:
        alpha = '0' + alpha
    return alpha
394781
function(val, fractionDigits)
    {
      return val == null || val === "" ? 0 : formatNumber(val,
      {
        minimumFractionDigits: fractionDigits == null ? 0 : fractionDigits,
        maximumFractionDigits: fractionDigits == null ? maximumFractionDigits : fractionDigits
      })
    }
34871
function(a, b) {

      if (isNaN(this['n']) || isNaN(this['d'])) {
        return new Fraction(NaN);
      }

      if (a === undefined) {
        return new Fraction(this["s"] * this["n"] % this["d"], 1);
      }

      parse(a, b);
      if (0 === P["n"] && 0 === this["d"]) {
        Fraction(0, 0); // Throw DivisionByZero
      }

      /*
       * First silly attempt, kinda slow
       *
       return that["sub"]({
       "n": num["n"] * Math.floor((this.n / this.d) / (num.n / num.d)),
       "d": num["d"],
       "s": this["s"]
       });*/

      /*
       * New attempt: a1 / b1 = a2 / b2 * q + r
       * => b2 * a1 = a2 * b1 * q + b1 * b2 * r
       * => (b2 * a1 % a2 * b1) / (b1 * b2)
       */
      return new Fraction(
              this["s"] * (P["d"] * this["n"]) % (P["n"] * this["d"]),
              P["d"] * this["d"]
              );
    }
316594
function toPercent(parsedNumber) {
    // if the number is 0, don't do anything
    if (parsedNumber.digits[0] === 0) {
        return parsedNumber;
    }
    // Getting the current number of decimals
    var fractionLen = parsedNumber.digits.length - parsedNumber.integerLen;
    if (parsedNumber.exponent) {
        parsedNumber.exponent += 2;
    }
    else {
        if (fractionLen === 0) {
            parsedNumber.digits.push(0, 0);
        }
        else if (fractionLen === 1) {
            parsedNumber.digits.push(0);
        }
        parsedNumber.integerLen += 2;
    }
    return parsedNumber;
}
531051
def decimal(self, column, total=8, places=2):
        
        return self._add_column("decimal", column, total=total, places=places)
1213002
private double round(double number) {
        boolean isNegative = isNegative(number);
        if (isNegative)
            number = -number;

        // Apply rounding after multiplier
        if (roundingDouble > 0.0) {
            // number = roundingDouble
            //    * round(number / roundingDouble, roundingMode, isNegative);
            return round(
                number, roundingDouble, roundingDoubleReciprocal, roundingMode,
                isNegative);
        }
        return number;
    }
927272
public FloatConstant addFloat(float value)
  {
    FloatConstant entry = getFloatByValue(value);

    if (entry != null)
      return entry;

    entry = new FloatConstant(this, _entries.size(), value);

    addConstant(entry);

    return entry;
  }
934884
public static FractionNumber createExactDenominator(final double value, int exactDenom, boolean wholeType) {
        final FractionNumber fractionNumber = new FractionNumber(value);
        fractionNumber.wholeType = wholeType;
        
        final SimpleFraction fraction = SimpleFraction.createFractionExactDenominator(Math.abs(value), exactDenom);
        setupFractionPart(fraction, fractionNumber);
        
        return fractionNumber;
    }