Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / SCClassLibrary / Common / Math / Number.sc
blob9a305e471fef12c097b99240f0d220b04847e92b
1 Number : Magnitude {
2         isNumber { ^true }
4         + { arg aNumber; ^this.subclassResponsibility(thisMethod) }
5         - { arg aNumber; ^this.subclassResponsibility(thisMethod) }
6         * { arg aNumber; ^this.subclassResponsibility(thisMethod) }
7         / { arg aNumber; ^this.subclassResponsibility(thisMethod) }
8         mod { arg aNumber; ^this.subclassResponsibility(thisMethod) }
9         div { arg aNumber; ^this.subclassResponsibility(thisMethod) }
10         pow { arg aNumber; ^this.subclassResponsibility(thisMethod) }
12         performBinaryOpOnSeqColl { arg aSelector, aSeqColl, adverb;
13                 ^aSeqColl.collect({ arg item;
14                         item.perform(aSelector, this, adverb)
15                 })
16         }
17         performBinaryOpOnPoint { arg op, aPoint, adverb;
18                 ^Point.new(this.perform(op, aPoint.x, adverb), this.perform(op, aPoint.y, adverb));
19         }
21         // polar support
22         rho { ^this }
23         theta { ^0.0 }
25         // complex support
26         real { ^this }
27         imag { ^0.0 }
29         // conversion
30         @ { arg aNumber; ^Point.new(this, aNumber) }
31         complex { arg imaginaryPart; ^Complex.new(this, imaginaryPart) }
32         polar { arg angle; ^Polar.new(this, angle) }
34         // iteration
35         for { arg endValue, function;
36                 var i, j = 0;
37                 i = this;
38                 while ({ i <= endValue }, { function.value(i, j); i = i + 1; j = j + 1 });
39         }
40         forBy { arg endValue, stepValue, function;
41                 var i, j=0;
42                 i = this;
43                 (stepValue > 0).if({
44                         while ({ i <= endValue }, { function.value(i,j); i = i + stepValue; j=j+1; });
45                 }, {
46                         while ({ i >= endValue }, { function.value(i,j); i = i + stepValue; j=j+1; });
47                 });
48         }
50         forSeries { arg second, last, function;
51                 // called by generator expression
52                 // compiler replaces this with special byte codes.
53                 var step, j=0;
54                 if (second.isNil) {
55                         last = last ? inf;
56                         step = if (this < last, 1, -1);
57                 }{
58                         last ?? { last = if (second < this, -inf, inf) };
59                         step = second - this;
60                 };
61                 ^this.forBy(last, step, function)
62         }