Yes we have been over it twice already although some of us still struggle with the concepts.
Please show a clear case where this is the issue. You do it by the rules, result should be the same. Also, those two meanings are almost identical in practice.You mean other than the entire premise on which this thread is based?
-2^2
...
What I was commenting was that someone started arguing that this is only the case "when '-' means the mathematical operation 'minus'", which is utter nonsense. Minus is always minus.
{x^2 -3xy^3 +2z^2} / {x(y^2 -z)}
x=-1
y=-2
z=1
would become{(-1)^2 - 3(-1)(-2)^3 + 2(1)^2} / {(-1)[(-2)^2 - (1)]}
-1^2 can be interpreted both ways. Either "negative one, squared", or if you follow PEMDAS, -(1^2). This is where the ambiguity stems from. Does the "-" mean "negative one", or is it a simple mathematical operation that should be applied after the exponent. Most compilers would follow the latter, while most human-written equations would imply the former. As you said, this kind of syntax should be avoided at all costs, use parenthesis and it's not a problem.
-1^2 can be interpreted both ways.
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> print -1**2
-1
>>>
You can investigate this with your calculator. Enter -2 onto the display and then press the x2 button. Do you get 4 or -4 as the result?
(Also take note of how you enter -2. I.e. you don't press "-" and then "2", rather you press "2" first and then press the "+/-" key. This illustrates how the sign belongs to the number and is not a subtraction operation.)
4
--
5
-----
6
is different than 4
-----
5
--
6
It cannot possibly be interpreted wrong. You either follow rules of arithmetic or you admit that your expression it is mis-spelled and needs parenthesis because you claim that the minus is a sign, and want it to be stronger than the exponentiation. You cannot use the sign of a number this way.
-1^2 is terrible style, avoid it at all cost. You can spend all day arguing with someone about if it is -(1^2) or (-1)^2 because both have a strong case.
In university math and engineering it is strongly advised to use parenthesis, since the goal is to minimize errors and to communicate proof. It can be assumed that 9 out of 10 times (-1) is what was meant and the author was just lazy, this is very common.
Question is why one would write something like -2^2 in a program at all? That is not efficient and borderline crazy - pre-calculate and then white full number/constant and save some clock cycles.
All this minus ("-") stuff has nothing to do with any compilers or mathematics itself. It has something to do with the users.
Question is why one would write something like -2^2 in a program at all? That is not efficient and borderline crazy - pre-calculate and then white full number/constant and save some clock cycles.You must not do a lot of work with complicated equations and math in programs? Any decent compiler will pull it out, that's not a problem, but clarity is. Writing known constants correctly has great value when using large equations, it makes finding mistakes and making your intent clear without even having to comment it. Boiling complicated equations down as far as possible, while it might make you feel better about yourself, won't make the resulting code any quicker (as long as the compiler is reasonably intelligent), it will just make debugging next to impossible.
All this minus ("-") stuff has nothing to do with any compilers or mathematics itself. It has something to do with the users.The problem is the translation between mathematical concepts and their symbolic representations in our numeral system.
[rambling about how you don't understand definitions]
Question is why one would write something like -2^2 in a program at all? That is not efficient and borderline crazy - pre-calculate and then white full number/constant and save some clock cycles.You must not do a lot of work with complicated equations and math in programs? Any decent compiler will pull it out, that's not a problem, but clarity is. Writing known constants correctly has great value when using large equations, it makes finding mistakes and making your intent clear without even having to comment it. Boiling complicated equations down as far as possible, while it might make you feel better about yourself, won't make the resulting code any quicker (as long as the compiler is reasonably intelligent), it will just make debugging next to impossible.I do write programs including heavy computational stuff, just for you to know.
You know, there is concept called comments in almost every programming language. Expecting every computable program line to be easily read by anyone without a clue is a share lunacy.
There are various ways to deal with the simplification - but that is still a problem for you.
// 3*5*7*9*11*13*15*17 = 34459425
i += 34459425;
i += 3*5*7*9*11*13*15*17;
Expecting every computable program line to be easily read by anyone without a clue is a share lunacy.
The compiler will do the computation before compiling. This is not up for debate - grab the assembly output and look at it. What kind of shitty compiler wouldn't?
This is "share" lunacy:Code: [Select]// 3*5*7*9*11*13*15*17 = 34459425
i += 34459425;
What kind of shitty programmer would write that? The compiler will generate the exact same code for this:Code: [Select]i += 3*5*7*9*11*13*15*17;
QuoteExpecting every computable program line to be easily read by anyone without a clue is a share lunacy.No, it's common freaking sense.
You suggested this poor example. There is nothing to comment really.
And not everything can be written in numbers - you can use various constants and variables (end result would be the same). Variable name can explain number value better than number itself.
Come on, this is math.
You seem to have done a complete 180 since posting this just a few hours before (with which I agree completely):