Recent

Author Topic: Math question  (Read 24783 times)

wp

  • Hero Member
  • *****
  • Posts: 13552
Re: Math question
« Reply #45 on: February 25, 2016, 09:48:42 pm »
We're not talking about the C XOR operator here.

MSABC

  • New Member
  • *
  • Posts: 43
Re: Math question
« Reply #46 on: February 25, 2016, 09:55:28 pm »
I'm sorry  :'(

Martin

wp

  • Hero Member
  • *****
  • Posts: 13552
Re: Math question
« Reply #47 on: February 25, 2016, 09:59:21 pm »
I didn't want to disappoint you. It was a good joke that I only realised after your second post.

bylaardt

  • Sr. Member
  • ****
  • Posts: 310
Re: Math question
« Reply #48 on: February 26, 2016, 05:20:23 am »
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.
"colective bug" catch up you too?

minus is a operator, not the unary in this case.
remember math commutative property
0-2^2 = -2^2+0


algebraic solution:
x-2^2=0
x=4

-2^2=-x
-2^2=-4
or
4=2^2
« Last Edit: February 26, 2016, 06:15:36 am by bylaardt »

avra

  • Hero Member
  • *****
  • Posts: 2590
    • Additional info
Re: Math question
« Reply #49 on: February 26, 2016, 12:08:34 pm »
-2² = ?

Is it 4 or -4?

Let's see...

https://en.wikipedia.org/wiki/Order_of_operations:
Quote
In written or printed mathematics, the expression −3² is interpreted to mean 0 − (3²) = −9

This convention is learned on math classes. Square is a function y=f(x). Negation is also a function. So we can say Square(x) = x*x and Negation(x) = -x.

In math we can replace x=-2² with x=-y where y=2². Following this math convention all major programming languages also calculate x=-2² as x=Negation(Square(x)), which is correct and convinient for both writing and calculating more complex functions. Therefore FPC, expression evaluators and parsers should follow this expected behavior and not something not expected as in Excel We all know Microsoft wants to make their own rules, but if one has big muscles it doesn't mean he is always right. Only spreadsheet related stuff will probably do it wrong but that is just to be compatible with Microsoft who still doesn't want to admit he's wrong (if you don't count correct math like parsing in their programming languages as admitting).
« Last Edit: February 26, 2016, 12:29:36 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Math question
« Reply #50 on: February 26, 2016, 01:06:47 pm »
"colective bug" catch up you too?
It's a documented behavior, so not a bug, at least in FPC. But many other languages (and their respective implementations) define the same rule. One exception is Smalltalk, which defines no precedence at all.
minus is a operator, not the unary in this case.
remember math commutative property
0-2^2 = -2^2+0


algebraic solution:
x-2^2=0
x=4

-2^2=-x
-2^2=-4
or
4=2^2
Whatever, but changing the behavior is not desirable (at least) for me.

VTwin

  • Hero Member
  • *****
  • Posts: 1227
  • Former Turbo Pascal 3 user
Re: Math question
« Reply #51 on: February 26, 2016, 04:03:49 pm »
-2^2=

MATLAB -4
GNU Octave -4
R -4
Mathematica -4

Excel 4

Cheers,
VTwin
« Last Edit: February 26, 2016, 04:05:41 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 15.3.2: Lazarus 3.8 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.8 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.8 (64 bit on VBox)

ArtLogi

  • Full Member
  • ***
  • Posts: 194
Re: Math question
« Reply #52 on: February 26, 2016, 04:33:15 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.
I have encountered both depending on the teacher / lecturer IIRC. Might be one of those math quirks around, but I don't have competence to say if it is.

-2^2 = 4
-(2)^2 = -4 <- This is for some teachers as above expression.
(-2)^2 = 4
-(2^2) = -4
1-2^2 = -3
1-(-2^2) = -3
1-+2^2 = -3

All of them I have seen in if memory serves .. They say math is exact, maybe .. but the teachers are not.  >:(

PS. This post is more or less oftopic, sry.
« Last Edit: February 26, 2016, 04:50:46 pm by ArtLogi »
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

bylaardt

  • Sr. Member
  • ****
  • Posts: 310
Re: Math question
« Reply #53 on: February 26, 2016, 08:18:45 pm »
It's a documented behavior, so not a bug, at least in FPC. But many other languages (and their respective implementations) define the same rule. One exception is Smalltalk, which defines no precedence at all.

FPC is Ok so as the documentation. This behavior occurs just in symbolic unit.
math unit send the correct result.
Code: [Select]
-2**2 // returns -4
Marcov choose this criteria (excel,not math) for symbolic unit and it is fine to me. My indignation was the sugestions to report a bug in FPC.

Whatever, but changing the behavior is not desirable (at least) for me.
i agree with you about no changes needed.
i see no reason to change the behavior or change the documents. Except advise symbolic have a behavior like excel, not fpc.

VTwin

  • Hero Member
  • *****
  • Posts: 1227
  • Former Turbo Pascal 3 user
Re: Math question
« Reply #54 on: February 27, 2016, 04:18:13 pm »
Marcov choose this criteria (excel,not math) for symbolic unit and it is fine to me. My indignation was the sugestions to report a bug in FPC.

I agree. No complaints here. Excel gives a different result than math programs, so is unexpected to me, but Excel compatibility may be desired.

Cheers
VTwin
« Last Edit: February 27, 2016, 05:00:21 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 15.3.2: Lazarus 3.8 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.8 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.8 (64 bit on VBox)

 

TinyPortal © 2005-2018