Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -0,0 +1,6 @@
|
|||
using System;
|
||||
|
||||
macro @^ (val, pow : int)
|
||||
{
|
||||
<[ Math.Pow($val, $pow) ]>
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
using Nemerle.Assertions;
|
||||
|
||||
module Expon
|
||||
{
|
||||
Expon(val : int, pow : int) : int // demonstrates simple/naive method
|
||||
requires pow > 0 otherwise throw ArgumentOutOfRangeException("Negative powers not allowed, will not return int.")
|
||||
{
|
||||
mutable result = 1;
|
||||
repeat(pow) {
|
||||
result *= val
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
Expon(val : double, pow : int) : double // demonstrates shift and square method
|
||||
{
|
||||
mutable neg = false;
|
||||
mutable p = pow;
|
||||
when (pow < 0) {neg = true; p = -pow};
|
||||
mutable v = val;
|
||||
mutable result = 1d;
|
||||
|
||||
while (p > 0) {
|
||||
when (p & 1 == 1) result *= v;
|
||||
v *= v;
|
||||
p >>= 1;
|
||||
}
|
||||
if (neg) 1d/result else result
|
||||
}
|
||||
|
||||
Main() : void
|
||||
{
|
||||
def eight = 2^3;
|
||||
// def oops = 2^1.5; // compilation error as operator is defined for integer exponentiation
|
||||
def four = Expon(2, 2);
|
||||
def four_d = Expon(2.0, 2);
|
||||
|
||||
WriteLine($"$eight, $four, $four_d");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
class Exp {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
Pow(2,30)->PrintLine();
|
||||
Pow(2.0,30)->PrintLine();
|
||||
Pow(2.0,-2)->PrintLine();
|
||||
}
|
||||
|
||||
function : native : Pow(base : Float, exp : Int) ~ Float {
|
||||
if(exp < 0) {
|
||||
return 1 / base->Power(exp * -1.0);
|
||||
};
|
||||
|
||||
ans := 1.0;
|
||||
while(exp > 0) {
|
||||
ans *= base;
|
||||
exp -= 1;
|
||||
};
|
||||
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue