Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Using Tap Forms › Math and Logic Operators
- This topic has 2 replies, 2 voices, and was last updated 7 years, 3 months ago by Mike Schwartz.
-
AuthorPosts
-
September 3, 2017 at 8:17 PM #24388
Mike SchwartzParticipantBrendan,
I’m starting a new thread branching off https://www.tapforms.com/forums/topic/is-there-a-way-to-edit-check-a-field/#post-24379, to continue the discussion about math and logic operators. I did some testing with the operators that were mentioned in Math Parser:
Arithmetic Operators: +, -, /, *, ^(power), %(mod)
Boolean Operators: <, >, =, &, |, ! ,<>, >=, <=In particular, I focused on the & (AND), | (OR), and ! (NOT) Boolean operators, and the ^(power) and %(modulo) arithmetic operators.
=============================================================
Boolean: I created a new form with two CheckMark fields, Check1 and Check2, and three calculation fields named AND, OR, and NOT with the formulas
Check1&Check2
,Check1|Check2
, and!Check1
.RESULTS: The AND
&
and NOT!
operators yield the correct values of 0 or 1 for all combinations of CheckMark values. However, the pipe operator|
does not work for OR. In fact, the Edit Formula sheet won’t even accept the pipe character — it appears on the sheet as you edit the formula, but after you click “Save” the pipe just vanishes from the saved formula. Doing a little research, it looks like the accepted symbol for the OR operation in Objective C is a double pipe ||. I tried that too, but again the pipes disappeared upon saving. I also tried /, //, \, and \\, but none of them worked either. To summarize: AND&
and NOT!
work in Tap Forms; OR|
does not work. Any insight into that?=============================================================
Arithmetic: I added two number fields to the form, Number1 and Number2, along with two more calculation fields named POWER and MODULO with the formulas
Number1^Number2
andNumber1%Number2
.RESULTS: The POWER operator yields mixed results.
Correct: 2^10 = 1024; 100^0.5 = 10; 100^-0.5 = 0.1
Incorrect: (-10)^2 = -100 (Remember, these are two variables A (-10) and B (2) stored in fields. A^B is (-10)^2 which is +100. It’s as if the parser is simply jamming the strings together to get -10^2, then using the rules of algebra to evaluate the exponent before evaluating the minus sign. But it’s still an incorrect result. The parser should be inserting parentheses around the base variable to properly account for negative numbers.The MODULO operator does not calculate properly at all:
Incorrect: 8%10 = 0 (should be 8); 18%10 = 1 (should be 8); 28%10 = 2 (should be 8). So it looks like the A%B yields the integer portion of A divided by B, instead of the remainder portion of A divided by B.=============================================================
Summary:
AND&
and NOT!
work; OR|
does not work.
POWER^
works great, even with negative or fractional exponents, as long as the base number is positive. If the base number is negative, the operator will treat the base as being positive, and then multiply the result by (-1).
MODULO%
does not produce the expected result at all.Can anything be done about this?
Thanks,
MikeSeptember 4, 2017 at 10:47 AM #24396
BrendanKeymasterHi Mike,
Well one issue is that internally I use the
|
character to separate the field references from the rest of the formula. So that clearly breaks using that as the or symbol. I’ll have to come up with a different character to use instead for the OR operator. That really sucks because|
is always used to represent logical OR in programming. Currently the code requires that it be a single character for an operator. When Tap Forms saves the formula to the database, it stores it like this:[fld-2343432423] | * 5 + | [fld-4534905940539]
So there are already
|
characters within the stored formula.Not sure about the POWER function. This is the code for that:
return @(pow([[parameters[0] getValue] doubleValue], [[parameters[1] getValue] doubleValue]));
So it’s not doing anything more complex than calling the built-in
pow(X,Y)
function.and the MODULO function appears to be calling the
intdiv
function, which does this:return @(floor(floor([[parameters[0] getValue] doubleValue]) / floor([[parameters[1] getValue] doubleValue])));
I’m not sure why it’s not calling the MODULO function though.
The MODULO function does this:
NSInteger x = floor([[parameters[0] getValue] doubleValue]); NSInteger y = floor([[parameters[1] getValue] doubleValue]); return @(x % y);
I can change that easily. And I just did.
September 4, 2017 at 12:58 PM #24410
Mike SchwartzParticipantOK, I did some additional testing. I added another calculation field POW with the formula POW(Number1,Number2) and it had no problem returning +100 with arguments -10 and 2. But the previous calculation field that uses Number1^Number2 still returns -100. I changed the latter formula to (Number1)^Number2, and that corrected the issue with negative base numbers. This is strictly a work-around.
Concerning the logical OR operator, I’ll suggest the backslash character \. It sorta looks like a drunken pipe. Thanks for fixing the MOD operator.
— Mike
-
AuthorPosts
You must be logged in to reply to this topic.