Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: NewYork
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 247 lines (186 sloc) 5.348 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
class Complex { ... }

augment class Num does Real {
    method Bridge() {
        self;
    }

    method Int() {
        Q:PIR {
            $P0 = find_lex 'self'
            $I0 = $P0
            $P1 = new ['Int']
            $P1 = $I0
            %r = $P1
        }
    }

    method Rat(Real $epsilon = 1.0e-6) {
        my sub modf($num) { my $q = $num.Int; $num - $q, $q; }

        my $num = +self;
        my $signum = $num < 0 ?? -1 !! 1;
        $num = -$num if $signum == -1;

        # Find convergents of the continued fraction.

        my ($r, $q) = modf($num);
        my ($a, $b) = 1, $q;
        my ($c, $d) = 0, 1;

        while $r != 0 && abs($num - ($b/$d)) > $epsilon {
            ($r, $q) = modf(1/$r);

            ($a, $b) = ($b, $q*$b + $a);
            ($c, $d) = ($d, $q*$d + $c);
        }

        # Note that this result has less error than any Rational with a
        # smaller denominator but it is not (necessarily) the Rational
        # with the smallest denominator that has less than $epsilon error.
        # However, to find that Rational would take more processing.

        ($signum * $b) / $d;
    }

    method Num() { self; }

    method isNaN() {
        self != self;
    }

    method ln(Num $x:) {
        pir::ln__Nn($x);
    }

    multi method perl() {
        ~self;
    }

    method sqrt(Num $x:) {
        pir::sqrt__Nn($x);
    }

    method floor(Real $x:) {
        given $x {
            when NaN { NaN }
            when Inf { Inf }
            when -Inf { -Inf }
            pir::box__PI(pir::floor__IN($x));
        }
    }

    method ceiling(Num $x:) {
        given $x {
            when NaN { NaN }
            when Inf { Inf }
            when -Inf { -Inf }
            pir::box__PI(pir::ceil__IN($x));
        }
    }

    method rand(Num $x:) {
        pir::box__PN(pir::rand__NN($x))
    }

    method sin(Num $x: $base = Radians) {
        pir::sin__Nn($x.to-radians($base));
    }

    method asin(Num $x: $base = Radians) {
        pir::asin__Nn($x).from-radians($base);
    }

    method cos(Num $x: $base = Radians) {
        pir::cos__Nn($x.to-radians($base));
    }

    method acos(Num $x: $base = Radians) {
        pir::acos__Nn($x).from-radians($base);
    }

    method tan(Num $x: $base = Radians) {
        pir::tan__Nn($x.to-radians($base));
    }

    method atan(Num $x: $base = Radians) {
        pir::atan__Nn($x).from-radians($base);
    }

    method sec(Num $x: $base = Radians) {
        pir::sec__Nn($x.to-radians($base));
    }

    method asec(Num $x: $base = Radians) {
        pir::asec__Nn($x).from-radians($base);
    }

    method sinh(Num $x: $base = Radians) {
        pir::sinh__Nn($x.to-radians($base));
    }

    method asinh(Num $x: $base = Radians) {
        ($x + ($x * $x + 1).sqrt).log.from-radians($base);
    }

    method cosh(Num $x: $base = Radians) {
        pir::cosh__Nn($x.to-radians($base));
    }

    method acosh(Num $x: $base = Radians) {
        ($x + ($x * $x - 1).sqrt).log.from-radians($base);
    }

    method tanh(Num $x: $base = Radians) {
        pir::tanh__Nn($x.to-radians($base));
    }

    method atanh(Num $x: $base = Radians) {
        (((1 + $x) / (1 - $x)).log / 2).from-radians($base);
    }

    method sech(Num $x: $base = Radians) {
        pir::sech__Nn($x.to-radians($base));
    }

    method asech(Num $x: $base = Radians) {
        (1 / $x).acosh($base);
    }

    method cosech(Num $x: $base = Radians) {
        1 / $x.sinh($base);
    }

    method acosech(Num $x: $base = Radians) {
        (1 / $x).asinh($base);
    }

    method cosec(Num $x: $base = Radians) {
        1 / $x.sin($base);
    }

    method acosec(Num $x: $base = Radians) {
        (1 / $x).asin($base);
    }

    method cotan(Num $x: $base = Radians) {
        1 / $x.tan($base);
    }

    method acotan(Num $x: $base = Radians) {
        (1 / $x).atan($base);
    }

    method cotanh(Num $x: $base = Radians) {
        1 / $x.tanh($base);
    }

    method acotanh(Num $x: $base = Radians) {
        (1 / $x).atanh($base);
    }

    method atan2(Num $y: $x = 1, $base = Radians) {
        pir::atan__NNn($y, $x.Numeric.Num).from-radians($base);
    }
}

multi sub infix:<cmp>(Num $a, Num $b) {
    pir::cmp__INN($a, $b);
}

multi sub infix:«<=>»(Num $a, Num $b) {
    pir::cmp__INN($a, $b);
}

multi sub infix:«==»(Num $a, Num $b) {
    pir::iseq__INN( $a, $b) ?? True !! False
}

multi sub infix:«!=»(Num $a, Num $b) {
    pir::iseq__INN( $a, $b) ?? False !! True # note reversed
}

multi sub infix:«<»(Num $a, Num $b) {
    pir::islt__INN( $a, $b) ?? True !! False
}

multi sub infix:«>»(Num $a, Num $b) {
    pir::isgt__INN( $a, $b) ?? True !! False
}

multi sub infix:«<=»(Num $a, Num $b) {
    pir::isgt__INN( $a, $b) ?? False !! True # note reversed
}

multi sub infix:«>=»(Num $a, Num $b) {
    pir::islt__INN( $a, $b) ?? False !! True # note reversed
}

# Arithmetic operators

multi sub prefix:<->(Num $a) {
    pir::neg__NN($a);
}

multi sub infix:<+>(Num $a, Num $b) {
    pir::add__NNN($a, $b)
}

multi sub infix:<->(Num $a, Num $b) {
    pir::sub__NNN($a, $b)
}

multi sub infix:<*>(Num $a, Num $b) {
    pir::mul__NNN($a, $b)
}

multi sub infix:</>(Num $a, Num $b) {
    pir::div__NNN($a, $b)
}

multi sub infix:<**>(Num $a, Num $b) {
    pir::pow__NNN($a, $b)
}


# vim: ft=perl6
Something went wrong with that request. Please try again.