2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,4 +1,8 @@
Most programming languages have a built-in implementation of exponentiation.
Re-implement integer exponentiation for both int<sup>int</sup> and float<sup>int</sup> as both a procedure, and an operator (if your language supports operator definition).
If the language supports operator (or procedure) overloading, then an overloaded form should be provided for both int<sup>int</sup> and float<sup>int</sup> variants.
;Task:
Re-implement integer exponentiation for both &nbsp; <big>int<sup>int</sup></big> &nbsp; and &nbsp; <big>float<sup>int</sup></big> &nbsp; as both a procedure, &nbsp; and an operator (if your language supports operator definition).
If the language supports operator (or procedure) overloading, then an overloaded form should be provided for both &nbsp; <big>int<sup>int</sup></big> &nbsp; and &nbsp; <big>float<sup>int</sup></big> &nbsp; variants.
<br><br>

View file

@ -1,7 +1 @@
$ awk 'function pow(x,n){r=1;for(i=0;i<n;i++)r=r*x;return r}{print pow($1,$2)}'
2.5 2
6.25
10 6
1000000
3 0
1

View file

@ -0,0 +1,2 @@
If you want to use arbitrary precision number with (more recent) awk, you have to use -M option :
$ gawk -M '{ printf("%f\n",$1^$2) }'

View file

@ -0,0 +1,2 @@
And if you want to use locales for decimal separator, you have tu use -N option :
$ gawk -N '{ printf("%f\n",$1^$2) }'

View file

@ -0,0 +1,11 @@
open number
_ ^ 0 = 1
x ^ n | n > 0 = f x (n - 1) x
|else = fail "Negative exponent"
where f _ 0 y = y
f a d y = g a d
where g b i | even i = g (b * b) (i `quot` 2)
| else = f b (i - 1) (b * y)
(12 ^ 4, 12 ** 4)

View file

@ -0,0 +1,20 @@
open number
//Function quot from number module is defined only for
//integral numbers. We can use this as an universal quot.
uquot x y | x is Integral = x `quot` y
| else = x / y
//Changing implementation by using generic numeric literals
//(e.g. 2u) and elimitating all comparisons with 0.
!x ^ n | n ~= 0u = 1u
| n > 0u = f x (n - 1u) x
| else = fail "Negative exponent"
where f a d y
| d ~= 0u = y
| else = g a d
where g b i | even i = g (b * b) (i `uquot` 2u)
| else = f b (i - 1u) (b * y)
(12 ^ 4, 12.34 ^ 4.04)

View file

@ -0,0 +1,28 @@
open number
//A class that defines our overloadable function
class Exponent a where
(^) a->a->_
//Implementation for integers
instance Exponent Int where
_ ^ 0 = 1
x ^ n | n > 0 = f x (n - 1) x
|else = fail "Negative exponent"
where f _ 0 y = y
f a d y = g a d
where g b i | even i = g (b * b) (i `quot` 2)
| else = f b (i - 1) (b * y)
//Implementation for floats
instance Exponent Single where
x ^ n | n < 0.001 = 1
| n > 0 = f x (n - 1) x
| else = fail "Negative exponent"
where f a d y
| d < 0.001 = y
| else = g a d
where g b i | even i = g (b * b) (i / 2)
| else = f b (i - 1) (b * y)
(12 ^ 4, 12.34 ^ 4.04)

View file

@ -1,38 +1,42 @@
/*REXX program to show various (integer) exponentiations. */
say center('digits='digits(),79,'')
/*REXX program computes and displays various (integer) exponentiations. */
say center('digits='digits(), 79, "")
say '17**65 is:'
say 17**65
say
numeric digits 100; say; say center('digits='digits(),79,'')
numeric digits 100; say center('digits='digits(), 79, "")
say '17**65 is:'
say 17**65
say
numeric digits 10; say; say center('digits='digits(),79,'')
numeric digits 10; say center('digits='digits(), 79, "")
say '2 ** -10 is:'
say 2 ** -10
say
numeric digits 30; say; say center('digits='digits(),79,'')
numeric digits 30; say center('digits='digits(), 79, "")
say '-3.1415926535897932384626433 ** 3 is:'
say -3.1415926535897932384626433 ** 3
say
numeric digits 1000; say; say center('digits='digits(),79,'')
numeric digits 1000; say center('digits='digits(), 79, "")
say '2 ** 1000 is:'
say 2 ** 1000
say
numeric digits 60; say; say center('digits='digits(),79,'')
say 'ipow(5,70) is:'
say ipow(5,70)
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────ERRIPOW subroutine──────────────────*/
errIpow: say; say '***error!***'; say; say arg(1); say; say; exit 13
/*──────────────────────────────────IPOW subroutine─────────────────────*/
ipow: procedure; parse arg x 1 _,p
if arg()<2 then call erripow 'not enough arguments specified'
if arg()>2 then call erripow 'too many arguments specified'
if \datatype(_,'N') then call erripow "1st arg isn't numeric:" _
if \datatype(p,'W') then call erripow "2nd arg isn't an integer:" p
if p=0 then return 1
pa=abs(p)
do pa-1; _=_*x; end
if p<0 then _=1/_
return _
numeric digits 60; say center('digits='digits(), 79, "")
say 'iPow(5, 70) is:'
say iPow(5, 70)
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
errMsg: say; say '***error***'; say; say arg(1); say; say; exit 13
/*──────────────────────────────────────────────────────────────────────────────────────*/
iPow: procedure; parse arg x 1 _,p
if arg()<2 then call errMsg "not enough arguments specified"
if arg()>2 then call errMsg "too many arguments specified"
if \datatype(x,'N') then call errMsg "1st arg isn't numeric:" x
if \datatype(p,'W') then call errMsg "2nd arg isn't an integer:" p
if p=0 then return 1
do abs(p) - 1; _=_*x; end /*abs(p)-1*/
if p<0 then _=1/_
return _