Working with Fixed Length Integers (BigInteger)
If we are working with large integer numbers, and want to work out the weight of the average planet, then we would want to use the Fixed Length integers which are implemented using Java BigIntegers. The "+", "-", "*", "/", "==", ">", "<", and so on, operators cannot be used. Instead, the methods of the BigInteger class have to be used when doing arithmetic and comparisons. In the BOM editor, the attribute sub-type should be set to Fixed Length:
The script should be written as shown below:
var totalKgs = ScriptUtil.createBigInteger(0); var planetCount = ScriptUtil.createBigInteger(0); var one = ScriptUtil.createBigInteger(1); for (var iterator = planets.planetList.listIterator(); iterator.hasNext(); ) { var planet = iterator.next(); totalKgs = totalKgs.add(planet.weightKgs); planetCount = planetCount.add(one); } if (planetCount.compareTo(one) >= 0) { planets.averageWeight = totalKgs.divide(planetCount); } else { planets.averageWeight = ScriptUtil.createBigInteger("0"); } |
Or, since the number of planets will not have a very large value, we can have the planet counter as a signed integer, and then convert it into BigInteger for the divide operation at the end of the script:
var totalKgs = ScriptUtil.createBigInteger(0); var planetCount = 0; for (var iterator = planets.planetList.listIterator(); iterator.hasNext(); ) { var planet = iterator.next(); totalKgs = totalKgs.add(planet.weightKgs); planetCount ++; } if (planetCount >= 1) { planets.averageWeight = totalKgs.divide(ScriptUtil.createBigInteger(planetCount)); } else { planets.averageWeight = ScriptUtil.createBigInteger("0"); } |
In order to compare Fixed Integers, you have to use the compareTo() or equals() methods. The equals() method returns true or false depending on whether the values are equal or not. Given two BigInteger variables x and y, the expression
returns the same results as the following when using the Signed Integer sub-type if x and y were signed integer values:
(Where <op> is one of the 6 comparison operators: {==, !=, <, <=, >=, >}).
For example, assuming that variable one has a value of 1 and variable two has a value of 2:
Then:
Operator | Description | Example | Result |
---|---|---|---|
x.compareTo(y) == 0 | Equals | one.compareTo(two) == 0 one.compareTo(one) == 0 |
false true |
x.compareTo(y) != 0 | Not Equals | one.compareTo(two) != 0 one.compareTo(one) != 0 |
true false |
x.compareTo(y) < 0 | Less than | one.compareTo(two) < 0 one.compareTo(one) < 0 two.compareTo(one) < 0 |
true false false |
x.compareTo(y) <= 0 | Less than or equals | one.compareTo(two) <= 0 one.compareTo(one) <= 0 two.compareTo(one) <= 0 |
true true false |
x.compareTo(y) >= 0 | Greater than or equals | one.compareTo(two) >= 0 one.compareTo(one) >= 0 two.compareTo(one) >= 0 |
false true true |
x.compareTo(y) > 0 | Greater than | one.compareTo(two) > 0 one.compareTo(one) > 0 two.compareTo(one) > 0 |
false false true |
There are other methods available on the BigInteger objects, which you can read about in Fixed Length Integer (BigInteger) Methods. These are:
One thing to note about BigIntegers is that they are Immutable, for example, they do not change once they are created. Therefore, all the above methods do not change the object that they are working on. However, if appropriate, they return a new BigInteger object that has the new value.
Another thing to bear in mind when creating BigInteger objects with the ScriptUtil.createBigInteger() method is that the number to create can be passed as a String or a Numeric type. It is important to be aware that the JavaScript numeric type is only accurate to about 16 significant figures, so when initializing large BigInteger values, the value should be passed as a String. Otherwise accuracy can decrease since it is converted to a double and then onto a BigInteger. For example:
The result is the creation of the number 9223372036854776, rather than 12345678901234567890 as might be expected.
For more details on the BigInteger type, see the Java documentation, located at the following web site:
http://download.oracle.com/javase/6/docs/api/java/math/BigInteger.html