The script requests/brainstorming thread!
I'd like to see a script that efficiently calculates decimal/fractional/irrational exponents; for that matter, I'd also like to see a script that efficiently calculates the nth root of a given number.
I say "efficiently" because I have a few behemoths that lumber around in my CPU for a few seconds, then spit out a semi-accurate result. Here they are, if it helps:
// ffract: estimates the fraction equivalent of a given floating-point number
ffract = [
tmp_remainder = $arg1; tmpd0 = 0; tmpd1 = 0; tmpd2 = 1; tmpc = 0; tmpn = 1
while [(&& (<= $tmpc $arg2) (<= $tmpd2 $arg3))] [
tmp_remainder = (divf 1 (-f $tmp_remainder (+ $tmp_remainder)))
tmpd0 = $tmpd1
tmpd1 = $tmpd2
tmpd2 = (+f $tmpd0 (*f $tmpd1 (+ $tmp_remainder)))
tmpn = (roundf (*f $arg1 $tmpd2))
+= tmpc 1]
result (concat (div $tmpn (GCD $tmpn $tmpd2)) (div $tmpd2 (GCD $tmpn $tmpd2)))]
// ffract (number) (iterations) (max denominator)

// powf: A ponderous attempt at decimal/irrational exponentiation
alias powf [if (isint $arg2) [pow $arg1 $arg2] [
if (< $arg2 0) [divf 1 (pow (nthroot (at (ffract (absval $arg2) 6) 1) $arg1) (at (ffract (absval $arg2) 6) 0))
] [pow (nthroot (at (ffract $arg2 6 112) 1) $arg1) (at (ffract $arg2 6 112) 0)]]]
// fpow (base) (decimal)

// nthroot: Newton's Method, sort of
alias nthroot [tmp_nroot = 2
loop i 81 [tmp_nroot = (*f (divf 1 $arg1) (+f (*f (- $arg1 1) $tmp_nroot) (divf $arg2 (pow $tmp_nroot (- $arg1 1)))))]
result $tmp_nroot]
// nthroot (degree) (base)
// nthroot 4 65536 returns 256
As you can see, combining ffract with Newton's nth root method gives rise to some numbers that the AC engine has trouble with...
Example:
ffract 3.141593
(at just a few iterations) returns "362122 115267".
Using powf to find 3^3.141593, the engine has to use this result in nthroot:
nthroot 115267 3
(attempting to find the 115267th root of 3).
Then part of the formula requires the engine to calculate this:
pow 3 115266
which the engine (and the Micro$oft Calculator as well) can't surmount.

Maybe AC could include Bignum...
My brain feels like big num now.
Thanks given by:


Messages In This Thread
RE: The script requests/brainstorming thread! - by V-Man - 14 Apr 11, 09:00AM