logical-operators

What happens when you logical not a float?

核能气质少年 提交于 2019-11-27 03:45:38
问题 I assume this just returns an int. Is there anything else going on I should be aware of? C/C++ differences? float a = 2.5; !a; // What does this return? Int? Float? 回答1: Regarding C++, quoting C++11 §5.3.1/9: The operand of the logical negation operator ! is contextually converted to bool ; its value is true if the converted operand is false and false otherwise. The type of the result is bool . So what's really relevant here is the behavior of static_cast<bool>(some_float) – quoting §4.12/1:

Python: Avoid short circuit evaluation

时光总嘲笑我的痴心妄想 提交于 2019-11-27 03:15:50
问题 This is a problem that occured to me while working on a Django project. It's about form validation. In Django, when you have a submitted form, you can call is_valid() on the corresponding form object to trigger the validation and return a Boolean value. So, usually you have code like that inside your view functions: if form.is_valid(): # code to save the form data is_valid() not only validates the form data but also adds error messages to the form object that can afterwards be displayed to

C Preprocessor testing definedness of multiple macros

半城伤御伤魂 提交于 2019-11-27 02:07:15
问题 I searched the site but did not find the answer I was looking for so here is a really quick question. I am trying to do something like that : #ifdef _WIN32 || _WIN64 #include <conio.h> #endif How can I do such a thing? I know that _WIN32 is defined for both 32 and 64 bit windows so I would be okay with either for windows detection. I am more interested in whether I can use logical operators like that with preprocessor directives, and if yes how, since the above does not work. Compiling with

Using multiple criteria in subset function and logical operators

心已入冬 提交于 2019-11-27 01:28:43
If I want to select a subset of data in R, I can use the subset function. I wanted to base an analysis on data that that was matching one of a few criteria, e.g. that a certain variable was either 1, 2 or 3. I tried myNewDataFrame <- subset(bigfive, subset = (bigfive$bf11==(1||2||3))) It did always just select values that matched the first of the criteria, here 1. My assumption was that it would start with 1 and if it does evaluate to "false" it would go on to 2 and than to 3, and if none matches the statement after == is "false" and if one of them matches, it is "true". I got the right result

How is i==(20||10) evaluated?

南楼画角 提交于 2019-11-26 23:44:09
问题 #include <stdio.h> int main(void) { int i=10; if(i==(20||10)) printf("True"); else printf("False"); return 0; } This gives the output False . Please explain to me how does this program work? 回答1: This line if(i==(20||10)) always evaluates to i==1 as Alk said in comments - (20||10) evaluates to 1 , hence when you compare i == 1 , that is why you get False as the output. A non-Zero value in C implies true. Read about Short-circuit evaluation Perhaps this is what you wanted: int i=10; if(i==20 |

R gotcha: logical-and operator for combining conditions is & not &&

不想你离开。 提交于 2019-11-26 22:40:18
Why doesn't subset() work with a logical and && operator combining two conditions? > subset(tt, (customer_id==177 && visit_date=="2010-08-26")) <0 rows> (or 0-length row.names) but they each work individually: > subset(tt, customer_id==177) > subset(tt, visit_date=="2010-08-26") (Want to avoid using large temporary variables - my dataset is huge) From the help page for Logical Operators , accessible by ?"&&" : & and && indicate logical AND and | and || indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates

Is the “true” result of >, <, !, &&, || or == defined?

好久不见. 提交于 2019-11-26 22:25:30
问题 When I for instance write 7>1 in C (say C99 if this is not an always-been feature), can I expect the result will be exactly 1 or just some non-zero value? Does this hold for all bool operators? 回答1: In C99 §6.5.8 Relational Operators, item 6 ( < , > , <= and >= ): Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false) The result has type int . As for equality

Is BitArray faster in C# for getting a bit value than a simple conjuction with bitwise shift?

匆匆过客 提交于 2019-11-26 22:25:27
问题 1). var bitValue = (byteValue & (1 << bitNumber)) != 0; 2). using System.Collections.BitArray with a Get(int index) method What is faster? In what situations for the .NET projects BitArray could be more useful than a simple conjunction with the bitwise shift? 回答1: BitArray is going to be able to handle an arbitrary number of boolean values, whereas a byte will hold only 8, int only 32, etc. This is going to be the biggest difference between the two. Also, BitArray implements IEnumerable ,

Reason for the existence of non-short-circuit logical operators

自作多情 提交于 2019-11-26 22:13:19
When used with boolean operands, & and | become logical operators per Section 15.22.2 of the JLS . Unlike && and || , however, these don't short-circuit; they always evaluate both sides. I have a silly question: Why are the less-efficient non-short-circuit logical operators ( & , | ) still there, when we have the more-efficient short-circuit logical operators ( && , || )? I mean, what is the real usage of the non-short-circuit logical operators, as opposed to with the short-circuit logical operators? In other words, what is the usage of always evaluating both sides by using the non-short

PHP: 'or' statement on instruction fail: how to throw a new exception?

。_饼干妹妹 提交于 2019-11-26 21:10:01
问题 Everyone here should know the 'or' statemens, usually glued to an die() command: $foo = bar() or die('Error: bar function return false.'); The most of the times we see something like: mysql_query('SELECT ...') or die('Error in during the query'); However, i cant understand how exactly that 'or' statement works. I would like to throw a new exception instead of die(), but: try{ $foo = bar() or throw new Exception('We have a problem here'); Doesnt work, and neither $foo = bar() or function(){