Add ipow, to compute power for integers.

This commit is contained in:
James Roseborough
2007-09-22 05:25:02 +00:00
parent 90cc227e34
commit e154a158a9
2 changed files with 31 additions and 4 deletions

View File

@@ -52,12 +52,31 @@ public class LDouble extends LNumber {
case Lua.OP_MUL: return new LDouble( lhs * rhs );
case Lua.OP_DIV: return new LDouble( lhs / rhs );
case Lua.OP_MOD: return new LDouble( lhs - Math.floor(lhs/rhs) * rhs );
// case Lua.OP_POW: return new LDouble( Math.pow(lhs, rhs) );
// case Lua.OP_POW: return new LDouble( dpow(lhs, rhs) );
}
return luaUnsupportedOperation();
}
/*
public static double dpow(double a, double b) {
if ( b < 0 )
return 1 / dpow( a, -b );
int p = 1;
int whole = (int) b;
for ( double v=a; whole > 0; whole>>=1, v=v*v )
if ( (whole & 1) != 0 )
p *= v;
int frac = (int) (0x10000 * b);
for ( ; (frac&0xffff)!=0; frac<<=1 ) {
a = Math.sqrt(a);
if ( (frac & 0x8000) != 0 )
p *= a;
}
return p;
}
*/
public int luaAsInt() {
return (int) m_value;
}

View File

@@ -43,11 +43,19 @@ public class LInteger extends LNumber {
case Lua.OP_MUL: return new LInteger( m_value * rhs );
case Lua.OP_DIV: return new LInteger( m_value / rhs );
case Lua.OP_MOD: return new LInteger( m_value - ((int) Math.floor(m_value/(double)rhs)) * rhs );
// case Lua.OP_POW: return new LInteger( (int) Math.pow(m_value, rhs) );
case Lua.OP_POW: return new LInteger( ipow(m_value, rhs) );
}
return luaUnsupportedOperation();
}
private static int ipow(int v, int rhs) {
int p = 1;
for ( ; rhs > 0; rhs>>=1, v=v*v )
if ( (rhs & 1) != 0 )
p *= v;
return p;
}
// binary operations on mixed integer, double
public LValue luaBinOpDouble(int opcode, double rhs) {
return LDouble.luaBinOpDoubleDouble(opcode, (double) m_value, rhs );