You should cast either the numerator or denominator to float - it does not help you to cast the first term of the subtraction. Only if you change one or both of the numerator or denominator to a real number, do you get a real division. BeanShell correctly evaluates the division before the subtraction - even if the Micro$**t Windoze calculator does not. That is, the inner expression should be
origAssault - (float)origAssault / origSP * SPLoss
This can obviously be simplified, and you can do away with the ternary condition with
origAssault * (1 - (float)SPLoss / origSP)
This is the entire expression - if SPLoss is 0, then the second term in the second factor is 0 and you multiply origAssault with the identity (1).
To round to integer, you can use Math.round
Math.round(origAssault * (1 - (float)SPLoss / origSP))
or “poor-man’s” rounding by adding 0.5 and casting to int (effectively flooring)
(int)(origAssault * (1 - (float)SPLoss / origSP) + 0.5)
Note, neither are true rounding because it does not disambiguate 0.50..., which means Math.round has little advantage over the “poor-man’s” - except, perhaps, clarity.
Note, rather than using JavaScript to test things out, you can use the BeanShell interpretor stand-alone. For example, if you put the following code in a file called - say - test.bsh
// Set-up
SPLoss = 0;
origAssault = 25;
origSPs = 13;
// Calculate value - a little too complicated
New = (SPLoss != 0 ?
(origAssault - ((origAssault/origSPs)*SPLoss))
: origAssault);
Expect = 25;
print("SPLoss ="+SPLoss);
print("origAssault="+origAssault);
print("origSPs ="+origSPs);
print("test ="+New+" (expected="+Expect+")");
// Change input
SPLoss = 4;
print("SPLoss ="+SPLoss);
// Calculate value - integer division
New = (SPLoss != 0 ?
(origAssault - ((origAssault/origSPs)*SPLoss))
: origAssault);
Expect = 17.307692307692307;
print("test ="+New+" (expected="+Expect+")");
// Calculate value - float division (cast numerator) and simplified
New = (origAssault - (((float)origAssault/origSPs)*SPLoss));
print("test ="+New+" (expected="+Expect+")");
// Calculate value - factor and simplify
New = origAssault * (1 - (float)SPLoss / origSPs);
print("test ="+New+" (expected="+Expect+")");
// Calculate value - float division, simplified, and rounded
New = Math.round(origAssault * (1 - (float)SPLoss / origSPs));
Expect = 17;
print("test ="+New+" (expected="+Expect+")");
// Calculate value - float division, simplified, and poor-man rounded
New = (int)(origAssault * (1 - (float)SPLoss / origSPs)+.5);
Expect = 17;
print("test ="+New+" (expected="+Expect+")");
and then run
java -cp /usr/share/java/jline.jar:/usr/share/java/bsh.jar jline.ConsoleRunner bsh.Interpreter test.bsh
you get a more valid test (adjust for your installation of BeanShell).
Though the development of BeanShell it self seems rather stale, not that Vassal uses and older or patched version of BeanShell, which means one cannot be entirely certain that Vassal will support everything that stand-alone BeanShell supports.
Yours,
Christian