2007-06-08 05:11:37 +00:00
|
|
|
package lua.value;
|
|
|
|
|
|
|
|
|
|
import lua.Lua;
|
|
|
|
|
|
|
|
|
|
public class LDouble extends LNumber {
|
|
|
|
|
|
|
|
|
|
private final double m_value;
|
|
|
|
|
|
|
|
|
|
public LDouble(double value) {
|
|
|
|
|
this.m_value = value;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-10 22:53:09 +00:00
|
|
|
public int hashCode() {
|
|
|
|
|
return (int) m_value;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-10 06:27:54 +00:00
|
|
|
public LString luaAsString() {
|
2007-09-19 04:44:31 +00:00
|
|
|
long l = (long) m_value;
|
|
|
|
|
if ( m_value == (double) l ) {
|
|
|
|
|
// TODO: is this a good idea?
|
|
|
|
|
return new LString( Long.toString( l ) );
|
|
|
|
|
} else {
|
|
|
|
|
return LString.valueOf( m_value );
|
|
|
|
|
}
|
2007-06-08 05:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
2007-09-03 00:38:38 +00:00
|
|
|
public boolean isInteger() {
|
|
|
|
|
// Cast to int and then back to double and see if the value
|
|
|
|
|
// survives the round trip.
|
|
|
|
|
return ( (double) ( (int) m_value ) ) == m_value;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-08 05:11:37 +00:00
|
|
|
// binary operations on integers, first dispatch
|
|
|
|
|
public LValue luaBinOpUnknown(int opcode, LValue lhs) {
|
|
|
|
|
return lhs.luaBinOpDouble( opcode, this.m_value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// binary operations on mixtures of doubles and integers
|
|
|
|
|
public LValue luaBinOpInteger(int opcode, int rhs) {
|
|
|
|
|
return luaBinOpDoubleDouble( opcode, m_value, (double) rhs );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// binary operations on doubles
|
|
|
|
|
public LValue luaBinOpDouble(int opcode, double rhs) {
|
|
|
|
|
return luaBinOpDoubleDouble( opcode, m_value, rhs );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static LValue luaBinOpDoubleDouble( int opcode, double lhs, double rhs ) {
|
|
|
|
|
switch ( opcode ) {
|
|
|
|
|
case Lua.OP_ADD: return new LDouble( lhs + rhs );
|
|
|
|
|
case Lua.OP_SUB: return new LDouble( lhs - rhs );
|
|
|
|
|
case Lua.OP_MUL: return new LDouble( lhs * rhs );
|
|
|
|
|
case Lua.OP_DIV: return new LDouble( lhs / rhs );
|
2007-08-01 04:57:12 +00:00
|
|
|
case Lua.OP_MOD: return new LDouble( lhs - Math.floor(lhs/rhs) * rhs );
|
2007-09-22 05:25:02 +00:00
|
|
|
// case Lua.OP_POW: return new LDouble( dpow(lhs, rhs) );
|
2007-06-08 05:11:37 +00:00
|
|
|
}
|
|
|
|
|
return luaUnsupportedOperation();
|
|
|
|
|
}
|
2007-09-22 05:25:02 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2007-06-08 05:11:37 +00:00
|
|
|
public int luaAsInt() {
|
|
|
|
|
return (int) m_value;
|
|
|
|
|
}
|
2007-06-10 19:49:47 +00:00
|
|
|
|
2007-06-24 15:04:19 +00:00
|
|
|
public double luaAsDouble() {
|
|
|
|
|
return m_value;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-10 19:49:47 +00:00
|
|
|
// binary compares on integers, first dispatch
|
|
|
|
|
public boolean luaBinCmpUnknown(int opcode, LValue lhs) {
|
|
|
|
|
return lhs.luaBinCmpDouble( opcode, this.m_value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// binary compares on mixtures of doubles and integers
|
|
|
|
|
public boolean luaBinCmpInteger(int opcode, int rhs) {
|
|
|
|
|
return luaBinCmpDoubleDouble( opcode, m_value, (double) rhs );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// binary compares on doubles
|
|
|
|
|
public boolean luaBinCmpDouble(int opcode, double rhs) {
|
|
|
|
|
return luaBinCmpDoubleDouble( opcode, m_value, rhs );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// compare two doubles
|
|
|
|
|
public static boolean luaBinCmpDoubleDouble( int opcode, double lhs, double rhs ) {
|
|
|
|
|
switch ( opcode ) {
|
|
|
|
|
case Lua.OP_EQ: return lhs == rhs;
|
|
|
|
|
case Lua.OP_LT: return lhs < rhs;
|
|
|
|
|
case Lua.OP_LE: return lhs <= rhs;
|
|
|
|
|
}
|
|
|
|
|
luaUnsupportedOperation();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Arithmetic negative */
|
|
|
|
|
public LValue luaUnaryMinus() {
|
|
|
|
|
return new LDouble( -m_value );
|
|
|
|
|
}
|
2007-06-08 05:11:37 +00:00
|
|
|
|
|
|
|
|
}
|