September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,23 @@
PRINT "11^5 = " ; FNipow(11, 5)
PRINT "PI^3 = " ; FNfpow(PI, 3)
END
DEF FNipow(A%, B%)
LOCAL I%, P%
P% = 1
FOR I% = 1 TO 32
P% *= P%
IF B% < 0 THEN P% *= A%
B% = B% << 1
NEXT
= P%
DEF FNfpow(A, B%)
LOCAL I%, P
P = 1
FOR I% = 1 TO 32
P *= P
IF B% < 0 THEN P *= A
B% = B% << 1
NEXT
= P

View file

@ -0,0 +1,6 @@
100 DEF POW(X,Y)
110 IF X=0 THEN LET POW=0:EXIT DEF
120 LET POW=EXP(Y*LOG(X))
130 END DEF
140 PRINT POW(PI,3)
150 PRINT PI^3

View file

@ -0,0 +1,18 @@
func raise<T: Numeric>(_ base: T, to exponent: Int) -> T {
precondition(exponent >= 0, "Exponent has to be nonnegative")
return Array(repeating: base, count: exponent).reduce(1, *)
}
infix operator **: MultiplicationPrecedence
func **<T: Numeric>(lhs: T, rhs: Int) -> T {
return raise(lhs, to: rhs)
}
let someFloat: Float = 2
let someInt: Int = 10
assert(raise(someFloat, to: someInt) == 1024)
assert(someFloat ** someInt == 1024)
assert(raise(someInt, to: someInt) == 10000000000)
assert(someInt ** someInt == 10000000000)

View file

@ -0,0 +1,48 @@
Public Function exp(ByVal base As Variant, ByVal exponent As Long) As Variant
Dim result As Variant
If TypeName(base) = "Integer" Or TypeName(base) = "Long" Then
'integer exponentiation
result = 1
If exponent < 0 Then
result = IIf(Abs(base) <> 1, CVErr(2019), IIf(exponent Mod 2 = -1, base, 1))
End If
Do While exponent > 0
If exponent Mod 2 = 1 Then result = result * base
base = base * base
exponent = exponent \ 2
Loop
Else
Debug.Assert IsNumeric(base)
'float exponentiation
If base = 0 Then
If exponent < 0 Then result = CVErr(11)
Else
If exponent < 0 Then
base = 1# / base
exponent = -exponent
End If
result = 1
Do While exponent > 0
If exponent Mod 2 = 1 Then result = result * base
base = base * base
exponent = exponent \ 2
Loop
End If
End If
exp = result
End Function
Public Sub main()
Debug.Print "Integer exponentiation"
Debug.Print "10^7=", exp(10, 7)
Debug.Print "10^4=", exp(10, 4)
Debug.Print "(-3)^3=", exp(-3, 3)
Debug.Print "(-1)^(-5)=", exp(-1, -5)
Debug.Print "10^(-1)=", exp(10, -1)
Debug.Print "0^2=", exp(0, 2)
Debug.Print "Float exponentiation"
Debug.Print "10.0^(-3)=", exp(10#, -3)
Debug.Print "10.0^(-4)=", exp(10#, -4)
Debug.Print "(-3.0)^(-5)=", exp(-3#, -5)
Debug.Print "(-3.0)^(-4)=", exp(-3#, -4)
Debug.Print "0.0^(-4)=", exp(0#, -4)
End Sub