When should I apply demorgans law in programming? [closed] - demorgans-law

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I think any programmer who has taken an intro to programming class had to memorize DeMorgan's law.
In case you don't know what it is, here is the gist
!(A && B) = !A || !B
!(A || B) = !A && !B
I was under the assumption that if i had to memorize it, I would find it applicable in a programming situation. But I haven't had to use it at all.
Is there any reason why I should use it in programming? Does it make the program faster, or does it make conditions easier to read?

Keeping code readable is a big reason, and I'm sure whoever works on your code after you will agree it's an important one. And you'll save a CPU cycle or two if you only invert (!) one value instead of two.
Another reason is to bypass short-circuiting. When many languages see !A || !B they stop evaluating if !A is true because it doesn't matter if !B is true or not. !A is enough for the OR to be true.
If A and B are functions instead of variables, and you want both to execute, you're gonna have a problem:
if( !save_record() || !save_another_record() ) {
echo 'Something bad happened';
}
Demorgan's laws let you replace that OR with an AND. Both sides of the AND need to be evaluated to make sure it's true:
if( !( save_record() && save_another_record() ) ) {
echo 'Something bad happened';
}

Related

For loop includes zero [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have fixed my problem by telling my loop to ignore instances of 0. But I don't know why it wants to include them in the first place.
P <- function(n) {
if (n <= 1) {
return (1)
}
s = 0
for (i in 1 : n - 1) {
if (i != 0) {
s <- s + P(i) * P(n - i)
}
}
return (s)
}
print (P(2))
It seems weird to include that in my loop, but without it I simply haven't been able to get the program to work. Not until I started putting in a trace to see what I was did I discover that i = 0. Did I write something wrong? I'm too new at R to even consider blaming this on the RGui that I'm using to run this, but I'm too old at programming to think anything else.
It is a precedent problem. ":" takes precedent over "-"
Compare:
n<-5
c(1: n -1)
c(1:(n-1))
This is because you forgot a parenthesis, see operator syntax & precedence :
n <- 2
1: n-1
[1] 0 1
1:(n-1)
[1] 1

How can we correctly determine if two floating point variables are equal? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm doing a quiz and I wonder if I can correctly determine it. These are options:
See if a == b is true
Check if a - b == 0 is true
Check if ((a-b)*(a-b))/(a*a + b*b) is close to or is zero
Check a*b == a*a is true
None of the above
I tried to compare float and double values in C++ and I didn't succeed with any of the given formulas. I'd say "None of the above" but I don't know how it works in other languages. I read somewhere that in Java could be different but I would choose the last options as generally other answers are incorrect. What do you think?
The floating-point comparison operation is “perfect” in that it returns true if and only if the tested relation is true. There are never any rounding errors. There is incorrect folklore that you cannot compare floating-point numbers or need to use some tolerance to compare them, but this is because earlier operations that prepare the operands to a comparison typically have rounding errors, similar to the way 7/3 rounds to 2 in integer arithmetic.
If your quiz is based on correct principles of floating-point arithmetic, then “See if a == b is true” is correct. Otherwise, the answer is unclear and the quiz is suspect.

Code to calculate a certain arithmetic expression in assemlby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Expression: z=a+b * b-(36/(b * b)/(1+(25/(b * b)))
I have no idea what data directives I should use and in what order I should write the code.
C = A + B for Z80 CPU:
ld a,A
ld b,B
add a,b ; a = C
C = A * B for 68000 CPU:
MOVE.W D0,A
MOVE.W D1,B
MULS.W D1,D0 ; D0 = C
et cetera ... check your target CPU instruction guide to see what arithmetic operations it does implement directly, and what operand types can be used for them, which registers you have available, and their data type...
Looks like you don't have to write universal math expression parser (this gets tricky quite quickly, once at high-school we had on programming competition the task to write exactly that, and at first we were like "what, a single task for 5h time, I will be done in 30min" ... then after 5h nobody's code passed the full test suite, best were around 80% correct).
So if only this particular expression should be calculated, you can "parse" it by hand, simplifying it down into particular steps involving only single operation and one of intermediate sub-results. Then just write that with your instructions, step by step, like you would calculate that by hand (also make sure you conform to the math expression calculation rules, you know which operations have priority over others? Parentheses override anything, then mul/div first, add/sub later, from left to right, but this is base school math stuff, so unless you are 10y.o., you shouldn't ask this).
If your CPU does not have instruction for division or multiplication, simply implement it by subtraction/addition in loop. It's very lame performance wise, but if you have to ask this question, then one can not expect you would even comprehend more advanced algorithm for these.

Why do Clojure vector function results exclude the stop value? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Forgive me for the newb question and potentially incorrect terminology.
Clojure vector functions produce values that do not include the stop value. For example:
=> (subvec [:peanut :butter :and :jelly] 1 3)
[:butter :and]
=> (range 1 5)
(1 2 3 4)
The doc for range explicitly states this but doesn't give a rational: "...Returns a lazy seq of nums from start (inclusive) to end (exclusive)...".
In Ruby these operations are inclusive:
(1..5).to_a
=> [1, 2, 3, 4, 5]
[:peanut, :butter, :and, :jelly][1,3]
=> [:butter, :and, :jelly]
Obviously these are very different languages, but I'm wondering if there was some underlying reason, beyond a personal preference by the language designers?
Making the end exclusive allows you to do things like specify (count collection) as the endpoint without getting an NPE. That's about the biggest difference between the two approaches.
It might be that the indexing was chosen in order to be consistent with Java libraries. java.lang.String.substring and java.util.List.subList both have exclusive-end indexes.

Why does my calculator return true for 2^34 == 2^34 - 1? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm asking this question here because I believe this is more of a programmatic issue than anything else.
I'm using a TI-84 Plus Sliver Edition calculator that contains logical operators that can test for less-than, greater-than, and equality. I find that when I input the expression:
2^34 == 2^34 - 1
it gives me a surprising 1 for true. It's worth mentioning that my calculator can't precisely output the result of 2^34. Rather, it uses the exponential notation for it and any other powers greater than 33. Is this a potential factor in the boolean output?
Furthermore, the equality test only returns true if the second expression is subtracting by 1 to 9. When the number is >= 10 it then correctly returns false.
Could it be a rounding error? Why is this expression returning true?
Your calculator cant keep track of numbers that large.
Every calculator has a set level of precision (lets say 10 digits). Every answer the calculator gives is rounded so that the answer has that many digits, then it shifts the decimal place as much as it needs to in order to make the number big or small (in your case very large).
Your number is so large that when you subtract 1, that causes a change after the 10th digit. This then gets rounded back to what you started with, and then compared. So naturally, it thinks they are the same number (to the precision it is capable of).

Resources