This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,6 @@
using System;
macro @^ (val, pow : int)
{
<[ Math.Pow($val, $pow) ]>
}

View file

@ -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");
}
}