Question:
Converting algebraic expressions into a form of programming language?
?
2010-05-10 00:59:00 UTC
The expression is written in the form of algebraic expressions... Write it in form of programming language instructions, any programming language...

Here is the expression:
http://www.itopendays.com/2010/wp-content/includes/images/prijava/2-4.png

Thank you very much!
Three answers:
anonymous
2010-05-10 01:05:10 UTC
in php

$f = (2*(pow(($p/$q),($k-1))) / (pow(($r-(3*$t)),(1/$m)))
Tizio 008
2010-05-10 08:47:20 UTC
what's the hard part?!

just "convert" each operator in the form used by the language; e.g. multiplication is often *, division (fractions are divisions) with / .. and so on...

the other thing to care of is the operations order; follows the rules of math and "combine" them with that of the language in use, tipically they "imitate" the math ones.



so: look at the expression: there's a big fraction between a numerator N and denominator D, so first your expression is



f = (N) / (D);



we've written () to be sure of the right order of computation even when we'll "expand" N and D.

now let's do it, let's expand N first



N is 2 * (p/q)^(k-1)

this is straightforward for languages that have ^ (others use **, others pow .......)

it works since operator precedence is what we expect in most languages. in case it is not, we should add a () like



2 * ( (p/q)^(k-1) )



this (or prev one) is N and we can replace it in our "model" f = (N) / (D); the () around k-1 needed since we must delimit the exp, or the interpretation would be

A^k - 1 (the exp being k alone). without () around p/q, it would be read as

p/(q^(k-1)), i.e. a fraction where numerator is p, denominator is q^(k-1), which is wrong for us



D (denominator) is similarly "created" following the math expression



(r - 3*t)^(1/m)

and here too there's no surprise. replace instead of D and you've your f.



note: many langs when dealing with floating point costants prefer it is explicitly given; so numbers like 3 could become 3.0 more correctly; others may "cast" intergers to floating point "automatcally" but it depends on the type of vars like m or t... e.g. if m is integer, a language could compute 1/m as integer division, and likely it yields 0 (for m>1) ... if we say 1.0 instead, we force a floating point division, which is likely what we wanted.



conventions may vary a bit according to the language, but basically many shares the same ideas.
Chaos
2010-05-10 08:04:24 UTC
f = 2* ( pow ( (p/q), k-1 ) ) / ( pow ( (r-3*t), (1/m) ) );



I wrote the above code in C++. U would need to include the math header file to use pow, and u will need to define the variables 1st. but the end expression would be that. The semicolon at the end is to signify the end of the line.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...