// Copyright (c) 1996-2002 Brian D. Carlstrom

package bdc.scheme.procedure;

import bdc.scheme.Scheme;
import bdc.scheme.SchemeException;
import bdc.scheme.Stack;
import bdc.scheme.expression.Procedure1;
import bdc.util.Constants;

/**
    (/ x) (/ x y)
*/
public class Divide extends Procedure1
{
    public Object apply1 (Stack stack) throws SchemeException
    {
        Object o1 = stack.array[stack.inUse-1];
        Number n  = Scheme.number(o1, this);
        if (n instanceof Double) {
            return new Double(1 / n.doubleValue());
        }
        return Constants.getInteger(1 / n.intValue());
    }

    public Object apply2 (Stack stack) throws SchemeException
    {
        Object o1 = stack.array[stack.inUse-1];
        Object o2 = stack.array[stack.inUse-2];
        Number n1  = Scheme.number(o1, this);
        Number n2  = Scheme.number(o2, this);

        if ((n1 instanceof Double) ||
            (n2 instanceof Double))
        {
            return new Double(n1.doubleValue() / n2.doubleValue());
        }
        return Constants.getInteger(n1.intValue() / n2.intValue());
    }
}
