Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Mathematical Precedences

Author
Message
PowerSoft
20
Years of Service
User Offline
Joined: 10th Oct 2004
Location: United Kingdom
Posted: 25th Feb 2007 16:24 Edited at: 25th Feb 2007 16:43
Hi there,

I'm not a maths newb so don't try flaming me, please (i am doing FMaths)

Anyway, I'm coding an expression parsing and it's going pretty sweetly. It includes all the basic binary functions such as * / + - ^, has support for brackets, allowing you to overwrite the basic BODMAS/PEDMAS order. I also have basic unary functions such as % (modulus/absolute) and factorial (!).

The problem I have is deciding on the 'precedence' of these unary elements and also their 'associativity'.

I'm thinking to assign the same precedence as multiplication for factorial (!) as that is basically a short hand way of writing n*n1*n2 etc. Also to let it be RTL associative as thats how its done, but i'm not sure.
[edit]on the topic of factorials, are negative! = 0?[/edit]

Modulus, should that not be the most prioritised? as %(1+2*4) would be %12 ? Is this one too LTR associative?

[edit2]Also as a side point what about functions? as to what precedence should they be given? Sin, Cos and Tan, for example, highest priority?[/edit2]


Thanks,
any help is greatly appreciated,


regards,
rich

The Innuendo's, 4 Piece Indie Rock Band
http://theinnuendos.tk:::http://myspace.com/theinnuendosrock
David T
Retired Moderator
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 25th Feb 2007 16:52
If you think about it sine / cosine / tangent / modulus etc are all just numbers once computed. They should be given the same precedence as a number or a variable, the only difference being that their value must be computed from some more inputs, rather than fetched straight away.

My tip is to keep your operators to +-/*^ and the unary operations (like - / + / ++ / --) and put the rest as built in functions, like

sin(num)
cos(num)
fac(num)

that way the same parsing code you write for user function calls can be reused for internal function calls and also things like factorial. Makes life simpler.

PowerSoft
20
Years of Service
User Offline
Joined: 10th Oct 2004
Location: United Kingdom
Posted: 25th Feb 2007 17:19
Ah cheers, that seems a good idea. It would mean that more functions could be simply added. So you would give the functions a 0 precedence? I don't have a precedence set for numeric values so I presume so? however when I want to do "2! * 2" it gives 24, rather than 4. This implies that factorials should have a higher precedence than binary/unary functions...

The Innuendo's, 4 Piece Indie Rock Band
http://theinnuendos.tk:::http://myspace.com/theinnuendosrock
Kentaree
22
Years of Service
User Offline
Joined: 5th Oct 2002
Location: Clonmel, Ireland
Posted: 25th Feb 2007 17:38
Functions themselves have the highest precedence, even over brackets, because they return the numbers ultimately subbed into the expression, to turn it from a programming expression into a mathematical one.

PowerSoft
20
Years of Service
User Offline
Joined: 10th Oct 2004
Location: United Kingdom
Posted: 25th Feb 2007 17:49
A thank you Ken, together with Dave's suggestion I may be onto a winner,

cheers,
rich

The Innuendo's, 4 Piece Indie Rock Band
http://theinnuendos.tk:::http://myspace.com/theinnuendosrock
Kentaree
22
Years of Service
User Offline
Joined: 5th Oct 2002
Location: Clonmel, Ireland
Posted: 25th Feb 2007 17:52

Login to post a reply

Server time is: 2024-11-18 07:22:47
Your offset time is: 2024-11-18 07:22:47