mirror of
https://github.com/zeroflag/fcl.git
synced 2025-01-11 20:01:10 +01:00
hex/bin/dec conversions
This commit is contained in:
parent
40e8a28595
commit
330f2d7f53
1 changed files with 25 additions and 11 deletions
|
@ -1,16 +1,15 @@
|
||||||
package com.vectron.fcl.types;
|
package com.vectron.fcl.types;
|
||||||
|
|
||||||
|
import static com.vectron.fcl.Fcl.STRICT;
|
||||||
|
|
||||||
import com.vectron.fcl.exceptions.NotUnderstood;
|
import com.vectron.fcl.exceptions.NotUnderstood;
|
||||||
import com.vectron.fcl.exceptions.TypeMismatched;
|
import com.vectron.fcl.exceptions.TypeMismatched;
|
||||||
|
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.text.DecimalFormatSymbols;
|
import java.text.DecimalFormatSymbols;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import static com.vectron.fcl.Fcl.STRICT;
|
|
||||||
|
|
||||||
public class Num implements Obj, LogicOperand, ArithmeticOperand {
|
public class Num implements Obj, LogicOperand, ArithmeticOperand {
|
||||||
public static final Num ZERO = new Num(0);
|
public static final Num ZERO = new Num(0);
|
||||||
public static final Num ONE = new Num(1);
|
public static final Num ONE = new Num(1);
|
||||||
|
@ -61,10 +60,26 @@ public class Num implements Obj, LogicOperand, ArithmeticOperand {
|
||||||
return value.toString();
|
return value.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String display() {
|
public String display(int radix) {
|
||||||
return value instanceof Long || value instanceof Double
|
if (value instanceof Long || value instanceof Double) {
|
||||||
? format.format(value)
|
switch (radix) {
|
||||||
: value.toString();
|
case 16:
|
||||||
|
return value instanceof Long || approximatelyLong()
|
||||||
|
? String.format("0x%04X", longValue())
|
||||||
|
: format.format(value);
|
||||||
|
case 2:
|
||||||
|
return value instanceof Long || approximatelyLong()
|
||||||
|
? String.format("%8sb", Long.toBinaryString(longValue())).replace(' ', '0')
|
||||||
|
: format.format(value);
|
||||||
|
default:
|
||||||
|
return format.format(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean approximatelyLong() {
|
||||||
|
return Math.abs(longValue() - doubleValue()) < 0.001;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -141,9 +156,8 @@ public class Num implements Obj, LogicOperand, ArithmeticOperand {
|
||||||
return new Num(Math.pow(doubleValue(), other.doubleValue()));
|
return new Num(Math.pow(doubleValue(), other.doubleValue()));
|
||||||
} else if (other.iterable().boolValue()) {
|
} else if (other.iterable().boolValue()) {
|
||||||
Lst result = Lst.empty();
|
Lst result = Lst.empty();
|
||||||
Iterator<Obj> it = ((Iterable)other).iterator();
|
for (Obj obj : (Iterable<Obj>) other)
|
||||||
while (it.hasNext())
|
result.append(this.pow(obj));
|
||||||
result.append(this.pow(it.next()));
|
|
||||||
return result;
|
return result;
|
||||||
} else if (STRICT) {
|
} else if (STRICT) {
|
||||||
throw new TypeMismatched("pow", this, other);
|
throw new TypeMismatched("pow", this, other);
|
||||||
|
@ -252,7 +266,7 @@ public class Num implements Obj, LogicOperand, ArithmeticOperand {
|
||||||
if (STRICT)
|
if (STRICT)
|
||||||
throw new TypeMismatched(this, "bool");
|
throw new TypeMismatched(this, "bool");
|
||||||
else
|
else
|
||||||
return value.longValue() != 0l;
|
return value.longValue() != 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue