Pow '^' operator

Just starting out? Need help? Post your questions and find answers here.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Pow '^' operator

Post 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
User avatar
TI-994A
Addict
Addict
Posts: 2698
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Pow '^' operator

Post 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. :)
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Pow '^' operator

Post 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
User avatar
TI-994A
Addict
Addict
Posts: 2698
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Pow '^' operator

Post by TI-994A »

Dude wrote:No need to reinvent the wheel: http://www.purebasic.fr/english/viewtop ... 12&t=15762
Well, there you go. Seems to work pretty well too. :lol:

Thanks Dude!
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Pow '^' operator

Post 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
User avatar
blueb
Addict
Addict
Posts: 1111
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Pow '^' operator

Post by blueb »

- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Pow '^' operator

Post 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?
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Pow '^' operator

Post 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.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Pow '^' operator

Post 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.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Pow '^' operator

Post 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.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Pow '^' operator

Post by c4s »

+1 for the feature request of adding a power operator such as "**" or "^".
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Pow '^' operator

Post 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
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Pow '^' operator

Post 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
Image
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: Pow '^' operator

Post 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?
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Pow '^' operator

Post by skywalk »

vb6 wrote:debug.Print 4^3^2
4096
vs2013 wrote:3^3^3 = 19683
Last edited by skywalk on Mon Apr 20, 2015 7:38 pm, edited 1 time in total.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply