File.exists: use boost::filesystem::exists
[supercollider.git] / HelpSource / Classes / SimpleNumber.schelp
blob6cbfd7b2640ca235231f7d2da00b5fcde82977ff
1 CLASS:: SimpleNumber
2 summary:: one-dimensional value
3 categories:: Math
4 related::Classes/Polar, Classes/Complex, Classes/Float, Classes/Integer, Classes/UnaryOpUGen, Classes/BinaryOpUGen
6 DESCRIPTION::
7 Base class for numbers which can be represented by a single one dimensional value.
9 Most of the Unary and Binary operations are also implemented by link::Classes/UnaryOpUGen:: and link::Classes/BinaryOpUGen::, so you can get more examples by looking at the help for those.
11 CLASSMETHODS::
13 method:: new
14 allocates a new SimpleNumber.
16 INSTANCEMETHODS::
18 private:: prSimpleNumberSeries
20 subsection:: math support
22 method:: +
23 Addition
26 method:: -
27 Subtraction
29 method:: *
30 Multiplication
32 method:: /
33 Division
35 method:: %
36 Modulo
38 method:: mod
39 Modulo
41 method:: div
42 Integer Division
44 method:: **
45 Exponentiation
47 method:: !=
48 Is not
50 method:: >
51 greater than
53 method:: <
54 greater than
56 method:: >=
57 greater or equal than
59 method:: <=
60 smaller or equal than
62 method:: lcm
63 Least common multiple
65 method:: gcd
66 Greatest common divisor
68 method:: round
69 Round to multiple of aNumber
70 method:: roundUp
71 round up to a multiply of aNumber
73 method:: thresh
75 method:: min
76 Minimum
78 method:: max
79 Maximum
81 method:: wrap2
83 method:: trunc
84 Truncate to multiple of aNumber
86 method:: atan2
87 Arctangent of (this/aNumber)
89 method:: hypot
90 Square root of the sum of the squares.
93 method:: log
94 returns:: Base e logarithm.
96 method:: log2
97 returns:: Base 2 logarithm.
99 method:: log10
100 returns:: Base 10 logarithm.
102 method:: neg
103 returns:: negation
105 method:: abs
106 returns:: absolute value.
108 method:: sign
109 returns:: Answer -1 if negative, +1 if positive or 0 if zero.
111 method:: ceil
112 returns:: next larger integer.
114 method:: floor
115 returns:: next smaller integer
117 method:: sin
118 Sine
120 method:: cos
121 Cosine
123 method:: tan
124 Tangent
126 method:: asin
127 Arcsine
129 method:: acos
130 Arccosine
132 method:: atan
133 Arctangent
135 method:: sinh
136 Hyperbolic sine
138 method:: cosh
139 Hyperbolic cosine
141 method:: tanh
142 Hyperbolic tangent
144 method:: frac
145 fractional part
147 method:: squared
148 the square of the number
150 method:: cubed
151 the cube of the number
153 method:: sqrt
154 the square root of the number.
156 method:: exp
157 e to the power of the receiver.
159 method:: reciprocal
160 1 / this
162 method:: pow
163 this to the power of aNumber
165 method:: fold2
166 the folded value, a bitwise or with aNumber
168 method:: previousPowerOf
169 the number relative to this that is the previous power of aNumber
171 method:: nextPowerOf
172 the next power of aNumber
174 method:: nextPowerOfTwo
175 returns:: the number relative to this that is the next power of 2
177 method:: nextPowerOfThree
178 the next power of three
180 method:: hash
181 returns:: a hash value
183 method:: <!
184 returns:: the receiver. aNumber is ignored.
186 method:: &
187 Bitwise And
189 method::|
190 Bitwise Or
192 method:: bitXor
193 Bitwise Exclusive Or
195 method:: bitHammingDistance
196 Binary Hamming distance: the count of bits that are not the same in the two numbers
198 method:: bitTest
199 returns:: true if bit at index aNumber is set.
201 method:: bitNot
202 returns:: ones complement
204 method:: <<
205 Binary shift left.
207 method:: >>
208 Binary shift right.
210 method:: +>>
211 Unsigned binary shift right.
213 method:: rightShift
214 returns:: performs a binary right shift
216 method:: unsignedRightShift
217 returns:: performs an unsigned right shift
219 method:: leftShift
220 returns:: performs a binary left shift
222 method:: bitOr
223 returns:: performs a bitwise or with aNumber
225 method:: bitAnd
226 returns:: performs a bitwise and with aNumber
228 method:: ring1
229 (a * b) + a
231 method:: ring2
232 ((a*b) + a + b)
234 method:: ring3
235 (a * a *b)
237 method:: ring4
238 ((a*a *b) - (a*b*b))
240 method:: difsqr
241 (a*a) - (b*b)
243 method:: sumsqr
244 (a*a) + (b*b)
246 method:: sqrdif
247 (a - b) ** 2
249 method:: sqrsum
250 (a + b) ** 2
252 method:: absdif
253 (a - b).abs
255 method:: amclip
256 0  when  b <= 0,  a*b  when  b > 0
258 method:: scaleneg
259 a * b when a < 0, otherwise a.
261 method:: clip2
262 clips receiver to +/- aNumber
264 method:: excess
265 Returns the difference of the receiver and its clipped form.
266 discussion::
267 code::
268 (a - clip2(a,b)).
271 subsection:: testing
272 method:: isPositive
273 Answer if the number is >= 0.
275 method:: isNegative
276 Answer if the number is < 0.
278 method:: isStrictlyPositive
279 Answer if the number is > 0.
281 method:: booleanValue
282 returns:: true, if strictly positive ( > 0), otherwise false (see link::Classes/Boolean::)
284 method:: isNaN
285 method:: ==
287 subsection:: conversion
289 method:: asFraction
290 argument::denominator
291 argument::fasterBetter
292 if true, asFraction may find a much closer approximation and do it faster.
293 returns:: an array of denominator and divisor of the nearest and smallest fraction
295 method:: asAudioRateInput
296 Converts this into an audiorate input.
298 method:: asTimeString
299 Compile a time string.
300 argument:: precision
301 0.1 by default
302 returns:: a string corresponding to the hours:minutes:seconds based on the receiver as number of seconds
303 discussion::
304 code::
306 var start;
307 start = Main.elapsedTime;
308 { loop({(Main.elapsedTime - start).asTimeString.postln; 0.05.wait}) }.fork;
312 method:: asPoint
313 returns:: this as link::Classes/Point::. x = y = this.
315 method:: asComplex
316 returns:: this as link::Classes/Point::. x = y = this.
318 method:: asWarp
319 argument::spec a link::Classes/ControlSpec::
320 returns:: this as link::Classes/CurveWarp:: according to spec.
322 method:: asFloat
323 returns:: this as link::Classes/Float::
325 method:: asRect
326 returns:: a link::Classes/Rect:: with x = y = w = h = this.
328 method:: asBoolean
329 returns:: this as a link::Classes/Boolean::.  this > 0
331 method:: asQuant
332 returns:: the values as link::Classes/Quant::
334 method:: asInteger
335 returns:: this as link::Classes/Integer::
337 subsection:: timing
339 method::wait
340 within a routine, yield the number so that the clock can wait for this many beats. Outside a Routine, this trows an error (see also Routine for details).
342 discussion::
343 Create a routine by a function fork
344 code::
346 fork {
347         1.wait;
348         "I did wait".postln;
349         1.0.rand.wait;
350         "No you didn't".postln;
351         2.wait;
352         (1..).do { |i|
353                 "yes I did".postln;
354                 i.asFloat.rand.wait;
355                 "no you didn't".postln;
356                 i.wait
357         }
362 method:: waitUntil
363 like wait, only specify a time (measured in beats of the current thread's clock). Outside a Routine, this trows an error (see also Routine for details).
365 method:: sleep
366 make the current thread sleep, until woken up by re-scheduling. Outside a Routine, this trows an error (see also Routine for details).
368 method:: nextTimeOnGrid
369 argument::clock
370 returns:: the next possible multiple of the clock's beats.
372 method:: schedBundleArrayOnClock
376 subsection:: series and arrays
378 method:: nearestInList
379 returns:: the value in the list closest to this
381 discussion::
382 code::
384 l = [0, 0.5, 0.9, 1];
385 (0, 0.05..1).collect { |i| i.nearestInList(l) }
389 method:: nearestInScale
390 argument:: scale
391 an array of SimpleNumbers each treated as a step in the octave.
392 argument:: stepsPerOctave
393 12 by default
394 returns:: the value in the collection closest to this, assuming an octave repeating table of note values.
396 discussion::
397 code::
399 l = [0, 1, 5, 9, 11]; // pentatonic scale
400 (60, 61..76).collect { |i| i.nearestInScale(l, 12) }
404 method:: series
405 return an artithmetic series from this over second to last.
406 discussion::
407 This is used in the shortcuts:
408 code::
409 (0..100);
410 (1, 3 .. 17)
412 If second is nil, it is one magnitude step towards last (1 or -1).
413 Examples:
414 code::
415 series(5, 7, 10);
416 series(5, nil, 10);
417 (5, 7 .. 10)
420 method:: seriesIter
421 returns:: a Routine that iterates over the numbers from this to last.
423 discussion::
424 Since this is a lazy operation, last may be inf, generating an endless series
425 (see also link::Guides/ListComprehensions::)
426 code::
427 r = seriesIter(0, 5);
428 r.nextN(8);
429 r.nextN(8);
433 subsection:: windowing
435 method:: rectWindow
436 returns:: a value for a rectangular window function between 0 and 1.
438 method:: hanWindow
439 returns:: a value for a hanning window function between 0 and 1.
441 method:: welWindow
442 returns:: a value for a welsh window function between 0 and 1.
444 method:: triWindow
445 returns:: a value for a triangle window function between 0 and 1.
447 subsection:: mapping
449 method:: distort
450 a nonlinear distortion function.
452 method:: softclip
453 Distortion with a perfectly linear region from -0.5 to +0.5
455 method:: scurve
456 Map receiver in the onto an S-curve.
457 discussion::
458 code::
459 ((0..100) / 100 ).collect(_.scurve).plot
462 method:: ramp
463 Map receiver onto a ramp starting at 0.
464 discussion::
465 code::
466 ((-100..100) / 100 ).collect(_.ramp).plot
469 method::magnitude
470 returns:: abolute value (see link::Classes/Polar::, link::Classes/Complex::)
472 method::angle
473 returns:: angle of receiver conceived as link::Classes/Polar:: or link::Classes/Complex:: number.
476 method:: degreeToKey
477 argument:: scale
478 an array of SimpleNumbers each treated as a step in the octave.
480 discussion::
481 the value is truncated to an integer and used as an index into an octave repeating table of note values. Indices wrap around the table and shift octaves as they do.
483 code::
485 l = [0, 1, 5, 9, 11]; // pentatonic scale
486 (1, 2..15).collect{|i|
487         i.degreeToKey(l, 12)
492 method:: keyToDegree
493 inverse of degreeToKey.
494 argument:: scale
495 an array of SimpleNumbers each treated as a step in the octave.
498 discussion::
499 code::
501 l = [0, 1, 5, 9, 11]; // pentatonic scale
502 (60, 61..75).collect { |i| i.keyToDegree(l, 12) }
505 code::
507 l = [0, 1, 5, 9, 11]; // pentatonic scale
508 (60, 61..75).postln.collect { |i| i.keyToDegree(l, 12).degreeToKey(l) }
514 method::gaussCurve
515 map the receiver onto a gauss function.
517 discussion::
518 Uses the formula:
519 code::
520 a * (exp(squared(this - b) / (-2.0 * squared(c)))) Defalt values: a = 1; b = 0; c = 1
522 Example code
523 code::
524 (0..1000).normalize(-10, 10).collect { |num| num.gaussCurve }.plot;
528 method:: equalWithPrecision
529 returns:: true if receiver is closer to that than precision.
531 discussion::
532 code::
533 3.1.equalWithPrecision(3.0, 0.05); // false
534 3.1.equalWithPrecision(3.0, 0.1); // false
535 3.1.equalWithPrecision(3.0, 0.11); // true
538 method:: quantize
539 round the receiver to the quantum.
540 argument::tolerance
541 allowed tolerance.
542 argument::strength
543 Determines how much the value is allowed to differ in the tolerance range.
544 discussion::
545 code::
546 ((0..10) / 10).collect { |num| num.quantize(1, 0.3, 0.5) }.postcs.plot;
547 ((0..10) / 10).collect { |num| num.quantize(1, 0.6, 0.5) }.postcs.plot;
548 ((0..10) / 10).collect { |num| num.quantize(1, 1.0, 0.5) }.postcs.plot;
551 method:: linlin
552 map the receiver from an assumed linear input range to a linear output range. If the input exceeds the assumed input range, the behaviour is specified by the clip argument.
553 argument::inMin
554 assumed input minimum
555 argument::inMax
556 assumed input maximum
557 argument::outMin
558 output minimum
559 argument::outMax
560 output maximum
561 argument::clip
562 nil (don't clip)
563 \max (clip ceiling)
564 \min (clip floor)
565 \minmax (clip both - this is default).
567 discussion::
568 code::
569 (0..10).collect { |num| num.linlin(0, 10, -4.3, 100) };
570 (0..10).linlin(0, 10, -4.3, 100); // equivalent.
573 method::linexp
574 map the receiver from an assumed linear input range (inMin..inMax) to an exponential output range (outMin..outMax). The output range must not include zero. If the input exceeds the input range, the following behaviours are specified by the clip argument.
575 argument::inMin
576 assumed input minimum
577 argument::inMax
578 assumed input maximum
579 argument::outMin
580 output minimum
581 argument::outMax
582 output maximum
583 argument::clip
584 nil (don't clip)
585 \max (clip ceiling)
586 \min (clip floor)
587 \minmax (clip both - this is default).
588 discussion::
589 code::
590 (0..10).collect { |num| num.linexp(0, 10, 4.3, 100) };
591 (0..10).linexp(0, 10, 4.3, 100); // equivalent.
594 method::explin
595 map the receiver from an assumed exponential input range (inMin..inMax) to a linear output range (outMin..outMax). If the input exceeds the assumed input range. The input range must not include zero.
596 If the input exceeds the input range, the following behaviours are specified by the clip argument.
597 argument::inMin
598 assumed input minimum
599 argument::inMax
600 assumed input maximum
601 argument::outMin
602 output minimum
603 argument::outMax
604 output maximum
605 argument::clip
606 nil (don't clip)
607 \max (clip ceiling)
608 \min (clip floor)
609 \minmax (clip both - this is default).
610 discussion::
611 code::
612 (1..10).collect { |num| num.explin(0.1, 10, -4.3, 100) };
613 (1..10).explin(0.1, 10, -4.3, 100); // equivalent.
616 method::expexp
617 map the receiver from an assumed exponential input range (inMin..inMax) to an exponential output range (outMin..outMax). If the input exceeds the assumed input range. Both input range and output range must not include zero.
618 If the input exceeds the input range, the following behaviours are specified by the clip argument.
619 argument::inMin
620 assumed input minimum
621 argument::inMax
622 assumed input maximum
623 argument::outMin
624 output minimum
625 argument::outMax
626 output maximum
627 argument::clip
628 nil (don't clip)
629 \max (clip ceiling)
630 \min (clip floor)
631 \minmax (clip both - this is default).
632 discussion::
633 code::
634 (1..10).collect { |num| num.expexp(0.1, 10, 4.3, 100) };
635 (1..10).expexp(0.1, 10, 4.3, 100); // equivalent.
638 method::lincurve
639 map the receiver from an assumed linear input range (inMin..inMax) to an exponential curve output range (outMin..outMax). A curve is like the curve parameter in Env. Unlike with linexp, the output range may include zero.
640 If the input exceeds the input range, the following behaviours are specified by the clip argument.
641 argument::inMin
642 assumed input minimum
643 argument::inMax
644 assumed input maximum
645 argument::outMin
646 output minimum
647 argument::outMax
648 output maximum
649 argument::clip
650 nil (don't clip)
651 \max (clip ceiling)
652 \min (clip floor)
653 \minmax (clip both - this is default).
654 discussion::
655 code::
656 (0..10).collect { |num| num.lincurve(0, 10, -4.3, 100, -3) };
657 (0..10).lincurve(0, 10, -4.3, 100, -3); // equivalent.
659 code::
660 // different curves:
661 (-4..4).do { |val|
662         (0..100).collect(_.lincurve(0, 100, 0, 1, val)).plot
666 method::curvelin
667 map the receiver from an assumed curve-exponential input range (inMin..inMax) to a linear output range (outMin..outMax). If the input exceeds the assumed input range. A curve is like the curve parameter in Env. Unlike with explin, the input range may include zero. If the input exceeds the input range, the following behaviours are specified by the clip argument.
668 argument::inMin
669 assumed input minimum
670 argument::inMax
671 assumed input maximum
672 argument::outMin
673 output minimum
674 argument::outMax
675 output maximum
676 argument::clip
677 nil (don't clip)
678 \max (clip ceiling)
679 \min (clip floor)
680 \minmax (clip both - this is default).
682 discussion::
683 code::
684 (1..10).collect { |num| num.curvelin(0, 10, -4.3, 100, -3) };
685 (1..10).curvelin(0, 10, -4.3, 100, -3); // equivalent.
687 code::
688 // different curves:
689 (-4..4).do { |val|
690         (0..100).collect(_.curvelin(0, 100, 0, 1, val)).plot
694 method::bilin
695 map the receiver from two assumed linear input ranges (inMin..inCenter) and (inCenter..inMax) to two linear output ranges (outMin..outCenter) and (outCenter..outMax). If the input exceeds the input range, the following behaviours are specified by the clip argument.
696 argument::inCenter
697 argument::inMin
698 assumed input minimum
699 argument::inMax
700 assumed input maximum
701 argument::outCenter
702 argument::outMin
703 output minimum
704 argument::outMax
705 output maximum
706 argument::clip
707 nil (don't clip)
708 \max (clip ceiling)
709 \min (clip floor)
710 \minmax (clip both - this is default).
711 discussion::
712 code::
713 var center = 0.5, ctlCenter;
714 w = Window("bilin", Rect(100, 100, 200, 100)).front;
715 a = Slider(w, Rect(20, 20, 150, 20)).value_(0.5);
716 b = Slider(w, Rect(20, 45, 150, 20)).value_(0.5);
717 b.action = { center = b.value };
718 a.mouseDownAction = { ctlCenter = a.value };
719 a.action = {
720         b.value = a.value.bilin(ctlCenter, 0, 1, center, 0, 1);
725 method::biexp
726 map the receiver from two assumed exponential input ranges (inMin..inCenter) and (inCenter..inMax) to two linear output ranges (outMin..outCenter) and (outCenter..outMax). The input range must not include zero. If the input exceeds the input range, the following behaviours are specified by the clip argument.
727 argument::inCenter
728 argument::inMin
729 assumed input minimum
730 argument::inMax
731 assumed input maximum
732 argument::outCenter
733 argument::outMin
734 output minimum
735 argument::outMax
736 output maximum
737 argument::clip
738 nil (don't clip)
739 \max (clip ceiling)
740 \min (clip floor)
741 \minmax (clip both - this is default).
743 discussion::
744 code::
745 // doesn't properly work yet.
747 var center = 0.5, ctlCenter;
748 w = Window("biexp", Rect(100, 100, 200, 100)).front;
749 a = Slider(w, Rect(20, 20, 150, 20)).value_(0.5);
750 b = Slider(w, Rect(20, 45, 150, 20)).value_(0.5);
751 b.action = { center = b.value };
752 a.mouseDownAction = { ctlCenter = a.value + 0.05 };
753 a.action = {
754         b.value = (a.value + 0.1).biexp(ctlCenter, 0.1, 1.1, center, 0, 1);
759 method::lcurve
760 map the receiver onto an L-curve.
762 discussion::
763 Uses the formula
764 code::
765 a * (m * exp(x) * rTau + 1) / (n * exp(x) * rTau + 1)
767 This is used for smoothing values and limiting them to a range.
768 code::
769 (0..1000).normalize(-10, 10).collect { |num| num.lcurve }.plot;
773 method:: degrad
774 returns:: converts degree to radian
776 method:: raddeg
777 returns:: converts radian to degree
779 method:: midicps
780 Convert MIDI note to cycles per second
781 returns:: cycles per second
783 method:: cpsmidi
784 Convert cycles per second to MIDI note.
785 returns:: midi note
788 method:: midiratio
789 Convert an interval in semitones to a ratio.
790 returns:: a ratio
792 method:: ratiomidi
793 Convert a ratio to an interval in semitones.
794 returns:: an interval in semitones
796 method:: ampdb
797 Convert a linear amplitude to decibels.
800 method:: dbamp
801 Convert a decibels to a linear amplitude.
803 method:: octcps
804 Convert decimal octaves to cycles per second.
806 method:: cpsoct
807 Convert cycles per second to decimal octaves.
810 subsection:: streams
812 method:: storeOn
813 stores this on the given stream
814 method:: printOn
815 printrs this on the given stream
817 subsection:: random
819 method:: coin
820 Answers a Boolean which is the result of a random test whose probability of success in a range from zero to one is this.
822 method:: rand
823 returns:: Random number from zero up to the receiver, exclusive.
825 method:: rand2
826 returns:: a random number from -this to +this.
828 method:: rrand
829 returns:: a random number in the interval ]a, b[.
830 argument::aNumber
831 the upper limit
832 discussion::
833 If both a and b are link::Classes/Integer:: then the result will be an link::Classes/Integer::.
835 method:: linrand
836 returns:: a linearly distributed random number from zero to this.
838 method:: bilinrand
839 returns:: Bilateral linearly distributed random number from -this to +this.
841 method:: sum3rand
842 This was suggested by Larry Polansky as a poor man's gaussian.
843 returns:: A random number from -this to +this that is the result of summing three uniform random generators to yield a bell-like distribution.
845 method:: exprand
846 an exponentially distributed random number in the interval ]a, b[. This is always a link::Classes/Float::.
847 argument::aNumber
848 the upper limit
850 method:: gauss
851 a gaussian distributed random number.
852 argument::aNumber
853 the upper limit
854 discussion::
855 Always returns a link::Classes/Float::.
856 code::
857 (0..1000).collect { |num| gauss(0.0, num) }.plot;
860 method:: partition
861 randomly partition a number into parts of at least min size.
862 argument:: parts
863 number of parts
864 argument:: min
865 the minimum size
867 discussion::
868 code::
869 75.partition(8, 3);
870 75.partition(75, 1);
874 subsection:: misc
876 method:: rate
877 method::isValidUGenInput
878 returns:: false if receiver cannot be used in UGen.
879 method:: performBinaryOpOnSignal (aSelector, aSignal)
880 method:: performBinaryOpOnComplex
882 method:: performBinaryOpOnSimpleNumber
883 method:: hypotApx
884 method:: fontID
885 method:: playAndDelta
886 method:: numChannels
887 method:: asBufWithValues
888 method:: firstArg