Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i know that it has to do something with compilers converting the infix expressions to postfix or prefix (i don't know which one exactly) and i think compilers do that because in postfix and prefix expressions parenthesis are not needed to emphasize on the precedence of an operator.
so can anyone tell me why and how exactly computer evaluate mathematical expressions?
is the process the same for all the programming languages compilers?
Usually it's to postfix notation, using operand and operator stacks. Any first year computer science book (compiler design) will discuss the details. It has to do with parentheses encountered, and relative precedence (and associativity) of operators encountered in the input. Most computer languages have similar evaluation, precedence, and associativity rules, and will use a similar process. But not all!
Related
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 6 years ago.
Improve this question
Out of curiosity, why the bitwise operator ~ is the only non-logical operator that does not have an assignment version in C++, i.e. ~=? All relational and bitwise operators have an assignment version, but ~= is missing. Is there a particular reason for that?
For all the operators in C++, see for instance: http://www.tutorialspoint.com/cplusplus/cpp_operators.htm
All operators from which compound assignments are made are binary. Tilde, on the other hand, is unary, so there is no easy way to make a compound assignment from it, because there is nothing to put on the right-hand side.
Other unary operands, such as unary minus and logical NOT ! operator, also do not have compound assignments.
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 6 years ago.
Improve this question
short circuit evaluation can shorten the compile time, so i learned that C, C++ is using that way. But are there any situations that short-circuit evaluation ruining the code?
Short circuiting does not shorten the compile time of the code. (by any meaningful amounts, at least)
It might be shortening the runtime, but it's not its intended purpose.
The purpose of short circuiting is to do the minimal amount of work in order to check a certain condition.
For example:
When using && (as opposed to a single &), the right operand won't be evaluated if the left one is false. This is due to the nature of a logical and operation: if at least one of the operands is false, the whole expression is false.
Technically, it will shorten the runtime if the condition fails early, but the amount of saved runtime is dependent on the expressions inside each operand.
Anyway, it's incorrect to use && because it's "faster" than &. You should use either when appropriate.
& is used for bitwise operations.
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 7 years ago.
Improve this question
What's standing in the way of new operators added to the C/C++ standard, such as <<< and >>> for bitwise left and right rotate operations? Is there a reason why the standards organizations can't include such operators in a future spec?
I know there are other ways to accomplish this:
& >> | << combo
Intrinsic functions
Inline assembly
The problems with these workarounds are:
Performance
Readability
Potentially more complex code generation
Platform/compiler dependence
To me, this seems like an inconsistency. If they are already making the effort to provide simple, readable, high-performance operators for & | ^ << and >>, bitwise rotate operators shouldn't be any more difficult to support for 8-, 16-, 32-, and 64-bit signed/unsigned values.
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 8 years ago.
Improve this question
I see, C++ compilers support && and its textual equivalent and operator (Similar to || operator to or). I tend to like its textual form compare to special character form. But, recently I am discouraged from using textual form of such operator without any concrete reasoning. I don't seriously think, textual form will require more typing compare to special character form. Also, there won't be much on compiler overhead on parsing part. So why major C++ community tend towards && instead and. While I think, textual form is much clear while stating a condition to beginner. It also promote more poetry style coding rather than bombarding code with gibberish character.
Just my personal preference here; visually the && breaks up two identifiers better than and does. So at a glance it is a bit quicker to mentally parse the expression.
This is kind of a standard in most of the major languages.. You got to accept it as syntax and should not argue about how it should be. Language isn't designed according to individual liking or disliking. You're going to find many somewhat confusing things in C/C++ which were taken care of in modern languages like python etc. But C, Java are still most widely used languages so you must follow their syntax if you wish to program in these languages.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I've written an application that converts a string into a mathematical expression for evaluation. This is done by converting the string into postfix and then by constructing an expression tree and solving it.
Now I want to know though, what is the most efficient way to do conversion into postfix?
Sample expression -
(2+(3*4+(4/(3*(4+6))))) or (3+4) or 3+4
I would suggest you consult Sedgewick's Algorithms, 4th ed. The code from the book for converting arithmetic expressions into postfix form is available from the website.
I suppose this question is about the algorithm, BUT - If I would have to something like that I would use something like BOOST::Python to just exec the string as python code and get the result. I like to avoid writing code, If I can..