Don't do brain surgery with it but this power# function comes pretty close for roots other than the square ones:

Code:

`' Power pack the short version.txt`

' written for Naalaa 6 by bplus posted 2018-07-23

' extracted from large test of fractions, random number functions, string... in Power test.txt

' The main purpose of this code: to demo a power function for real numbers,

' I had an idea for how real numbers to the power of real numbers might be done ie x# ^ y# = ?

' This means that not only can you take SQR of a number, you can get cube or cube root, quartic, 5th 6th... roots and any multiple

' It came from this simple idea

' 2 ^ 3.5 = 2 ^ 3 * 2 ^ .5 = 8 * Sqr(2)

' 3 ^ 3.5 = 3 ^ 3 * 3 ^ .5 = 27 * Sqr(3)

' so 2 ^ 3.25 = 2 ^ 3 * 2 ^ .25

' what is 2 ^ .25 ? It is sqr(sqr(2)) !

' likewise 2 ^ 3.125 = 2 ^ 3 * 2 ^ 1/8

' what is 2 ^ 1/8 ? It is sqr(sqr(sqr(2))) !

' any decimal can be written as a sum of fraction powers of 2, as any integer can be written in powers of 2.

' in binary expansions

' 1/2 = .1 or .5 base 10

' 1/4 = .01 or .25 base 10

' 1/8 = .001 or .125 base 10

' 1/16 = .0001 or .0625 base 10

' So with binary expansion of decimal, we can SQR and multiply our way to an estimate

' of any real number to the power of another real number using binary expansion of

' the decimal parts as long as we stay in Naalaa integer limits and are mindful of precision.

constant:

wW 800

wH 720

hidden:

set window 100, 20, wW, wH

do

wln "And now for the main event! We test the power#(x#, pow#) function!"

write "(nothing) quits, Please enter a real (float) decimal number raise to some power. x# = "

x# = rln#()

if x# = 0.0 then break

write "(nothing) quits, Please enter a real (float) decimal power. pow# = "

pw# = rln#()

if pw# = 0.0 then break

result# = power#(x#, pw#)

wln result#, " is what we estimate for x# raised to power of pow#"

wln

loop

wln

wln "Far from precise of course, but this code is clear proof of concept!"

wln " OMG, it worked!!!"

wait keydown

' A power function for real numbers (small ones but still!)

' x# to the power of pow#

function power#(x#, pow#)

'this sub needs 2 other subs

'rite$

'bExpand20$

'this is going to follow covertReal2Fraction$, first deal with integer part if any

r$ = str$(pow#)

s$[] = split(r$, ".")

integer$ = s$[0]

build# = 1.0

if integer$ <> "0"

p = int(integer$)

for i = 1 to p

build# = build# * x#

next

endif

'that takes care of integer part,

'now for the fraction part convert decimal to fraction

n$ = s$[1]

ld = len(n$)

while rite$(n$, 1) = "0"

n$ = left$(n$, ld - 1)

ld = len(n$)

wend

denom = 10

for i = 2 to ld

denom = denom * 10

next

numer = int(n$)

'OK for bExpand20$ don't have to simplify and that saves us having to extract n and d again from n/d

bs$ = bExpand20$(numer, denom)

'at moment we haven't taken any sqr of x

runningXSQR# = x#

'run through all the 0's and 1's in the bianry expansion of the fraction part of the power float

for i = 1 to len(bs$)

'this is the matching sqr of the sqr of the sqr... of x#

runningXSQR# = sqr#(runningXSQR#)

'for every 1 in the expansion, multiple our build with the running sqr of ... sqr of x

if mid$(bs$, i - 1, 1) = "1" then build# = build# * runningXSQR#

next

'our build# should be a estimate or x# to power of pow#

return build#

endfunc

'write a series of 1s and 0s that represent the decimal fraction n/d in binary 20 places long

function bExpand20$(nOver, d)

' b for base

b# = 0.5

' r for remainder

r# = float(nOver)/float(d)

' s for string$ 0's and 1's that we will build and return for function value

s$ = ""

' f for flag to stop

f = 0

' c for count to track how far we are, don't want to go past 20

c = 0

while f = 0

if r# < b#

s$ = s$ + "0"

else

s$ = s$ + "1"

if r# > b#

r# = r# - b#

else

f = 1

endif

endif

c = c + 1

if c >= 20 then f = 1

b# = b# * 0.5

wend

return s$

endfunc

'not right$

function rite$(stringy$, amount)

return mid$(stringy$, len(stringy$) - amount, amount)

endfunc

b = b + geberation