Page 1 of 3
Pow '^' operator
Posted: Sun Apr 19, 2015 9:41 am
by applePi
is there any hope to replace Pow(5,11) with 5^11 ?? at least using external substitution program
in fact i sometimes stumbled over situations like this:
y = (0.4^2-(0.6-(x^2+y^2)^0.5)^2)^0.5
such as the torus function:
http://www.benjoffe.com/code/tools/functions3d/examples
i feel tired when trying to replace the ^ with Pow or Sqr, it will be very complex most of the time since '^' are essential and recur too much in all literature, the '^' is available in quickbasic and all other basics and even in
http://www.wolframalpha.com/ try Pow[2,3] it will give you 8, try again with 2^3 and the result is also 8
in Python we have **, ...
in perl try: print 2**3; and the result is 8
the Pow is a C specific
Re: Pow '^' operator
Posted: Sun Apr 19, 2015 11:16 am
by TI-994A
applePi wrote:is there any hope to replace Pow(5,11) with 5^11 ??
Hi applePi. What you need could only be achieved through the implementation of a new operator, and I believe that this can only be done by the dev team. However, you could look into creating a math parser and evaluator, which would take an entire expression, parse and calculate it, and return the results. The syntax would look something like this:
Code: Select all
y = math("(0.4^2-(0.6-(x^2+y^2)^0.5)^2)^0.5") ;assuming the variables are globals
There are quite a number of old Visual Basic examples of such functions on the net, which could easily be adapted to PureBasic.
Just a suggestion.

Re: Pow '^' operator
Posted: Sun Apr 19, 2015 11:25 am
by Dude
TI-994A wrote:you could look into creating a math parser and evaluator
No need to reinvent the wheel:
http://www.purebasic.fr/english/viewtop ... 12&t=15762
Re: Pow '^' operator
Posted: Sun Apr 19, 2015 11:42 am
by TI-994A
Well, there you go. Seems to work pretty well too.
Thanks Dude!
Re: Pow '^' operator
Posted: Sun Apr 19, 2015 12:28 pm
by applePi
Thanks for the suggestions, i also have found a solution, is to use
http://sourceforge.net/projects/bcx-basic/ . and after the install, make a test.bas file contains as an example y = (0.4^2-(0.6-(x^2+y^2)^0.5)^2)^0.5 then save it to its Bin folder and from command prompt write bc test.bas , it will produce test.c which is
y=pow((pow(.4,2)-pow((.6-pow((pow(x,2)+pow(y,2)),.5)),2)),.5);
then we use it in purebasic after replacing .5 to 0.5
seems usable
Re: Pow '^' operator
Posted: Sun Apr 19, 2015 3:47 pm
by blueb
Re: Pow '^' operator
Posted: Sun Apr 19, 2015 11:16 pm
by Dude
applePi wrote:from command prompt write bc test.bas
Why on earth would you use a third-party solution when you can do it natively with PureBasic, as BlueB and I showed?
Re: Pow '^' operator
Posted: Sun Apr 19, 2015 11:59 pm
by Danilo
Dude wrote:applePi wrote:from command prompt write bc test.bas
Why on earth would you use a third-party solution when you can do it natively with PureBasic, as BlueB and I showed?
applePi did ask for a expression converter that turns
Code: Select all
y = (0.4^2-(0.6-(x^2+y^2)^0.5)^2)^0.5
into
Code: Select all
y = pow((pow(0.4,2)-pow((0.6-pow((pow(x,2)+pow(y,2)),0.5)),2)),0.5)
Adding operator ^ to PB is a feature request and should be asked for in the "Feature Requests and Wishlists" section of this forum.
Re: Pow '^' operator
Posted: Mon Apr 20, 2015 11:22 am
by Dude
Yes, he asked for a converter, which was the PureBasic code we linked to. It does the job, but he's going to use a non-PureBasic third-party solution instead to get the same results? I think it's better to keep it all as one single source code with one executable, rather than calling an external app to get the results. Just my 2 cents, anyway.
Re: Pow '^' operator
Posted: Mon Apr 20, 2015 1:51 pm
by applePi
i seek the easy things Dude, i don't want to use converters/parsers which will complicate things.
i don't want to know what is the parser is. no time, too much reading .
and regarding the basic to C, i never used it until yesterday as if the collective global mind helps somehow. googling for basic to C translator he gives me BaCon:
http://www.basic-converter.org but it is for Unix
the second result is BCX Basic to C translator, so looking inside bin i found BC.exe so i supposed it is basic to C and it is done easily. the meaning is that '^' are too natural and available in all documentation and not Pow which are used in c/c++ which are the programming languages in which the programmers make Higher level Languages which usually does not have Pow except wolframalpha which allow Pow or '^'. now since i have found the easy to use bc.exe there will be no problem when confronting a maze like '^' expression with many parenthesis. it is not bad to use easy to use basic to C translator to serve purebasic as a utility. using parsers makes the whole program complex and big but if that parser is inside the PB.exe/ pb.dll no problem.
i respect the developers choice anyway.
Re: Pow '^' operator
Posted: Mon Apr 20, 2015 2:54 pm
by c4s
+1 for the feature request of adding a power operator such as "**" or "^".
Re: Pow '^' operator
Posted: Mon Apr 20, 2015 4:20 pm
by Little John
c4s wrote:+1 for the feature request of adding a power operator such as "**" or "^".
I'm neither for this request nor against it, but I've got an associated request:
If such an operater will be implemented in PB, then it should work mathematically correct!
Unfortunately, I've often seen in expression evaluators that something like
4^3^2 is
evaluated from left to right, which is mathematically
wrong:
(4^3)^2 = 64^2 =
4096
Correctly, multiple power operators without any brackets are
evaluated from right to left:
4^3^2 = 4^(3^2) = 4^9 =
262144
Re: Pow '^' operator
Posted: Mon Apr 20, 2015 5:17 pm
by applePi
Thanks Little John for the notes, the bc.exe have translated:
a=4^3^2 wrongly to:
a=pow(pow(4,3),2);
while translated
a=4^(3^2) correctly to:
a=pow(4,(pow(3,2)));
but wolframalpha have deciphered 4^3^2 like you said from right to left with the result 262144, he positions 3 over 4 and 2 over 3

Re: Pow '^' operator
Posted: Mon Apr 20, 2015 6:43 pm
by jack
I am not sure about that
Mathematica 4^3^2 -> 262144
Maple 4^3^2 -> Error, power is non-associative
Wikipedia says the exponential operator is right associative
http://en.wikipedia.org/wiki/Operator_associativity
Maxima 4^3^2 -> 262144
Axiom 4^3^2 -> 262144
Reduce 4^3^2 -> 4096
so who's right?
Re: Pow '^' operator
Posted: Mon Apr 20, 2015 7:06 pm
by skywalk
vb6 wrote:debug.Print 4^3^2
4096