Package net.fortytwo.ripple.libs.math

Source Code of net.fortytwo.ripple.libs.math.Mod

package net.fortytwo.ripple.libs.math;

import net.fortytwo.flow.Sink;
import net.fortytwo.ripple.RippleException;
import net.fortytwo.ripple.model.ModelConnection;
import net.fortytwo.ripple.model.NumericValue;
import net.fortytwo.ripple.model.PrimitiveStackMapping;
import net.fortytwo.ripple.model.RippleList;

/**
* A primitive which consumes two numbers x and y and produces the number x
* modulo y.
*
* @author Joshua Shinavier (http://fortytwo.net)
*/
public class Mod extends PrimitiveStackMapping {
    private static final String[] IDENTIFIERS = {
            MathLibrary.NS_2013_03 + "mod",
            MathLibrary.NS_2008_08 + "mod",
            MathLibrary.NS_2007_08 + "mod",
            MathLibrary.NS_2007_05 + "mod"};

    public String[] getIdentifiers() {
        return IDENTIFIERS;
    }

    public Mod()
            throws RippleException {
        super();
    }

    public Parameter[] getParameters() {
        return new Parameter[]{
                new Parameter("x", null, true),
                new Parameter("y", null, true)};
    }

    public String getComment() {
        return "x y  =>  x % y";
    }

    public void apply(final RippleList arg,
                      final Sink<RippleList> solutions,
                      final ModelConnection mc) throws RippleException {
        RippleList stack = arg;

        NumericValue a, b, result;

        b = mc.toNumericValue(stack.getFirst());
        stack = stack.getRest();
        a = mc.toNumericValue(stack.getFirst());
        stack = stack.getRest();

        // Note: mod by zero simply does not yield a result.
        if (!b.isZero()) {
            result = a.mod(b);

            solutions.put(
                    stack.push(result));
        }
    }
}
TOP

Related Classes of net.fortytwo.ripple.libs.math.Mod

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.