Basic arithmetic operations

0 views
Skip to first unread message

Simon Bennetts

unread,
Sep 5, 2014, 8:33:07 AM9/5/14
to mozill...@googlegroups.com
I think it would be useful is Zest supported basic arithmetic operations.
This would be useful if you need to extract a numeric value from a response and then manipulate it before using it again.

We could either have a fully structured statement, eg
ZestAssignCalc(String variableName, String operand1, String operator, String operand2)
ZestAssignCalc("result", "{{var2}}, "divideBy", "2")

or we could have a more flexible approach which would require more parsing, eg
ZestAssignCalc(String variableName, String calculation)
ZestAssignCalc("result", "{{var2}} / 2")

Any views?
Could this be useful?
Any preferences as to the approach, or alternative suggestions?

Cheers,

Simon

Lars Kristensen

unread,
Sep 5, 2014, 9:42:08 AM9/5/14
to mozill...@googlegroups.com
Hi Simon,

As a developer, I would prefer the latter example - it's easier to type in and modify, and it's a little more readable than the former. I'm not sure a graphical editor for that example would be intuitive enough for a non-developer, though. An editor for the former example would definitely be more intuitive.

Could it be possible to support both? If only two arguments are passed, then the expression is parsed, and saved in result - If four arguments are passed, the values are calculated using the operator, and the two values :-)

I think it would be a useful feature. Something like in-/decrementing ID's when querying pages could be a possible application. 

Cheers, 

Lars 

Simon Bennetts

unread,
Sep 10, 2014, 11:55:31 AM9/10/14
to mozill...@googlegroups.com
I think I agree, but we will need to make sure that we are very clear as to whats supported and how it should be parsed.
This is especially important as we now have non java versions of the runtime - in particular Sunny's javascript one.

Some options that come to mind:
  1. Just support integers and the plus, minus, multiple and divide operators
  2. As above but also supporting brackets
  3. As above but also supporting fractions
  4. Loads more arithmetic functions, like cos, sin, tan etc etc

I think we should keep it as simple as possible, ie either 1 or maybe 2 at a stretch, but I'm very interested to see what everyone else thinks!

Simon

Sunny

unread,
Jan 11, 2015, 6:32:02 AM1/11/15
to mozill...@googlegroups.com
Hi Simon,

This feature would be really useful. I feel the need for it very often. So I started implementing it in Zest.js, I would like to share and propose a few things from my experience.

In my implementation, a ZestAssignCalc statement's json form looks like
{
  "variableName": <varName>,
  "operandA": <varName/number>,
  "operandB": <varName/number>,
  "operation": <"add"/"subtract"/"multiply"/"divide">,
  "index": <number>,
  "enabled": <boolean>,
  "elementType": "ZestAssignCalc"
}

Here,
"variableName" is the variable where the result of the operation would be stored. The result would be a string. Since we don't have a ZestAssignInteger or a similar statement, we have to store the result in variable as a string. (Needs more discussion)
"operandA" is the first operand of the binary operation. This would be a variable name or a number.
"operandB" is the second operand of the binary operation. This would also be a variable name or a number.
"operation" is the operator of the binary operation. This could be add, subtract, multiply and divide.

Example: 
{
  "variableName": "m",
  "operandA": "m",
  "operandB": 1,
  "operation": "add",
  "index": 12,
  "enabled": true,
  "elementType": "ZestAssignCalc"
}

In the above example statement, we have a variable `m` with some initial value, `operandA` the first operand as `m`, `operandB` the second operand as 1 and `operation` as "add". The above statement would increment `m` by 1.

This has worked pretty well for performing binary arithmetic operations. We sure can add more operations to it like the modulo operation, exponent operation, etc once we agree on a given form.


I would like to know what others think of this proposed json form for ZestAssignCalc. Maybe suggest some better attribute names for operands and operation?
Do we need more statements like ZestAssignInteger?


Thanks

Simon Bennetts

unread,
Jan 14, 2015, 11:46:17 AM1/14/15
to mozill...@googlegroups.com
Great proposal - I'll start on the Java version asap :D
I dont think we need a ZestAssignInteger, we can just use 'standard' Zest variables and parse them as ints - failing if they are not.
However a ZestExpressionIsInteger might be useful?

Many thanks,

Simon

Simon Bennetts

unread,
Jan 19, 2015, 8:18:29 AM1/19/15
to mozill...@googlegroups.com
I've implemented both ZestAssignCalc and ZestExpressionIsInteger in the Java version: https://github.com/mozilla/zest/issues/69

That ok?

Simon

Sunny

unread,
Jan 19, 2015, 8:42:58 AM1/19/15
to mozill...@googlegroups.com
Yeah! Now I got to implement ZestExpressionIsInteger in js.

Tell me if this is the right format:

{
  "variableName": <string>,
  "not": <boolean>,
  "elementType": "ZestExpressionIsInteger"

Simon Bennetts

unread,
Jan 19, 2015, 8:48:52 AM1/19/15
to mozill...@googlegroups.com
Thats it :)

We really should have a compatibility test suite that we can run against the different implementations :/

Sunny

unread,
Jan 20, 2015, 7:31:17 AM1/20/15
to mozill...@googlegroups.com
I have implemented ZestExpressionIsInteger in Zest-runner js ( https://github.com/darkowlzz/zest-runner/commit/d2eaa7d04ad0e67269bbe196106e6895a8ca6b4f ).

Yes, we really need a compatibility test suit, but I am not sure how to do that. Maybe log some data to files for both the implementation and compare them?
Would look for a better solution.
Reply all
Reply to author
Forward
0 new messages