I'm getting a warning
The left operand uses '?.', so its value can be null
from this line of code
code = username?.hashCode % 7 ?? 0 % 7;
Not sure what the best practise is and why!
You're trying to use the % 7 operator before you do your null check. You might be calling null % 7, which definitely won't work. Move that outside of your null check.
code = (username?.hashCode ?? 0) % 7;
Related
sorry for asking this question but I couldn't understand it
-- but i don't understand this code
ballDX = math.random(2) == 1 and 100 or -100
--here ballDY will give value between -50 to 50
ballDY = math.random(-50, 50)
I don't understand the structure what is (2) and why it's == 1
Thank you a lot
math.random(x) will randomly return an integer between 1 and x.
So math.random(2) will randomly return 1 or 2.
If it returns 1 (== 1), ballDX will be set to 100.
If it returns 2 (~= 1), ballDX will be set to -100.
A simple way to make a 50-50 chance.
That is a very common way of assigning variables in Lua based on conditionals. It’s the same you’d do, for example, in Python with “foo = a if x else b”:
The first function, math.random(2), returns either 1 or 2. So, if it returns 1 the part math.random(2) == 1 is true and so you assign 100 to the variable ballDX. Otherwise, assign -100 to it.
In lua
result = condition and first or second
basically means the same as
if condition and first ~= nil and first ~= false then
result = first
else
result = second
end
So in your case
if math.random(2) == 1 then
ballDX = 100
else
ballDX = -100
end
in other words, there is a 50/50 chance for ballDX to become 100 or -100
For a better understanding, a look at lua documentation helps a lot :
https://www.lua.org/pil/3.3.html
You can read:
The operator or returns its first argument if it is not false; otherwise, it returns its second argument:
So if the random number is 1 it will return the first argument (100) of the "or" otherwise it will return the second argument (-100).
How do I add an offset to a value obtained from an IndexOf expression? That is, how do I do this?
> import z3
> s = 'hello'
> t = 'e'
> z3.simplify(z3.IndexOf(s, t, 0) + z3.IntVal(1))
z3.z3types.Z3Exception: Non-sequence passed as a sequence
I want to get the location after than of e.
On the other hand, switching the order works as expected
> z3.simplify(z3.IntVal(1) + z3.IndexOf(s, t, 0))
2
You found a bug in z3py!
The bug is on this line: https://github.com/Z3Prover/z3/blob/master/src/api/python/z3/z3.py#L10150
which reads:
return SeqRef(Z3_mk_seq_index(s.ctx_ref(), s.as_ast(), substr.as_ast(), offset.as_ast()), s.ctx)
Instead, it should say:
return ArithRef(Z3_mk_seq_index(s.ctx_ref(), s.as_ast(), substr.as_ast(), offset.as_ast()), s.ctx)
I've reported this at their bug tracker: https://github.com/Z3Prover/z3/issues/2159
Once you make that change in your local-copy of z3.py, your program should work as is. Or you can wait till they release a fix.
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 have some trouble compiling lodepng (http://lodev.org/lodepng/) for D into my project.
In Encode.d I have the following code, where the compiler does not expect the assert statements.
Removing this block solves the issue.
invariant
{
assert(compressionLevel >=0 && compressionLevel <= 9, "invalid zlib compression level");
assert(targetColorType == ColorType.Any ||
targetColorType == ColorType.RGB ||
targetColorType == ColorType.RGBA, "colortype is not supported");
}
In Decode.d I have even more trouble, with the error "no identifier for declarator inout(value)"
for the middle line in:
info.backgroundColor.length = chunk.data.length / 2;
foreach(index, inout value; info.backgroundColor)
value = chunk.data[index * 2];
Is there some trouble with old syntax here, and how do I fix it?
Is there some other way to create png images in D in a simple manner?
Not sure about the invariant problem, but the second problem should be solved by replacing the "inout" with "ref" (D2 syntax change).
I gave up on lodepng, and used the code on
http://www.dsource.org/projects/plot2kill/browser/trunk/png.d
which works after some minor changes.
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.