BBC BASIC for Windows
Programming >> BBC BASIC language >> EVAL
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1325018510

EVAL
Post by JB91 on Dec 27th, 2011, 7:41pm

Hi,

I'm making a calculator program, and to get the result I am using EVAL. Is there a way of giving EVAL an expression and it won't calculate it using BIDMAS?
Re: EVAL
Post by admin on Dec 27th, 2011, 9:45pm

on Dec 27th, 2011, 7:41pm, JB91 wrote:
Is there a way of giving EVAL an expression and it won't calculate it using BIDMAS?

One way would be to add parentheses in the appropriate places to force left-to-right evaluation. For example if you have the expression 'a + b * c ^ d' you could add brackets as follows: '((a + b) * c) ^ d' which would override the normal operator priority.

If you are building the expression step-by-step (as a calculator often would) this might be easy to arrange - just enclose the expression-so-far in brackets before you append the next term.

Alternatively you could parse the expression yourself and evaluate the individual operators separately rather than getting EVAL to do it. It's not particularly hard to do.

Incidentally some calculators do observe Order of Operations. This Wikipedia article states that Microsoft's calculator evaluates left-to-right in standard view but observes operator precedence in scientific view:

http://en.wikipedia.org/wiki/Order_of_operations

Richard.