Observations: Math in DynamicProperties

Hello,

Just started using build svn7835. I use DP calls to make math changes to DynamicProps. I have noticed that:

  1. Only one operator can be executed per keyword.
  2. Division requires an Integer in the denominator unless it is a constant (a number like 2.0).

Exempli gratia,

Name: NewRoot
Value: 1.0
Numeric: check
Key Command: [sqrt ] [set value directly] [{Range_sq/Root}]
Key Command: [sqrt ] [set value directly] [{NewRoot+Root}]
Key Command: [sqrt ] [set value directly] [{NewRoot/2.0}

{Root} then gets updated with {NewRoot} and the sequence is repeated (Newton’s Method for square roots).

When {NewRoot} is a float (like 200.5) and updates {Root}, The first [sqrt] keyword command fails. I copy a Report below.

Starting with Range_sq = 400 and Root = 400:

  • Root=400, NewRoot=1

  • Root=400, NewRoot=401

  • Root=400, NewRoot=200.5

  • Root=200.5, NewRoot=

  • Root=200.5, NewRoot=200.5

  • Root=200.5, NewRoot=

Any special information about Math in 3.2?

Thanks

Flaney

Update report

  • Root=400, NewRoot=1
  • Root=400, NewRoot=401
  • Root=400, NewRoot=200.5
  • Bad Data in Module: HeavyWeapon (3) [Dynamic Property - NewRoot] - Expression evaluation error {Range_sq/Root}
  • Root=200.5, NewRoot=
  • Root=200.5, NewRoot=200.5
  • Root=200.5, NewRoot=
  • Bad Data in Module: HeavyWeapon (3) [Dynamic Property - NewRoot] - Expression evaluation error {NewRoot/2.0}

Hi,

Thanks for trying this out.

No such restriction exists. Your expressions can be arbitrarily complex with any number of operators and Property references.

It’s doesn’t handle non-integer arithmetic very well at the moment. It will return results with decimal places from divisions, but any attempt to use them as expression values will fail. This is basically the same as the original arithmetic, but it is not failing quite so gracefully. Even if an expression returns a floating point value, there is very little you can do with it in Vassal.

Is there a particular reason you want to do floating point arithmetic? Or are you just testing?

I should be able to extend the Expression interpreters to support floating point if you can convince me it will be useful.

Rgds,
Brent.

Ok,
It turned out this was really easy to fix. Arithmetic now follows Java rules. If you divide one integer into another integer, you get an integer result. If either value is a float, you get a float result. To force a float result from fividing two integers, you need to cast one to a float value:-

{ property1 / (float) property2 }

Note you can use any elements of the Java language in expressions, plus utlity classes like Math.

Joel, the fix is in branch Brent-3.2@7865

Thus spake Brent Easton:

Ok,
It turned out this was really easy to fix. Arithmetic now follows Java
rules. If you divide one integer into another integer, you get an
integer result. If either value is a float, you get a float result. To
force a float result from fividing two integers, you need to cast one to
a float value:-

{ property1 / (float) property2 }

Note you can use any elements of the Java language in expressions, plus
utlity classes like Math.

Joel, the fix is in branch Brent-3.2@7865

Merged to trunk@7868.


J.

Hello Brent,

I am using floating point to calculate the distance a token is moved. Using a loop implementing Newton’s Method, one can get the square root of the (X^2 + Y^2). You say “Note you can use any elements of the Java language in expressions, plus utility classes like Math”, Does this mean Square Roots are now available?

Thanks for including in the builds!

Flaney

Yes Indeed

{ Math.sqrt (X * X + Y * Y) }

Brent.

Do you have a reference for the Math syntax?

Thanks!

Any Java reference or tutorial. The expressions are interpreted Java with access to a subset of the standard Java API.