Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,15 @@
|
|||
package Integer_Exponentiation is
|
||||
-- int^int
|
||||
procedure Exponentiate (Argument : in Integer;
|
||||
Exponent : in Natural;
|
||||
Result : out Integer);
|
||||
function "**" (Left : Integer;
|
||||
Right : Natural) return Integer;
|
||||
|
||||
-- real^int
|
||||
procedure Exponentiate (Argument : in Float;
|
||||
Exponent : in Integer;
|
||||
Result : out Float);
|
||||
function "**" (Left : Float;
|
||||
Right : Integer) return Float;
|
||||
end Integer_Exponentiation;
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
with Ada.Float_Text_IO, Ada.Integer_Text_IO, Ada.Text_IO;
|
||||
with Integer_Exponentiation;
|
||||
|
||||
procedure Test_Integer_Exponentiation is
|
||||
use Ada.Float_Text_IO, Ada.Integer_Text_IO, Ada.Text_IO;
|
||||
use Integer_Exponentiation;
|
||||
R : Float;
|
||||
I : Integer;
|
||||
begin
|
||||
Exponentiate (Argument => 2.5, Exponent => 3, Result => R);
|
||||
Put ("2.5 ^ 3 = ");
|
||||
Put (R, Fore => 2, Aft => 4, Exp => 0);
|
||||
New_Line;
|
||||
|
||||
Exponentiate (Argument => -12, Exponent => 3, Result => I);
|
||||
Put ("-12 ^ 3 = ");
|
||||
Put (I, Width => 7);
|
||||
New_Line;
|
||||
end Test_Integer_Exponentiation;
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
package body Integer_Exponentiation is
|
||||
-- int^int
|
||||
procedure Exponentiate (Argument : in Integer;
|
||||
Exponent : in Natural;
|
||||
Result : out Integer) is
|
||||
begin
|
||||
Result := 1;
|
||||
for Counter in 1 .. Exponent loop
|
||||
Result := Result * Argument;
|
||||
end loop;
|
||||
end Exponentiate;
|
||||
|
||||
function "**" (Left : Integer;
|
||||
Right : Natural) return Integer is
|
||||
Result : Integer;
|
||||
begin
|
||||
Exponentiate (Argument => Left,
|
||||
Exponent => Right,
|
||||
Result => Result);
|
||||
return Result;
|
||||
end "**";
|
||||
|
||||
-- real^int
|
||||
procedure Exponentiate (Argument : in Float;
|
||||
Exponent : in Integer;
|
||||
Result : out Float) is
|
||||
begin
|
||||
Result := 1.0;
|
||||
if Exponent < 0 then
|
||||
for Counter in Exponent .. -1 loop
|
||||
Result := Result / Argument;
|
||||
end loop;
|
||||
else
|
||||
for Counter in 1 .. Exponent loop
|
||||
Result := Result * Argument;
|
||||
end loop;
|
||||
end if;
|
||||
end Exponentiate;
|
||||
|
||||
function "**" (Left : Float;
|
||||
Right : Integer) return Float is
|
||||
Result : Float;
|
||||
begin
|
||||
Exponentiate (Argument => Left,
|
||||
Exponent => Right,
|
||||
Result => Result);
|
||||
return Result;
|
||||
end "**";
|
||||
end Integer_Exponentiation;
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
@echo off
|
||||
setlocal enableextensions enabledelayedexpansion
|
||||
set /p a=
|
||||
set /p b=
|
||||
call :pow %a% %b%
|
||||
echo %_a%
|
||||
goto :eof
|
||||
:pow Integer Integer
|
||||
:: Right-to-left binary exponentiation
|
||||
set /a "_a=1, s=%1, e=%2"
|
||||
:looppow
|
||||
if %e% neq 0 (
|
||||
set /a "t=e&1"
|
||||
if !t! equ 1 (set /a "_a*=s")
|
||||
set /a "s*=s, e>>=1"
|
||||
goto looppow
|
||||
)
|
||||
goto :eof
|
||||
|
|
@ -1,15 +1,13 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">powir</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">i</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">b</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">b</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">b</span>
|
||||
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">powir</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
with javascript_semantics
|
||||
function powir(atom b, integer i)
|
||||
atom res = 1
|
||||
if i<0 then {b,i} = {1/b,abs(i)} end if
|
||||
while i do
|
||||
if and_bits(i,1) then res *= b end if
|
||||
b *= b
|
||||
i = floor(i/2)
|
||||
end while
|
||||
return res
|
||||
end function
|
||||
?powir(-3,-5)
|
||||
?power(-3,-5)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,68 @@
|
|||
class num2
|
||||
static function ipow(i, exp)
|
||||
assert(math.type(i) == "integer", "receiver must be an integer")
|
||||
assert(math.type(exp) == "integer", "exponent must be an integer")
|
||||
if i == 1 or exp == 0 then return 1 end
|
||||
if i == -1 then return exp % 2 == 0 ? 1 : -1 end
|
||||
assert(exp >= 0, "exponent cannot be negative for non-unit values")
|
||||
local ans = 1
|
||||
local base = i
|
||||
local e = exp
|
||||
while e > 1 do
|
||||
if e % 2 == 1 then ans *= base end
|
||||
e //= 2
|
||||
base *= base
|
||||
end
|
||||
return ans * base
|
||||
end
|
||||
|
||||
static function fpow(f, exp)
|
||||
assert(type(f) == "number", "receiver must be a number")
|
||||
assert(math.type(exp) == "integer", "exponent must be an integer")
|
||||
local ans = 1.0
|
||||
local e = exp
|
||||
local base = e < 0 ? 1 / f : f
|
||||
if e < 0 then e = -e end
|
||||
while e > 0 do
|
||||
if e % 2 == 1 then ans *= base end
|
||||
e //= 2
|
||||
base *= base
|
||||
end
|
||||
return ans
|
||||
end
|
||||
|
||||
public n
|
||||
|
||||
function __construct(n)
|
||||
assert(type(n) == "number", "argument must be a number")
|
||||
self.n = n
|
||||
end
|
||||
|
||||
-- Overloads '^' operator.
|
||||
function __pow(exp)
|
||||
assert(math.type(exp) == "integer", "exponent must be an integer")
|
||||
if math.type(n) == "integer" and (exp >= 0 or math.abs(self.n) == 1) then
|
||||
return num2.ipow(self.n, exp)
|
||||
else
|
||||
return num2.fpow(self.n, exp)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
print("Using static methods:")
|
||||
print($" 2 ^ 3 = {num2.ipow( 2, 3)}")
|
||||
print($" 1 ^ -10 = {num2.ipow( 1, -10)}")
|
||||
print($" -1 ^ -3 = {num2.ipow(-1, -3)}")
|
||||
print()
|
||||
print($" 2.0 ^ -3 = {num2.fpow(2.0, -3)}")
|
||||
print($" 1.5 ^ 0 = {num2.fpow(1.5, 0)}")
|
||||
print($" 4.5 ^ 2 = {num2.fpow(4.5, 2)}")
|
||||
|
||||
print("\nUsing the ^ operator:")
|
||||
print($" 2 ^ 3 = {new num2(2) ^ 3}")
|
||||
print($" 1 ^ -10 = {new num2(1) ^ -10}")
|
||||
print($" -1 ^ -3 = {new num2(-1) ^ -3}")
|
||||
print()
|
||||
print($" 2.0 ^ -3 = {new num2(2.0) ^ -3}")
|
||||
print($" 1.5 ^ 0 = {new num2(1.5) ^ 0}")
|
||||
print($" 4.5 ^ 2 = {new num2(4.5) ^ 2}")
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
function pow($a, [int]$b) {
|
||||
if ($b -eq -1) { return 1/$a }
|
||||
if ($b -eq 0) { return 1 }
|
||||
if ($b -eq 1) { return $a }
|
||||
if ($b -lt 0) {
|
||||
$rec = $true # reciprocal needed
|
||||
$b = -$b
|
||||
}
|
||||
|
||||
$result = $a
|
||||
2..$b | ForEach-Object {
|
||||
$result *= $a
|
||||
}
|
||||
|
||||
if ($rec) {
|
||||
return 1/$result
|
||||
} else {
|
||||
return $result
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
Pow ← ∧(×⊙˙×)⊙1+1×⊙-₁(⇌⋯)
|
||||
Pow 10 2
|
||||
⍜⊙°ₑ× 10 2
|
||||
⍜⊙°ₑ× 0.5 2
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
Function pow(x,y)
|
||||
pow = 1
|
||||
If y < 0 Then
|
||||
For i = 1 To Abs(y)
|
||||
pow = pow * (1/x)
|
||||
Next
|
||||
Else
|
||||
For i = 1 To y
|
||||
pow = pow * x
|
||||
Next
|
||||
End If
|
||||
End Function
|
||||
|
||||
WScript.StdOut.Write "2 ^ 0 = " & pow(2,0)
|
||||
WScript.StdOut.WriteLine
|
||||
WScript.StdOut.Write "7 ^ 6 = " & pow(7,6)
|
||||
WScript.StdOut.WriteLine
|
||||
WScript.StdOut.Write "3.14159265359 ^ 9 = " & pow(3.14159265359,9)
|
||||
WScript.StdOut.WriteLine
|
||||
WScript.StdOut.Write "4 ^ -6 = " & pow(4,-6)
|
||||
WScript.StdOut.WriteLine
|
||||
WScript.StdOut.Write "-3 ^ 5 = " & pow(-3,5)
|
||||
WScript.StdOut.WriteLine
|
||||
Loading…
Add table
Add a link
Reference in a new issue