expression-evaluation

C++ and PHP vs C# and Java - unequal results

拟墨画扇 提交于 2019-12-30 03:40:08
问题 I found something a little strange in C# and Java. Let's look at this C++ code: #include <iostream> using namespace std; class Simple { public: static int f() { X = X + 10; return 1; } static int X; }; int Simple::X = 0; int main() { Simple::X += Simple::f(); printf("X = %d", Simple::X); return 0; } In a console you will see X = 11 (Look at the result here - IdeOne C++). Now let's look at the same code on C#: class Program { static int x = 0; static int f() { x = x + 10; return 1; } public

C++ and PHP vs C# and Java - unequal results

爱⌒轻易说出口 提交于 2019-12-30 03:40:07
问题 I found something a little strange in C# and Java. Let's look at this C++ code: #include <iostream> using namespace std; class Simple { public: static int f() { X = X + 10; return 1; } static int X; }; int Simple::X = 0; int main() { Simple::X += Simple::f(); printf("X = %d", Simple::X); return 0; } In a console you will see X = 11 (Look at the result here - IdeOne C++). Now let's look at the same code on C#: class Program { static int x = 0; static int f() { x = x + 10; return 1; } public

Convert NSString of a math equation to a value

折月煮酒 提交于 2019-12-29 00:07:06
问题 I would like to know how to evaluate a string representation of an equation as if it were a real equation: if(@"15+14==23") { //True statement... } else { //False statement.... } I want to return "false" because 15+14 does not equal 23. How can I get this to work? 回答1: Here is an example how to do it with NSPredicate : NSPredicate *p = [NSPredicate predicateWithFormat:@"1+2==3"]; NSLog(@"%d", [p evaluateWithObject:nil]); p = [NSPredicate predicateWithFormat:@"1+2==4"]; NSLog(@"%d", [p

Does Java strictfp modifier have any effect on modern CPUs?

独自空忆成欢 提交于 2019-12-28 14:57:31
问题 I know the meaning of the strictfp modifier on methods (and on classes), according to the JLS: JLS 8.4.3.5, strictfp methods: The effect of the strictfp modifier is to make all float or double expressions within the method body be explicitly FP-strict (§15.4). JLS 15.4 FP-strict expressions: Within an FP-strict expression, all intermediate values must be elements of the float value set or the double value set, implying that the results of all FP-strict expressions must be those predicted by

sed replace number with number-1

有些话、适合烂在心里 提交于 2019-12-25 09:12:04
问题 Can sed handle a substitution and simultaneously evaluate an expression to substitute somehow? For example, I have the following entry in a text file: ##################################### topd Tree1 - Tree14 ####################################### For reasons I won't go in to, each Tree's number is N+1 relative to another numbering scheme I've been using so far. Could sed (or any util really, sed is just my go to for find-and-replace operations), find every instance of TreeN and replace them

R: How to go from a vector of strings to a vector of quotes / names?

*爱你&永不变心* 提交于 2019-12-23 21:16:54
问题 How to get from input = c("a", "b", "c") to output = c(quote(a), quote(b), quote(c)) automatically? 回答1: List apply as.symbol() to your input vector. lapply(X = input, as.symbol) 来源: https://stackoverflow.com/questions/50710103/r-how-to-go-from-a-vector-of-strings-to-a-vector-of-quotes-names

Arithmetic Expression Evaluation using Reverse Polish Notation (RPN)

╄→尐↘猪︶ㄣ 提交于 2019-12-22 09:19:08
问题 A mathematical expression is usually expressed in infix notation. For evaluation purposes, we can change it to postfix (reverse polish) notation (using algorithms like Shunting-Yard) and then evaluate the postfix notation using stack. I found out that calculators use this technique, but do today's modern compilers use this for arithmetic expression evaluation? Is it efficient enough or other techniques (or algorithms) are being used? 回答1: To answer this question let's focus on the concepts

updating references in an expression with a nested assignment

坚强是说给别人听的谎言 提交于 2019-12-22 04:15:11
问题 Looking at this example code similar to this question: public class A { public static void main(String args[]){ A a = new A(); System.out.println(a.equals((a = null))); } } This prints false. Why doesn't it fail with a NullPointerException? The assignment has to get processed before the equals method can run, but somehow that doesn't affect the reference that equals is called on until after the whole line is evaluated? I didn't see where in the Java language spec it describes this, did I miss

c++, evaluating expressions with multiple `&&`s and no operator of lower precedence

白昼怎懂夜的黑 提交于 2019-12-13 16:18:37
问题 If an expression evaluates multiple && operators, and does not evaluate any operators of lower precedence (eg. || , ?: ), will the expression evaluate to 0 as soon as one of the && s return 0, or will it finish evaluating the remaining && s? For example, q=0; w=1; e=1; r=1; if(q && w && r && e) {} Will this if() evaluate to false as soon as q && w evaluates to 0 (since the remaining && must all evaluate to 0 regardless of the right hand operators)? 回答1: Yes, evaluation will terminate early (

Writing String evaluation function

你离开我真会死。 提交于 2019-12-12 11:03:56
问题 I'm trying to write a String evaluation function i.e. evaluate("4 + 1") ; // returns 5 evaluate("4 + 1 + 3") ; // returns 8 evaluate("4 + 1 * 3") ; // returns 7 (not 15) The operators are + - / and * My initial though was to use regular expressions to collect operators and digits as these can be matched. And than after finding that info, somehow figure out a way to prioritize /* ove -+ operators. Here is how I started : static String regex = "([\\+\\*-/])+"; static String digitRegex = "(\\d)+