Recent

Author Topic: Math question  (Read 21999 times)

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Math question
« on: February 23, 2016, 07:18:23 pm »
-2² = ?
I know! obvious "-4", but :
Code: Pascal  [Select][+][-]
  1. Symbolic.QuickEvaluate('-2^2',[],[]))
returns 4
Other expressions parsers based on rpn conversion, return "4" too.
spreedshets like Libreoffice, GoogleSheets (https://www.google.com/sheets/about/) retur "4"

is this a collective bug or a expected behavior?

PS: 0-2^2 returns "-4" as expected.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Math question
« Reply #1 on: February 23, 2016, 07:29:29 pm »
It is a question of interpretation.
Does -2² mean (-2)*(-2) or -(2²)?

Mathematician's conventions may differ from programmers or writers of scripting languages.
« Last Edit: February 23, 2016, 07:32:20 pm by howardpc »

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Math question
« Reply #2 on: February 23, 2016, 07:33:06 pm »
See the section on "Exceptions" in https://en.wikipedia.org/wiki/Order_of_operations

Use parenthesis for clarification: (-2)^2 = 4  or -(2^2) = -4
« Last Edit: February 23, 2016, 07:36:16 pm by wp »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Math question
« Reply #3 on: February 23, 2016, 07:34:05 pm »
-2² = ?
is this a collective bug or a expected behavior?
Any negative multiple by a negative number it will yield a positive number. It is basic multiplication theory http://www.mathsisfun.com/multiplying-negatives.html
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Math question
« Reply #4 on: February 23, 2016, 07:35:24 pm »
It is a question of interpretation.
Does -2² mean (-2)*(-2) or -(2²)?

Mathematician's conventions may differ from programmers or writers of scripting languages.
no its not, if there is no parenthesis specified then your first interpretation is implied.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Math question
« Reply #5 on: February 23, 2016, 07:52:38 pm »
See the section on "Exceptions" in https://en.wikipedia.org/wiki/Order_of_operations

Use parenthesis for clarification: (-2)^2 = 4  or -(2^2) = -4
I expected -power(2,2) = -4 without patenthesis.

"0-2^2" is different from "-2^2"? why? for me is totally no sense! 

@tazz  (-2)^2 is diferent from -(2)^2. implicit parenthesis needs criteria too.
@howardpc "conventions" and "differ" are not compatible! math and programmers can be.

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Math question
« Reply #6 on: February 23, 2016, 08:03:04 pm »
The problem is that the "-" character has two meanings: unary operator (creates negative numbers) or binary operator (subtraction). The unary operator has the highest priority, i.e. -2^2 is seen by the formula parser as (-2)^2

The unary operator must have a higher priority than a binary operator. Otherwise an expression like -2+2 would be interpreted as -(2+2) = -4. And that's even worse...

The steps in parsing a formula like -2^2 are:

- first token "-" --> unary operator
- 2nd token "2"
- 3rd token "^" --> binary operation (^) --> we know the first operand which is "-2"
- 4th token "2"
- no more characters --> the second operand is "2" --> we calculate the binary operation (-2)^2 = 4.

If the formula were 0-2^2 then the steps are

- first token "0"
- second token "-" --> this is a binary operator now!; first operand is 0
- third token "2"
- forth token "^" --> binary operator, higher priority than the 2nd token; first operand is 2
- fifth token "2" --> second operand 2
- calculate power: 2^2 = 4 --> second operand of subraction
- calculate subtraction: 0 - 4 = -4

This way of thinking is as logical as yours. The only way to overcome this ambiguity is to used parenthesis.
« Last Edit: February 23, 2016, 08:21:11 pm by wp »

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Math question
« Reply #7 on: February 23, 2016, 08:18:56 pm »
so? is a rpn convenience, not a normalization.

Thanks wp, tazz and howardpc.

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Math question
« Reply #8 on: February 23, 2016, 08:30:45 pm »
Interestingly, the operator ** of fpc gets the "humanly" correct result:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   ShowMessage(IntToStr(-2**2));  // --> "-4"
  4. end;
I don't know the implementation used but I guess fpc prepends the expression with "0+" which converts the unary minus operator to a binary operator.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Math question
« Reply #9 on: February 23, 2016, 08:53:23 pm »
@tazz  (-2)^2 is diferent from -(2)^2. implicit parenthesis needs criteria too.
-2^2 = (-2)^2 no ifs no buts. If an interpreter gets this wrong then its a bug.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Math question
« Reply #10 on: February 23, 2016, 08:58:10 pm »
Note that the precedence levels are fixated in the tables at the top of parsexpr.inc. Changing them is fairly easy, but requires recompile.


pminus is the unary one, psub is the binary minus. The code that handles that is near parseexpr.inc:329-331

Code: Pascal  [Select][+][-]
  1.     If Expr[I-1] IN ['+','(','*','/','-','^'] then
  2.           IsMinus:=true;
  3.         If IsMinus then
  4.          OperationNr:=PMinus; // unary
  5.  


IIRC when I originally crafted symbolic (2003) I was aware of this, I went with the more mathematical definition because I saw symbolic mainly as a way to evaluate expressions (somewhat) fast to draw plots of functions that were input by users.
« Last Edit: February 23, 2016, 09:02:44 pm by marcov »

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Math question
« Reply #11 on: February 23, 2016, 09:29:53 pm »
-2^2 = (-2)^2 no ifs no buts. If an interpreter gets this wrong then its a bug.

Then fpc has a bug according to your opinion - see above, or try this:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Math;
  7. begin
  8.   WriteLn(-2**2);  // --> -4
  9.   ReadLn;
  10. end.  

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Math question
« Reply #12 on: February 23, 2016, 09:38:17 pm »
-2^2 = (-2)^2 no ifs no buts. If an interpreter gets this wrong then its a bug.

Then fpc has a bug according to your opinion - see above, or try this:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Math;
  7. begin
  8.   WriteLn(-2**2);  // --> -4
  9.   ReadLn;
  10. end.  
I do not know what ** is never used it my self but if it is a power sign then yes it is a bug. -2 is a number not a subtraction sign. Any mathematician can verify this. It is one of the "rules" of mathematics.
« Last Edit: February 23, 2016, 09:41:15 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Math question
« Reply #13 on: February 23, 2016, 10:04:37 pm »
I do not know what ** is never used it my self but if it is a power sign then yes it is a bug. -2 is a number not a subtraction sign. Any mathematician can verify this. It is one of the "rules" of mathematics.
The usual rules of mathematics that I learned at school say that the expression -2^2 means: the number to be squared is 2, and the result is negated, i.e. we calculate -(2^2) = -4. That's what FPC does.
Excel and the usual formula parsers have a different convention, they put an invisible bracket around -2 and calculate (-2)^2 = 4 (like you do).
From what I learned at school I would say the Excel has a bug, but there are also arguments from the parser point of view for the opposite - that's what I was trying to explain above.

There's no chance to alter neither fpc nor Excel, that would break numerous calculations all over the world.

Again: that entire discussion would be avoided if people would use parenthesis in such unclear cases.

Aaah, I forgot: the ** operator is the same as the ^ in this discussion.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Math question
« Reply #14 on: February 23, 2016, 10:14:36 pm »
The usual rules of mathematics that I learned at school say that the expression -2^2 means: the number to be squared is 2, and the result is negated, i.e. we calculate -(2^2) = -4. That's what FPC does.

Strange, that is not normal in math. The unary sign is usually part of the number. (collection ℤ from German Zahlen), and then the number is squared -> 4.

 

TinyPortal © 2005-2018