I have the task, and I can understand how to deal with it (in what direction I need to start). Application need to understand user-inputted condition like that:
((VAR1 != 1 && VAR2 == 2) OR (VAR3 != 1 && VAR4 == 2)) AND (VAR5 = 2)
I have that variables inside my database, so it's no problem to replace VAR1 with real data. How in what way I can process that condition inside application. I thought about separating full condition to little blocks, but I don't understand yet how to separate, there is no symbol that I can split string.
So can you help in what direction I need to start working to process such conditions in Swift app?
Thanks to #Sulthan, NSPredicate is solution.
let expressionString = "((1 == 1) or (1 == 2)) and (1 == 2)"
let predicate = NSPredicate(format: expressionString)
print(predicate.evaluateWithObject(nil))
// Output: false
Related
I am trying to model graph connectivity with Z3. Specifically I am partitioning a graph and need the subgraphs to remain connected. However TransitiveClosure doesn't work as I expect. I model edges with F
Here's a MWE:
s = Solver()
N = DeclareSort('N')
a,b,c = Consts('a b c', N)
F = Function(N,N,BoolSort())
s.add(F(A,B) == True)
s.add(F(B,A) == True)
s.add(F(B,C) == False)
s.add(F(C,B) == False)
s.add(F(A,C) == False)
s.add(F(C,A) == False)
s.add(A != B, B != C, C != A)
FX = TransitiveClosure(F)
s.add(FX(A,C))
This is apparently SAT, which doesn't make much sense to me. If I change s.add(FX(A,C)) to s.add(Not(FX(A,C))).
Why is this? C should not be a member of FX. Am I somehow setting FX(A,C) == True) by adding it to the model? Why doesn't that conflict with the definition of FX.
The output/connection lines of the model are hard to understand so I'm not quite sure what's going on.
I am working with tclosures as well and found this helpful: https://theory.stanford.edu/~nikolaj/programmingz3.html#sec-transitive-closure
Maybe there is an answer to your 2nd question as well.
I need to have a condition with logical operator OR. I try to do in this way but it seem doesn't work :
<div th:if="${(fingerprints.totalPages != 0) or (fingerprints.totalPages != 1)}"
How I suppose to do ? :)
Both or and || work. In this case, your logic is wrong. I think your expression should be:
th:if="${(fingerprints.totalPages != 0) and (fingerprints.totalPages != 1)}"
Your original expression is always true. (Since fingerprints.totalPages is always going to be either != 1 or != 0.
This question already has answers here:
What is a simple example of floating point/rounding error?
(9 answers)
Why is Lua arithmetic is not equal to itself? [duplicate]
(1 answer)
Closed 9 years ago.
Today morning I found a bug on my Lua Script, wich seem very weird. How can this evaluation fail this way? Examples can be tested in here
First example:
if( math.abs(29.7 - 30) <= 0.3 ) then
result = 1
else
result = 0
end
print("result = "..result )
-->> result = 0
Second example:
if( 0.3 <= 0.3 ) then
result = 1
else
result = 0
end
print("result = "..result )
-->> result = 1
Third example
if( math.abs(29.7-30) == 0.3 )then
print("Lua says: "..math.abs(29.7-30).." == 0.3")
else
print("Lua says: "..math.abs(29.7-30).." ~= 0.3")
end
-->> Lua says: 0.3 ~= 0.3 WHAT?
I am really confuse, and I would like to understand this to avoid similiar bugs in the future. Thanks
You are being hit by the fact that Lua uses (IEEE 754) 64-bit double-precision floating point numbers.
Look at the following examples
> print(0.3 == 0.3)
true
> print(0.3 <= 0.3)
true
> print(0.3 >= 0.3)
true
The actual value of 0.3 in memory is:
> print(string.format("%1.64f",math.abs(-0.3)))
0.2999999999999999888977697537484345957636833190917968750000000000
Now look at you example:
> print(math.abs(29.7-30) == 0.3)
false
> print(math.abs(29.7-30) >= 0.3)
true
> print(math.abs(29.7-30) <= 0.3)
false
The actual value of 29.7-30 is:
> print(string.format("%1.64f",29.7-30))
-0.3000000000000007105427357601001858711242675781250000000000000000
The actual value of math.abs(29.7-30) is:
> print(string.format("%1.64f", math.abs(29.7-30))
0.3000000000000007105427357601001858711242675781250000000000000000
And just for fun the value of math.abs(-0.3) is:
> print(string.format("%1.64f", math.abs(-0.3)))
0.2999999999999999888977697537484345957636833190917968750000000000
There are two solutions to you problem, the first is read What Every Computer Scientist Should Know About Floating-Point Arithmetic, and understand it :-). The second solution is to configure Lua to use another type for numbers, see Values and Types for hints.
Edit
I just thought of another way of "solving" the problem, but it is a bit of a hack, and not guarantied to always work. You can use fixed point numbers in lua by first converting the float to a string with a fixed precision.
In your case that would look something like:
a = string.format("%1.1f", math.abs(29.7 - 30))
print(a == "0.3")
or a bit more robust:
a = string.format("%1.1f", math.abs(29.7 - 30))
print(a == string.format("%1.1f", 0.3))
However you must make sure that you use a precision that is both adequate and the same for all you comparisons.
As we know, float point has a precision problem
Refer: http://lua-users.org/wiki/FloatingPoint
a = 1
if a < 1 then print("<1") end
Will never print "<1". Not unless you actually change a
I'm trying to get Velocity to output the following Javascript code:
if ((whichOne+1) <= numCallouts ) {
whichOne = whichOne + 1; } else {
whichOne = 1;
}
Whenever I try to get Velocity to print a > or a <, it represents it as a & gt; or & lt;, which doesn't help me since I'm trying to get it to produce Javascript. I've tried:
#set ( $gt = ">" )
But even that ends up as a & gt;
Thanks in advance.
It is not a default behavior, the only reason I can think of why this is happening is if you have event ReferenceInsertionEventHandler configured with EscapeHtmlReference either in your velocity.config or in the Velocity initialization code.
Here is more info about events
I've had the same issue with Velocity - however, the problem is that I was using Velocity as an third party embedded language, and didn't have access to change the Velocity settings.
Unfortunately the only solution I was able to find was to rewrite the code without using greater than/less than explicitly, which admittedly is awful, but it's all about getting it to work...
Here is an example workaround for conditionals where you are trying to see if one number is larger than another:
if (n1 > n2) //Doesn't work because velocity turns this into if (n1 > n2)
if (n1 != n2)
{
diff = n1 - n2;
abs = abs(n1 - n2);
if (diff / abs == 1) //Greater than
else //if == -1 then less than
}
else //Equal
Maybe you are able to use the alternate symbols as described here :
http://velocity.apache.org/engine/devel/vtl-reference-guide.html#aifelseifelse_-_Output_conditional_on_truth_of_statements
So try to use if (n1 gt n2).
In Lua, using the = operator without an l-value seems to be equivalent to a print(r-value), here are a few examples run in the Lua standalone interpreter:
> = a
nil
> a = 8
> = a
8
> = 'hello'
hello
> = print
function: 003657C8
And so on...
My question is : where can I find a detailed description of this use for the = operator? How does it work? Is it by implying a special default l-value? I guess the root of my problem is that I have no clue what to type in Google to find info about it :-)
edit:
Thanks for the answers, you are right it's a feature of the interpreter. Silly question, for I don't know which reason I completely overlooked the obvious. I should avoid posting before the morning coffee :-) For completeness, here is the code dealing with this in the interpreter:
while ((status = loadline(L)) != -1) {
if (status == 0) status = docall(L, 0, 0);
report(L, status);
if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
lua_getglobal(L, "print");
lua_insert(L, 1);
if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
l_message(progname, lua_pushfstring(L,
"error calling " LUA_QL("print") " (%s)",
lua_tostring(L, -1)));
}
}
edit2:
To be really complete, the whole trick about pushing values on the stack is in the "pushline" function:
if (firstline && b[0] == '=') /* first line starts with `=' ? */
lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
Quoting the man page:
In interactive mode ... If a line starts with '=', then lua displays the values of all the expressions in the remainder of the line. The expressions must be separated by commas.
I think that must be a feature of the stand alone interpreter. I can't make that work on anything I have compiled lua into.
I wouldn't call it a feature - the interpreter just returns the result of the statement. It's his job, isn't it?
Assignment isn't an expression that returns something in Lua like it is in C.