view core/src/luan/impl/ModExpr.java @ 614:9ecf5673fb6d

start version 0.15
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 14 Dec 2015 23:55:36 -0700
parents b48cfa14ba60
children 859c0dedc8b6
line wrap: on
line source

package luan.impl;

import luan.LuanException;
import luan.LuanElement;


final class ModExpr extends BinaryOpExpr {

	ModExpr(LuanElement se,Expr op1,Expr op2) {
		super(se,op1,op2);
	}

	@Override public Object eval(LuanStateImpl luan) throws LuanException {
		Object o1 = op1.eval(luan);
		Object o2 = op2.eval(luan);
		if( o1 instanceof Number && o2 instanceof Number ) {
			double d1 = ((Number)o1).doubleValue();
			double d2 = ((Number)o2).doubleValue();
			return d1 - Math.floor(d1/d2)*d2;
		}
		return arithmetic(luan,"__mod",o1,o2);
	}
}