Recent

Author Topic: Math question  (Read 24788 times)

wp

  • Hero Member
  • *****
  • Posts: 13583
Re: Math question
« Reply #15 on: February 23, 2016, 10:23:26 pm »
Quoted from above wiki article:
Quote
There exist differing conventions concerning the unary operator − (usually read "minus"). In written or printed mathematics, the expression −32 is interpreted to mean 0 − (32) = −9, but in some applications and programming languages, notably Microsoft Office Excel (and other spreadsheet applications) ... , unary operators have a higher priority than binary operators, that is, the unary minus ... has higher precedence than exponentiation, so in those languages −32 will be interpreted as (−3)2 = 9

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Math question
« Reply #16 on: February 23, 2016, 10:28:01 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.
Only inside a formula for example, 12 - 2 ^ 2, then - is an operator because there is a rule in effect that says that the positive numbers can be written with out a sign, in this case if you want to subtract a negative number you have to write 12 - -2 ^ 2 in which case it is customary to include the parenthesis for clarity ee 12 -(-2) ^ 2. -2^2 is negative 2 in the power of 2.
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.
Then a big warning is in order, put it in documentation in bold and it should be fine. I do not use the ** operator my self so its not going to affect me.
Again: that entire discussion would be avoided if people would use parenthesis in such unclear cases.
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.
« Last Edit: February 23, 2016, 10:30:17 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: 13583
Re: Math question
« Reply #17 on: February 23, 2016, 10:44:30 pm »
Then a big warning is in order, put it in documentation in bold and it should be fine. I do not use the ** operator my self so its not going to affect me.
It is not so dramatic, normally you have variables, and the unary negation operator is not involved:
Code: Pascal  [Select][+][-]
  1. var
  2.   x: Double;
  3. begin
  4.   WriteLn(-2.0**2); --> -4
  5.  
  6.   // but:
  7.   x := -2.0;
  8.   WriteLn(x**2);  ---> 4
  9. end;

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Math question
« Reply #18 on: February 23, 2016, 11:05:19 pm »
Then a big warning is in order, put it in documentation in bold and it should be fine. I do not use the ** operator my self so its not going to affect me.
It is not so dramatic, normally you have variables, and the unary negation operator is not involved:
Code: Pascal  [Select][+][-]
  1. var
  2.   x: Double;
  3. begin
  4.   WriteLn(-2.0**2); --> -4
  5.  
  6.   // but:
  7.   x := -2.0;
  8.   WriteLn(x**2);  ---> 4
  9. end;
This shows the bug fairly well yes. Since it is against the math rules it is a big deal I'm sorry to say that but a big warning is in order.
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: 13583
Re: Math question
« Reply #19 on: February 24, 2016, 12:18:37 am »
I got the feeling that you don't understand me. Once more before I leave this topic: This is not a bug. fpc's implementation is in agreement with the usual mathematical definition - see also this reference here:

http://mathforum.org/library/drmath/view/53194.html:
Quote
-3^2
The negation operator properly has precedence below the exponential, so that this means -(3^2) = -9 rather than (-3)^2 = 9

bylaardt

  • Sr. Member
  • ****
  • Posts: 310
Re: Math question
« Reply #20 on: February 24, 2016, 12:30:52 am »
I ask about this "collective bug" because the formula must be calculated at running time based on users entry.
FPC is correct, wp is right and sorry tazz, but math is math, and math rules cannot be change by freewill or particular needs/porpouses.
I understand minus/unary concept, and i see no problem about this on a pascal code. I see problem in user entry, who expect a result and retrieves another.
This is no about FPC! is about users and formulas.
math says -2^4 = -4.
Commercial Sheets says -2^4=4 // wrong by classic math rules.
if have no normatization about this, i believe is time to do it.

bylaardt

  • Sr. Member
  • ****
  • Posts: 310
Re: Math question
« Reply #21 on: February 24, 2016, 01:19:34 am »
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.
In this case minus is a operator. Unary must be set after a operator.
with ['/','*'] operations have no differences:-2*x =  (-2)*x = -(2)*x. Minus is a operator, The order in which are multiplied/divided has no bearing on the result sign

[and, or,xor, not] operators have lowest level of priority as minus, then solve minus operation first (becomes unary)
example:
Code: Pascal  [Select][+][-]
  1.  writeln(-1 or 2);
  2.   writeln((-1) or 2);
  3.   writeln(-(1 or 2));
  4.  
  5.   writeln(-1 and 2);
  6.   writeln((-1) and 2);
  7.   writeln(-(1 and 2));
  8. e

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Math question
« Reply #22 on: February 24, 2016, 01:26:50 am »
I got the feeling that you don't understand me. Once more before I leave this topic: This is not a bug. fpc's implementation is in agreement with the usual mathematical definition - see also this reference here:

http://mathforum.org/library/drmath/view/53194.html:
Quote
-3^2
The negation operator properly has precedence below the exponential, so that this means -(3^2) = -9 rather than (-3)^2 = 9
I understand perfectly, but everything you say comes in contrast of what I use the last 30 years in day to day life and everything I've seen outside the computer world. What ever you might think it is the first time I hear of the negation operator it simple does not exists as an operation by it self outside the parsing world. I'll have to investigate farther but until then I'll keep saying that -2^2 = 4 not -4 and -4 is a bug in contrast with what ever any compiler scientist might think they know and this bug must be documentation in a very visible way.

The thing reminds of the change of GB to mean 1000 MB just to accommodate the hard disk manufacturers that lied in their advertising.
I ask about this "collective bug" because the formula must be calculated at running time based on users entry.
FPC is correct, wp is right and sorry tazz, but math is math, and math rules cannot be change by freewill or particular needs/porpouses.
I understand minus/unary concept, and i see no problem about this on a pascal code. I see problem in user entry, who expect a result and retrieves another.
This is no about FPC! is about users and formulas.
math says -2^4 = -4.
Commercial Sheets says -2^4=4 // wrong by classic math rules.
if have no normatization about this, i believe is time to do it.
Sorry you are wrong math says that negative 4 raised in any even power is going to give back a positive number. Normalization made a huge mistake and now passes a bug as prior art instead of correcting it.
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: 13583
Re: Math question
« Reply #23 on: February 24, 2016, 01:32:52 am »
if have no normatization about this, i believe is time to do it.
If I understand your intended "normalization" correctly let me give you a warning: You cannot change Excel, and you cannot change your users who after working with Excel for a long time may heve forgotten the "correct" rules. What you must do is detect the ambiguous or difficult expressions and tell the user: "Hey, this formula is not unique. Please use parenthesis."

bylaardt

  • Sr. Member
  • ****
  • Posts: 310
Re: Math question
« Reply #24 on: February 24, 2016, 01:51:48 am »
Ok.
i'll guide users to use parethesis.

bylaardt

  • Sr. Member
  • ****
  • Posts: 310
Re: Math question
« Reply #25 on: February 24, 2016, 02:05:04 am »
Sorry you are wrong math says that negative 4 raised in any even power is going to give back a positive number. Normalization made a huge mistake and now passes a bug as prior art instead of correcting it.
Ok. you have a point. (-2)^2=4 and you are right about negative numbers.
Now open your mind: isn't about negative numbers, this is about notation
when notation is -2² you are saying -(2²) not (-2)².

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Math question
« Reply #26 on: February 24, 2016, 03:26:25 am »
Hate to repeat what others said, but for statistics. At least around here they teach us in schools very clearly that -2^2 = -4. (-2)^2 = 4, -(2^2) = -4

Or: -2^2 = (-1) * (2^2)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Math question
« Reply #27 on: February 24, 2016, 04:58:22 am »
no that is wrong
Hate to repeat what others said, but for statistics. At least around here they teach us in schools very clearly that -2^2 = -4. (-2)^2 = 4, -(2^2) = -4

Or: -2^2 = (-1) * (2^2)
no that is wrong -2^2 = (-1*2)^2
Sorry you are wrong math says that negative 4 raised in any even power is going to give back a positive number. Normalization made a huge mistake and now passes a bug as prior art instead of correcting it.
Ok. you have a point. (-2)^2=4 and you are right about negative numbers.
Now open your mind: isn't about negative numbers, this is about notation
when notation is -2² you are saying -(2²) not (-2)².
My mind is as open as it can be -2 is a single number it can not be divided in to two that way -2² is a single step I can open mind as wide as the universe but this is not going to change. From a parser's point of view you might be right from mathematical point of view there is no such thing.

And I'm out my view on the subject is here for everyone to read I have nothing else to add.
« Last Edit: February 24, 2016, 04:59:58 am 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

Thaddy

  • Hero Member
  • *****
  • Posts: 19273
  • Glad to be alive.
Re: Math question
« Reply #28 on: February 24, 2016, 10:12:04 am »
Sorry you are wrong math says that negative 4 raised in any even power is going to give back a positive number. Normalization made a huge mistake and now passes a bug as prior art instead of correcting it.
Ok. you have a point. (-2)^2=4 and you are right about negative numbers.
Now open your mind: isn't about negative numbers, this is about notation
when notation is -2² you are saying -(2²) not (-2)².

Sigh.
Powers are positive (always)
They can be made negative by a signed multiplication.
(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.

This goes only for the natural range. Otherwise negative powers are possible. f.e. Euler e**(Pi i) = -1
[edit]
See: https://en.wikipedia.org/wiki/Euler's_identity
Pascal parsing satisfies that.
« Last Edit: February 24, 2016, 10:22:12 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

wp

  • Hero Member
  • *****
  • Posts: 13583
Re: Math question
« Reply #29 on: February 24, 2016, 10:21:57 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).

 

TinyPortal © 2005-2018