Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: Tokyo
Fetching contributors…

Cannot retrieve contributors at this time

file 278 lines (208 sloc) 8.497 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
my role Numeric {
    multi method Numeric(Numeric:D:) { self }

    multi method ACCEPTS(Numeric:D: $a) {
        self.isNaN ?? $a.isNaN !! $a == self;
    }

    proto method log(|) {*}
    multi method log(Numeric:D: Cool $base) { self.log / $base.Numeric.log }
    multi method log(Numeric:D: Numeric $base) { self.log / $base.log }

    method log10() { self.log / 10e0.log }

    proto method exp(|) {*}
    multi method exp(Numeric:D: $base) {
        $base ** self;
    }
    method roots(Cool $n) { self.Complex.roots($n.Int) }

    multi method Bool(Numeric:D:) { self != 0 }

    multi method gist(Numeric:D:) { self.Str }

    method succ() { self + 1 }
    method pred() { self - 1 }
}

multi sub infix:<eqv>(Numeric:D $a, Numeric:D $b) {
    $a.WHAT === $b.WHAT && ($a cmp $b) == 0
}

## arithmetic operators

proto prefix:<+>(|) { * }
multi prefix:<+>(\a) { a.Numeric }

proto prefix:<->(|) { * }
multi prefix:<->(\a) { -a.Numeric }

proto sub abs(|) { * }
multi sub abs(\a) { abs a.Numeric }

proto sub sign(|) {*}
multi sub sign(Numeric \x) { x.sign }
multi sub sign(Cool \x) { x.Numeric.sign }

proto sub log(|) {*}
multi sub log(Numeric $x) { $x.log }
multi sub log(Numeric $x, Numeric $base) { $x.log($base) }
multi sub log(Cool $x) { $x.Numeric.log }
multi sub log(Cool $x, Cool $base) { $x.Numeric.log($base.Numeric) }

proto sub log10(|) {*}
multi sub log10(Numeric $x) { $x.log(10e0) }
multi sub log10(Cool $x) { $x.Numeric.log(10e0) }

proto sub exp(|) {*}
multi sub exp(Numeric $x) { $x.exp }
multi sub exp(Numeric $x, Numeric $base) { $x.exp($base) }

proto sub sin(|) {*}
multi sub sin(Numeric \x) { x.sin }
multi sub sin(Cool \x) { x.Numeric.sin }

proto sub asin(|) {*}
multi sub asin(Numeric \x) { x.asin }
multi sub asin(Cool \x) { x.Numeric.asin }

proto sub cos(|) {*}
multi sub cos(Numeric \x) { x.cos }
multi sub cos(Cool \x) { x.Numeric.cos }

proto sub acos(|) {*}
multi sub acos(Numeric \x) { x.acos }
multi sub acos(Cool \x) { x.Numeric.acos }

proto sub tan(|) {*}
multi sub tan(Numeric \x) { x.tan }
multi sub tan(Cool \x) { x.Numeric.tan }

proto sub atan(|) {*}
multi sub atan(Numeric \x) { x.atan }
multi sub atan(Cool \x) { x.Numeric.atan }

proto sub sec(|) {*}
multi sub sec(Numeric \x) { x.sec }
multi sub sec(Cool \x) { x.Numeric.sec }

proto sub asec(|) {*}
multi sub asec(Numeric \x) { x.asec }
multi sub asec(Cool \x) { x.Numeric.asec }

proto sub cosec(|) {*}
multi sub cosec(Numeric \x) { x.cosec }
multi sub cosec(Cool \x) { x.Numeric.cosec }

proto sub acosec(|) {*}
multi sub acosec(Numeric \x) { x.acosec }
multi sub acosec(Cool \x) { x.Numeric.acosec }

proto sub cotan(|) {*}
multi sub cotan(Numeric \x) { x.cotan }
multi sub cotan(Cool \x) { x.Numeric.cotan }

proto sub acotan(|) {*}
multi sub acotan(Numeric \x) { x.acotan }
multi sub acotan(Cool \x) { x.Numeric.acotan }

proto sub sinh(|) {*}
multi sub sinh(Numeric \x) { x.sinh }
multi sub sinh(Cool \x) { x.Numeric.sinh }

proto sub asinh(|) {*}
multi sub asinh(Numeric \x) { x.asinh }
multi sub asinh(Cool \x) { x.Numeric.asinh }

proto sub cosh(|) {*}
multi sub cosh(Numeric \x) { x.cosh }
multi sub cosh(Cool \x) { x.Numeric.cosh }

proto sub acosh(|) {*}
multi sub acosh(Numeric \x) { x.acosh }
multi sub acosh(Cool \x) { x.Numeric.acosh }

proto sub tanh(|) {*}
multi sub tanh(Numeric \x) { x.tanh }
multi sub tanh(Cool \x) { x.Numeric.tanh }

proto sub atanh(|) {*}
multi sub atanh(Numeric \x) { x.atanh }
multi sub atanh(Cool \x) { x.Numeric.atanh }

proto sub sech(|) {*}
multi sub sech(Numeric \x) { x.sech }
multi sub sech(Cool \x) { x.Numeric.sech }

proto sub asech(|) {*}
multi sub asech(Numeric \x) { x.asech }
multi sub asech(Cool \x) { x.Numeric.asech }

proto sub cosech(|) {*}
multi sub cosech(Numeric \x) { x.cosech }
multi sub cosech(Cool \x) { x.Numeric.cosech }

proto sub acosech(|) {*}
multi sub acosech(Numeric \x) { x.acosech }
multi sub acosech(Cool \x) { x.Numeric.acosech }

proto sub cotanh(|) {*}
multi sub cotanh(Numeric \x) { x.cotanh }
multi sub cotanh(Cool \x) { x.Numeric.cotanh }

proto sub acotanh(|) {*}
multi sub acotanh(Numeric \x) { x.acotanh }
multi sub acotanh(Cool \x) { x.Numeric.acotanh }

proto sub sqrt(|) {*}
multi sub sqrt(Numeric \x) { x.sqrt }
multi sub sqrt(Cool \x) { x.Numeric.sqrt }

proto sub roots($, $) { * }
multi sub roots($x, Cool $n) { $x.Numeric.Complex.roots($n.Int) }
multi sub roots($x, Numeric $n) { $x.Numeric.Complex.roots($n.Int) }

proto sub floor(|) { * }
multi sub floor($a) { $a.Numeric.floor }
multi sub floor(Numeric $a) { $a.floor }

proto sub ceiling(|) { * }
multi sub ceiling($a) { $a.Numeric.ceiling }
multi sub ceiling(Numeric $a) { $a.ceiling }

proto sub round(|) { * }
multi sub round($a) { $a.Numeric.round }
multi sub round(Numeric $a) { $a.round }
multi sub round(Numeric $a, $scale) { $a.round($scale) }

proto infix:<+>($a?, $b?) { * }
multi infix:<+>($x = 0) { $x.Numeric }
multi infix:<+>(\a, \b) { a.Numeric + b.Numeric }

proto infix:<->($a?, $b?) { * }
multi infix:<->($x = 0) { $x.Numeric }
multi infix:<->(\a, \b) { a.Numeric - b.Numeric }

proto infix:<*>($a?, $b?) { * }
multi infix:<*>($x = 1) { $x.Numeric }
multi infix:<*>(\a, \b) { a.Numeric * b.Numeric }

proto infix:</>($a?, $b?) { * }
multi infix:</>() { fail "No zero-arg meaning for infix:</>" }
multi infix:</>($x) { $x.Numeric }
multi infix:</>(\a, \b) { a.Numeric / b.Numeric }

proto infix:<div>($a?, $b?) { * }
# rest of infix:<div> is in Int.pm

proto infix:<%>($a?, $b?) { * }
multi infix:<%>() { fail "No zero-arg meaning for infix:<%>" }
multi infix:<%>($x) { $x }
multi infix:<%>(\a, \b) { a.Real % b.Real }

proto infix:<%%>($a?, $b?) { * }
multi infix:<%%>() { fail "No zero-arg meaning for infix:<%%>" }
multi infix:<%%>($x) { Bool::True }
multi infix:<%%>(\a, \b) { a.Real % b.Real == 0 }

proto infix:<lcm>($a?, $b?) { * }
multi infix:<lcm>(Int $x = 1) { $x }
multi infix:<lcm>(\a, \b) { a.Int lcm b.Int }

proto infix:<gcd>($a?, $b?) { * }
multi infix:<gcd>() { fail 'No zero-arg meaning for infix:<gcd>' }
multi infix:<gcd>(Int $x) { $x }
multi infix:<gcd>(\a, \b) { a.Int gcd b.Int }

proto infix:<**>($a?, $b?) { * }
multi infix:<**>($x = 1) { $x.Numeric }
multi infix:<**>(\a, \b) { a.Numeric ** b.Numeric }

## relational operators

proto infix:«<=>»(|) { * }
multi infix:«<=>»(\a, \b) { a.Real <=> b.Real }

proto infix:<==>($a?, $b?) { * }
multi infix:<==>($x?) { Bool::True }
multi infix:<==>(\a, \b) { a.Numeric == b.Numeric }

proto infix:<!=>(Mu $a?, Mu $b?) { * }
multi infix:<!=>($x?) { Bool::True }
multi infix:<!=>(Mu \a, Mu \b) { not a == b }

proto infix:«<»($a?, $b?) { * }
multi infix:«<»($x?) { Bool::True }
multi infix:«<»(\a, \b) { a.Real < b.Real }

proto infix:«<=»($a?, $b?) { * }
multi infix:«<=»($x?) { Bool::True }
multi infix:«<=»(\a, \b) { a.Real <= b.Real }

proto infix:«>»($a?, $b?) { * }
multi infix:«>»($x?) { Bool::True }
multi infix:«>»(\a, \b) { a.Real > b.Real }

proto infix:«>=»($a?, $b?) { * }
multi infix:«>=»($x?) { Bool::True }
multi infix:«>=»(\a, \b) { a.Real >= b.Real }

## bitwise operators

proto infix:<+&>(|) { * }
multi infix:<+&>() { +^0 }
multi infix:<+&>($x) { $x }
multi infix:<+&>($x, $y) { $x.Numeric.Int +& $y.Numeric.Int }

proto infix:<+|>(|) { * }
multi infix:<+|>() { 0 }
multi infix:<+|>($x) { $x }
multi infix:<+|>($x, $y) { $x.Numeric.Int +| $y.Numeric.Int }

proto infix:<+^>(|) { * }
multi infix:<+^>() { 0 }
multi infix:<+^>($x) { $x }
multi infix:<+^>($x, $y) { $x.Numeric.Int +^ $y.Numeric.Int }

proto infix:«+<»(|) { * }
multi infix:«+<»() { fail "No zero-arg meaning for infix:«+<»"; }
multi infix:«+<»($x) { $x }
multi infix:«+<»($x,$y) { $x.Numeric.Int +< $y.Numeric.Int }

proto infix:«+>»(|) { * }
multi infix:«+>»() { fail "No zero-arg meaning for infix:«+>»"; }
multi infix:«+>»($x) { $x }
multi infix:«+>»($x,$y) { $x.Numeric.Int +> $y.Numeric.Int }

proto prefix:<+^>(|) { * }
multi prefix:<+^>($x) { +^ $x.Numeric.Int }
Something went wrong with that request. Please try again.