Example #1
0
    public override int lambda(Stack st)
    {
        int  narg    = getNarg(st);
        List cond    = getList(st);
        List step_in = getList(st);
        List thru_in = getList(st);
        List body    = getList(st);

        pc.process_list(cond, true);
        if (pc.stack.Count == 0 || !(pc.stack.Peek() is Zahl) || ((Algebraic)pc.stack.Peek()).Name == null)
        {
            throw new ParseException("Non-constant initializer in for loop.");
        }
        Zahl   x     = (Zahl)pc.stack.Pop();
        string xname = x.Name;

        pc.process_list(step_in, true);
        if (pc.stack.Count == 0 || !(pc.stack.Peek() is Zahl))
        {
            throw new ParseException("Step size must be constant.");
        }
        Zahl step = (Zahl)pc.stack.Pop();

        pc.process_list(thru_in, true);
        if (pc.stack.Count == 0 || !(pc.stack.Peek() is Zahl))
        {
            throw new ParseException("Wrong format in for-loop.");
        }
        Zahl thru = (Zahl)pc.stack.Pop();
        bool pos  = !step.smaller(Zahl.ZERO);

        while (true)
        {
            if ((pos ? thru.smaller(x) : x.smaller(thru)))
            {
                break;
            }
            pc.env.putValue(xname, x);
            int ret = pc.process_list(body, true);
            switch (ret)
            {
            case Processor.BREAK:
                return(0);

            case Processor.RETURN:
            case Processor.EXIT:
            case Processor.ERROR:
                return(ret);

            case Processor.CONTINUE: break;
            }
            x = (Zahl)x.add(step);
        }
        return(0);
    }
Example #2
0
    internal virtual Algebraic fzexakt(Zahl x)
    {
        if (x.smaller(Zahl.ZERO))
        {
            Algebraic r = fzexakt((Zahl)x.mult(Zahl.MINUS));
            if (r != null)
            {
                return(r.cc());
            }
            return(r);
        }

        if (x.integerq())
        {
            if (x.intval() % 2 == 0)
            {
                return(Zahl.ONE);
            }
            else
            {
                return(Zahl.MINUS);
            }
        }

        Algebraic qs = x.add(new Unexakt(.5));

        if (((Zahl)qs).integerq())
        {
            if (((Zahl)qs).intval() % 2 == 0)
            {
                return(Zahl.IMINUS);
            }
            else
            {
                return(Zahl.IONE);
            }
        }

        qs = x.mult(new Unexakt(4));

        if (((Zahl)qs).integerq())
        {
            Algebraic sq2 = FunctionVariable.create("sqrt", new Unexakt(0.5));
            switch (((Zahl)qs).intval() % 8)
            {
            case 1:
                return(Zahl.ONE.add(Zahl.IONE).div(Zahl.SQRT2));

            case 3:
                return(Zahl.MINUS.add(Zahl.IONE).div(Zahl.SQRT2));

            case 5:
                return(Zahl.MINUS.add(Zahl.IMINUS).div(Zahl.SQRT2));

            case 7:
                return(Zahl.ONE.add(Zahl.IMINUS).div(Zahl.SQRT2));
            }
        }
        qs = x.mult(new Unexakt(6));
        if (((Zahl)qs).integerq())
        {
            switch (((Zahl)qs).intval() % 12)
            {
            case 1:
                return(Zahl.SQRT3.add(Zahl.IONE).div(Zahl.TWO));

            case 2:
                return(Zahl.ONE.add(Zahl.SQRT3.mult(Zahl.IONE)).div(Zahl.TWO));

            case 4:
                return(Zahl.SQRT3.mult(Zahl.IONE).add(Zahl.MINUS).div(Zahl.TWO));

            case 5:
                return(Zahl.IONE.sub(Zahl.SQRT3).div(Zahl.TWO));

            case 7:
                return(Zahl.IMINUS.sub(Zahl.SQRT3).div(Zahl.TWO));

            case 8:
                return(Zahl.SQRT3.mult(Zahl.IMINUS).sub(Zahl.ONE).div(Zahl.TWO));

            case 10:
                return(Zahl.SQRT3.mult(Zahl.IMINUS).add(Zahl.ONE).div(Zahl.TWO));

            case 11:
                return(Zahl.IMINUS.add(Zahl.SQRT3).div(Zahl.TWO));
            }
        }
        return(null);
    }