Recent

Author Topic: Math question  (Read 24786 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 19273
  • Glad to be alive.
Re: Math question
« Reply #30 on: February 24, 2016, 10:23:17 am »
This seems to be an endless discussion, everybody's convinced that his position is correct. What about both calculations being correct? I mean: Can there be two ways this rare kind of math is calculated? In my country, we learned: calculate the square first, then do the negation. Taazz and Marco, in your country, maybe you learned: the sign is part of the number, i.e. do the negation first, and then calculate the square.

If this is true an expression like -2^2 should raise an exception because its result is not unique, it depends on the country where it is calculated.

The problem is how to detect this (and other) ambiguous case(es).

It is not ambiguous. It is just operator precedence. (But I edited my post)
objects are fine constructs. You can even initialize them with constructors.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12905
  • FPC developer.
Re: Math question
« Reply #31 on: February 24, 2016, 11:25:43 am »
(Dutch:"Meneer Van Daalen Wacht Op Antwoord") has no space for operator precedence of signed Powers, but a signed Power is an expression of a Power multiplied with a negative value of -1. And that is explained above many many times.

As said you CAN see it that way, but you CAN also see it as part of the number.

Personally I don't care. I'll commit a patch that makes it configurable with some tests when it comes. WAAAYP.

bylaardt

  • Sr. Member
  • ****
  • Posts: 310
Re: Math question
« Reply #32 on: February 24, 2016, 08:32:56 pm »
@User137, Thaddy and wp: Thanks. You reforce me to aply math rules, and guide users to  use the "less doubts and more parenthesis" mode on.
@taazz: You are my inspiration to go on with normalization and documentation for advanced users, because some users are excel users too and have the same concept as you about formulas. Thanks for your share.
@ marcov: Thanks for your concern, but no patches needed. is a criteria issue, not a bug. BTW, symbolic is a great inspiration to rebuild my parser in a rpn mode. when i finish it, i share the code under brasilian public domain licence.

MathMan

  • Hero Member
  • *****
  • Posts: 518
Re: Math question
« Reply #33 on: February 25, 2016, 05:43:46 pm »
...

There is no situation or discussion here, find any mathematician and ask him, actually I think we have a couple of mathematicians visiting from time to time it would be nice to hear from them.

OK - my two cents on the matter as a mathematician. The way I was taught math (in school and university) is that for a mathematician there is no unary operator "-" - that is -2^2 for a mathematician is equivalent to 0-2^2. That is why we also define operator precedence only on exponentiation / multiplication and division / addition and subtraction. If you want to talk about unary operators then the mathematical perspective is that the unary operator is applied to the fully qualified operand (for whose evaluation again operator precedence is applied). In both cases -2^2 is seen as -4 (which was also the reading I applied when I started looking into this thread).

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Math question
« Reply #34 on: February 25, 2016, 07:48:27 pm »
i cannot believe my eyes!!!! some of the texts i read in thisthread are complete non sense!!! every negative number raised to a even power is positive. raised on an odd power the result is nevative. full stop!  period! -2 ^ 2 is 4
 -2^2 = (0-2)  x (0-2) = 4

-(2^2)=
-1x2^2=
(0-1) * (0-2) *(0-2) = 2 * (0-2) = -4
« Last Edit: February 25, 2016, 07:54:24 pm by Blestan »
Speak postscript or die!
Translate to pdf and live!

Septe

  • Jr. Member
  • **
  • Posts: 68
Re: Math question
« Reply #35 on: February 25, 2016, 08:09:37 pm »
I'm looking at it this way.  I know the behavior of how the formula parses out.  The fact that it's correct math or not is besides the point.  Marcov pointed out that when he can, he'll put in an option to switch how it parses out (at least, that's how I read it).  Personally, it's an implementation of how a computer can read formulas and by knowing how it is read, you, the programmer, can adjust it so it reads how it's supposed to read.

     a)  -2^2 = 4 since the unary takes highest precedence
     b)  -(2^2) = -4 do this if you wanted it to be -4

Thing is to be consistent.  Putting in a switch just adds another spot where confusion can occur but I'll live with it if Marcov puts it in.

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Math question
« Reply #36 on: February 25, 2016, 08:13:28 pm »
most of the calculators and parsers read -2^2  as (-1) * (2*2) witch is -4 ... do it this way to be compatible ... to have 4 one must write it (-2)^2 ....and it wil be parsed and calculated 4

in 99.99% case the minus will be applied after the power  dont go with variations ... just follow the masses :)
« Last Edit: February 25, 2016, 08:16:17 pm by Blestan »
Speak postscript or die!
Translate to pdf and live!

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Math question
« Reply #37 on: February 25, 2016, 08:19:28 pm »
It seems to be a documented behavioral difference. I would prefer what has been established in the programming world for all this time, though. It's actually documented that unary operator has the highest precedence and unary minus is given as example, but missing from the table.

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Math question
« Reply #38 on: February 25, 2016, 08:50:23 pm »
after a little reading my conclusion is that you have 2 options
follow ms excel where -2^2 = 4
or follow google and texas instruments where -2^2 = -4

the agony of choice  >:D
Speak postscript or die!
Translate to pdf and live!

wp

  • Hero Member
  • *****
  • Posts: 13583
Re: Math question
« Reply #39 on: February 25, 2016, 08:57:36 pm »
Exactly, and therefore such an expression must be forbidden and raise an exception, the user must set brackets to show what he means.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12905
  • FPC developer.
Re: Math question
« Reply #40 on: February 25, 2016, 08:58:15 pm »
It's actually documented that unary operator has the highest precedence and unary minus is given as example, but missing from the table.

Best is to report documentation errors immediately: http://bugs.freepascal.org/view.php?id=29728

A few people keeping that up for a while really matters in the span of a few years.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Math question
« Reply #41 on: February 25, 2016, 09:02:07 pm »
Ask the calculator, or ask http://www.wolframalpha.com/input/?i=-2%5E2

Windows calculator interpret it as 0 - 2 ^ 2. In all cases it's -4.

Ask scientific calculator: http://web2.0calc.com/

or.. ask Google: https://www.google.fi/webhp?sourceid=chrome-instant&ion=1&espv=2&es_th=1&ie=UTF-8#q=-2%5E2

MSABC

  • New Member
  • *
  • Posts: 43
Re: Math question
« Reply #42 on: February 25, 2016, 09:22:09 pm »
here is another funny one:

#include <stdio.h>

int Result,val, mval;

main()
{

val= 2;
mval=-val;
Result = mval^val;
printf(" %d^%d = %d\n", mval, val,Result);


val= 3;
mval=-val;
Result = mval^val;
printf(" %d^%d = %d\n", mval, val,Result);
}


leads to:
 -2^2 = -4
 -3^3 = -2

The first result really(!) suprised me, but is correct as the second one (in this context)  ;D

regards Martin

wp

  • Hero Member
  • *****
  • Posts: 13583
Re: Math question
« Reply #43 on: February 25, 2016, 09:28:14 pm »
Isn't this a typo? The second one should be -27.

MSABC

  • New Member
  • *
  • Posts: 43
Re: Math question
« Reply #44 on: February 25, 2016, 09:33:55 pm »
no -3^3 = -2 is correct (in this context)

But i don't want to spoil the party by revealing too soon.
Maybe some people like to find out by themselfes.
( As I mentioned : -2^2=-4 really(!) suprised me, I had to check this in detail)

best regards

Martin

 

TinyPortal © 2005-2018