LUA - greater than and less than - lua

I am having problems making my new LUA code. I am making a grading system in a test game and I want to use
if Grade Value = <100 and >89
then print("STUDENT GOT AN A!")
but it says it has an error processing the limits and I want for this to be answered.

(This question is better suited on SO but I'll answer it here.)
Your code is just.. wrong! The equal sign is causing the error, and it's not required.
Here's the corrected version (I'm a python programmer so the syntax might be kinda off).
if grade_value <= 100 and grade_value >= 90 then
print("STUDENT GOT AN A")
end
You may have noticed the code works differently, and that's because your current system allows 89.5 but not 100.

Related

using z3 for ALLSAT

I'm using Z3 as a black box to find all possible combinations of some real-world objects with C# code like this:
while (solver.Check() == Status.SATISFIABLE)
{
SATModel = solver.Model;
....
//invert the Model
....
solver.Assert(InvertedModel)
}
For most of my problems the program is working fine, but now I have a bigger problem, where there would be 8.5E+64 possible combinations without constraints.
I'm starting with some 6000 constraints.
What I observe is that the check action takes less than .02 seconds at the beginning and builds up slowly. After 100000 found solutions it takes already 1 second per turn and after 130000 turns I measure 2 seconds.
Is there an easy way to improve the performance?
It's not unreasonable that the solver is taking longer and longer with each constraint. But to make sure it's not some sort of a memory-leak on the C# part, you should check that the time taken in your while loop is really in the Check part and not in the invert/assert part. If you determine z3 is the responsible party, perhaps filing it at https://github.com/Z3Prover/z3/issues might solicit a better answer from the developers.

Programming can be confusing. Ruby language

I am here because I have recently decided to change careers, and considering both my parents are programmers I have always wanted to try out learning to code.
I want to apply to a little academy in San Francisco named App Academy but in order to move forward, I have to pass an exam for them. Their website is:
http://www.appacademy.io/
I've been reading a lot lately trying to prepare for their exam so to speak but somehow I'm still struggling to get the logic out of this. I feel like there are so many different ways of programming something, there are literally no limits sometimes.
In short, here is my question:
Write a method that will take in a number of minutes, and returns a string that formats the number into hours:minutes.
def time_conversion(minutes)
end
Where I'm struggling is that I'm never sure where to start. Every time I attack a new problem somehow I can't figure out the logic. In this problem, please don't provide answers as I want to try it myself. However I would really appreciate some help on learning a methodology to apply for every problem.
For instance, here the first thing I realize is that every hour has 60 minutes so at some point I will have to include that in my answer. Do I have to create a loop? I think so. Keep in mind I DON'T KNOW how to write yet...Perhaps something like:
def time_conversion(minutes)
i = 0
while i > 60
return hours of integer i % 60
return minutes of remainder * 60
else return minutes of remainder * 60
end
I'm sure I got it all wrong....please HELP! Is the logic in the right direction though?
You're going to have to work backwards here. Formatting a string is easily done with the sprintf method, but how do you know what to put in that? Here's the target:
sprintf("%d:%02d", hours, minutes)
Calculating hours and minutes given seconds just requires math:
hours = minutes / 60
minutes = minutes % 60
Then you can combine those two and get your result.
You might be overthinking this. How about the following approach?
def minutes_timestamp(minutes)
"%d:%.2d" % [minutes/60, minutes % 60]
end
EDIT
tadman's answer is correct as well. The above example is equivalent to:
def minutes_timestamp(minutes)
sprintf("%d:%.2d", minutes/60, minutes % 60)
end
The "%d:%.2d" syntax is a bit cryptic, but it simply ensures the resulting string conforms to the HH:MM format, while also ensuring a leading zero for single digit minutes. There's a great tutorial that can explain sprintf and string formatting better than I can here: https://blog.udemy.com/ruby-sprintf/.
Welcome to Stack Overflow. Learning programming is very difficult task as you have to change how you approach problems. Also you need before starting to learn logic to know the syntax of language you are learning. In your case return statement will exit the function and thus not allowing you to go to minutes part.
I can only recommend this book http://poignant.guide/ for a start in Ruby as it's very funny (at least to me). It will help you understand a language. After it you should try solving the logic/approach problem now that you know what you can use.
For finding solutions I can only recommend you to use pen and paper and find a solution there. After you are sure of human logic, try to pretend that you are a computer with access only to data (written on paper and identified by name) and operations that you know code can do and mark the changes in data after each operation.
I.e. you as human know that 1,3,5,7,9... are odd numbers, but given a number x how can you know is it odd in code? This is simple: is_odd = x % 2 == 1 so you can use that part of code whenever you had to check if something is odd. So my advice is to try to approach the problems more as a machine then as a human.
Temporarily ignore the fact that a computer is involved, and pretend you're giving instructions to someone without any common sense, who will follow your instructions to the letter.
What is it that you want the person to do for you? In this case, turn a single number (of minutes) into two numbers (a number of hours, and then a number of minutes left over), and then put them side-by-side with a colon between them.
Because it isn't immediately obvious how to do this, you then break this down:
How can you turn a number of minutes into a number of hours (ignoring the remainder, the formatting etc.)?
Once you have some of the minutes represented as a number of hours, how can you work out the number of minutes left?
Once you have the two numbers, how can you package them up in the desired output representation?
If these steps are too complex to just write the answer to, break each of them down, and so on. The computer code is merely a final imprint of the design.
minutes = 246
"%02d:%02d" % minutes.divmod(60) #=> "04:06"
Fixnum#divmod don't get no respect.

Cocos2d, iOS, Objective-C: float error [duplicate]

This question already has answers here:
How dangerous is it to compare floating point values?
(12 answers)
Error subtracting floating point numbers when passing through 0.0
(4 answers)
Closed 9 years ago.
I tested this on a empty project and does not happen.
As you can see the newValue becomes 2.98023e-08 when I subtract the bossPercentage value.
This happens only when bossPercentage is 0.2f and the previous value is 0.2f.
The difference should be 0.0f but I don't understand why I get 2.98023e-08 instead.
For reference, remainingBossPercentage is a property in [GameController] class defined as following:
//header
#property (readwrite, nonatomic) float remainingBossPercentage;
//.m
#synthetize remainingBossPercentage;
//init
remainingBossPercentage=1.0f;
I'd like to ask you inisght on what I may be doing that causes this error.
EDIT: I subtract 0.2f to remainingBossPercentage (for each boss enemy) and everything works fine until I reach the last enemy object that has again 0.2f and I get to the crucial point of doing 0.2f - 0.2f (screenshot below)
EDIT 2: I am greatful for all comments and answers, also the closing votes. What induced me to ask this question is the fact that newValue is 2.98023e-08. I now see that there are also comparison issues (thanks to the extremely useful QA linked by the people that voted to close the answer). What I wonder is.. why in my new test project with only 2 test variables this does not happen? (I created a HelloWorld project that substracts two floats).
I am asking this because, as one of the user suggests, is important to understand floating points without taking shourtcuts. YES, I am taking a shortcut by asking this question because I don't have time tonight to study it properly but I would like to try understanding and learning at the best I can. I will read the answers properly and dedicate my time to understand but if in the meanwhile I can I would like to add a doubt:
could it be that for memory management reasons the two projects (the test one and my actual game) beheave differently? Could the different beheaviour of the two projects somehow linked with memory being swapped in dirty areas? (e.g. the game having bigger memory usage gets swapped more and hence there may be a loss of precision?)
PS: I found out a question with exactly the same 2.98023e-08 value. What I still wonder is why this doesn't happen in the same test project (I am doing some more testing now).
Simply, floating point numbers should not be expected to be completely accurate.
Floating point numbers (as used in our usual computers) are natively in base 2, out usual number is base 10. Not all numbers in one number base can be expressed with full accuracy in another number base.
As an empale 1/3 can not be expressed with complete accurate in the base 10 number system (0.333333...) but can be in the base 3 number system.
The upshot, one needs to compare floating point numbers with a specified error range. Take the absolute value of the difference and compare that to the allowable range.
Because of this financial amounts are generally not (should not be) expressed as floating point numbers. This give rise to classes such as NSDecimalNumber.

Conjoint analysis based on a orthogonal design

I'm having some issues regarding a conjoint analysis. Excuse me if some of the terms I use are wrong, but it has been some time since I last worked with SPSS - and my teacher was Danish.
Task object
I am to make a series of concept travelpackages (attributes and attribute notes/levels).
This far I've got things under control - I've reduced the number of packages from 81 to 9, with the help of 'orthogonal' design.
These 9 packages have been rated by some people (1-10), on a questionnaire.
Then I've been asked to write a syntax which evaluates my conjoint plan:
CONJOINT PLAN= 'C:\Users\MYNAME\DROBBOXFOLDER\Conjoint_cards.SAV'
/DATA='C:\Users\MYNAME\DROBBOXFOLDER\Respondents.SAV'
/SCORE=Card_1 TO Card_9
/SUBJECT=ID
/FACTORS= SMS Minutter Data Tryghed
/PRINT=ALL
/PLOT=ALL.
However I keep getting this error:
SUBJECT SUBCOMMAND -- Subject variable is not on data file.
Execution of this command stops.
At this point I've been to the dark pages of Google and back for an answer to what I am doing wrong, but nothing so far. The answer is probably staring me in the face. But I will appreciate any help or pointers as to what I'm doing wrong.
Problem solved:
So apparently one shouldn't follow a guide to the letter. My datafile didn't contain a ID, so removing this from my syntax solved the problem.

Why does this code causes the machine to crash?

I am trying to run this code but it keeps crashing:
log10(x):=log(x)/log(10);
char(x):=floor(log10(x))+1;
mantissa(x):=x/10**char(x);
chop(x,d):=(10**char(x))*(floor(mantissa(x)*(10**d))/(10**d));
rnd(x,d):=chop(x+5*10**(char(x)-d-1),d);
d:5;
a:10;
Ibwd:[[30,rnd(integrate((x**60)/(1+10*x^2),x,0,1),d)]];
for n from 30 thru 1 step -1 do Ibwd:append([[n-1,rnd(1/(2*n-1)-a*last(first(Ibwd)),d)]],Ibwd);
Maxima crashes when it evaluates the last line. Any ideas why it may happen?
Thank you so much.
The problem is that the difference becomes negative and your rounding function dies horribly with a negative argument. To find this out, I changed your loop to:
for n from 30 thru 1 step -1 do
block([],
print (1/(2*n-1)-a*last(first(Ibwd))),
print (a*last(first(Ibwd))),
Ibwd: append([[n-1,rnd(1/(2*n-1)-a*last(first(Ibwd)),d)]],Ibwd),
print (Ibwd));
The last difference printed before everything fails miserably is -316539/6125000. So now try
rnd(-1,3)
and see the same problem. This all stems from the fact that you're taking the log of a negative number, which Maxima interprets as a complex number by analytic continuation. Maxima doesn't evaluate this until it absolutely has to and, somewhere in the evaluation code, something's dying horribly.
I don't know the "fix" for your specific example, since I'm not exactly sure what you're trying to do, but hopefully this gives you enough info to find it yourself.
If you want to deconstruct a floating point number, let's first make sure that it is a bigfloat.
say z: 34.1
You can access the parts of a bigfloat by using lisp, and you can also access the mantissa length in bits by ?fpprec.
Thus ?second(z)*2^(?third(z)-?fpprec) gives you :
4799148352916685/140737488355328
and bfloat(%) gives you :
3.41b1.
If you want the mantissa of z as an integer, look at ?second(z)
Now I am not sure what it is that you are trying to accomplish in base 10, but Maxima
does not do internal arithmetic in base 10.
If you want more bits or fewer, you can set fpprec,
which is linked to ?fpprec. fpprec is the "approximate base 10" precision.
Thus fpprec is initially 16
?fpprec is correspondingly 56.
You can easily change them both, e.g. fpprec:100
corresponds to ?fpprec of 335.
If you are diddling around with float representations, you might benefit from knowing
that you can look at any of the lisp by typing, for example,
?print(z)
which prints the internal form using the Lisp print function.
You can also trace any function, your own or system function, by trace.
For example you could consider doing this:
trace(append,rnd,integrate);
If you want to use machine floats, I suggest you use, for the last line,
for n from 30 thru 1 step -1 do :
Ibwd:append([[n-1,rnd(1/(2.0*n- 1.0)-a*last(first(Ibwd)),d)]],Ibwd);
Note the decimal points. But even that is not quite enough, because integration
inserts exact structures like atan(10). Trying to round these things, or compute log
of them is probably not what you want to do. I suspect that Maxima is unhappy because log is given some messy expression that turns out to be negative, even though it initially thought otherwise. It hands the number to the lisp log program which is perfectly happy to return an appropriate common-lisp complex number object. Unfortunately, most of Maxima was written BEFORE LISP HAD COMPLEX NUMBERS.
Thus the result (log -0.5)= #C(-0.6931472 3.1415927) is entirely unexpected to the rest of Maxima. Maxima has its own form for complex numbers, e.g. 3+4*%i.
In particular, the Maxima display program predates the common lisp complex number format and does not know what to do with it.
The error (stack overflow !!!) is from the display program trying to display a common lisp complex number.
How to fix all this? Well, you could try changing your program so it computes what you really want, in which case it probably won't trigger this error. Maxima's display program should be fixed, too. Also, I suspect there is something unfortunate in simplification of logs of numbers that are negative but not obviously so.
This is probably waaay too much information for the original poster, but maybe the paragraph above will help out and also possibly improve Maxima in one or more places.
It appears that your program triggers an error in Maxima's simplification (algebraic identities) code. We are investigating and I hope we have a bug fix soon.
In the meantime, here is an idea. Looks like the bug is triggered by rnd(x, d) when x < 0. I guess rnd is supposed to round x to d digits. To handle x < 0, try this:
rnd(x, d) := if x < 0 then -rnd1(-x, d) else rnd1(x, d);
rnd1(x, d) := (... put the present definition of rnd here ...);
When I do that, the loop runs to completion and Ibwd is a list of values, but I don't know what values to expect.

Resources