File: Float.schelp

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,292 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (180 lines) | stat: -rw-r--r-- 4,827 bytes parent folder | download | duplicates (2)
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
Class:: Float
summary:: 64-bit Floating point number
categories:: Math

description::
A 64-bit double precision floating point number. Float inherits most of its behaviour from its superclass.

code::
// generate 10 random floats between 0 and 1
{ 1.0.rand }.dup(10)

// Pythagorean comma
// expressed as a floating point number resulting from calculations with integers
(
var apotome = (3 ** 7) / (2 ** 11);
var limma =   (2 ** 8) / (3 ** 5);

apotome / limma
)
::

Note that despite its name, link::Classes/FloatArray:: only holds 32-bit (single precision) floats.
For a raw array of 64-bit floats, use link::Classes/DoubleArray::.

ClassMethods::

method:: from32Bits
returns:: a new Float from a 32-bit word.

method:: from64Bits
returns:: a new Float from a 64-bit word.

InstanceMethods::

method:: do
iterates a link::Classes/Function:: from code::0:: to code::this-1::. See also: link::Classes/Integer#-do::, link::Classes/Collection#-do::
argument:: function
The function to iterate.

method:: reverseDo
iterates function from this-1 to 0
argument:: function
The function to iterate.

method:: clip
Return this if lo <= this <= hi, otherwise return the nearest boundary: lo if this < lo, hi if this > hi.
argument:: lo
The low threshold of clipping.
argument:: hi
The high threshold of clipping.

method:: fold
Fold this to [lo, hi].
argument:: lo
The low threshold of folding.
argument:: hi
The high threshold of folding.

method:: wrap
Wrap this around [lo, hi) such that it falls in range. Equivalent to (this % (hi - lo)) + lo.
argument:: lo
The low threshold (inclusive) of wrapping.
argument:: hi
The high threshold (exclusive) of wrapping.

method:: coin
Let emphasis::x:: be the receiver clipped to the range [0, 1]. With probability emphasis::x::, return true. With probability 1 - emphasis::x::, return false.

returns:: a link::Classes/Boolean::
discussion::
code::
0.2.coin; // 20 % chance for true.
::
See also: link::Guides/Randomness::

method::xrand2
returns::a random float from this.neg to this, excluding the value exclude.

method:: isFloat
returns:: code::true:: since this is a Float.

method:: asFloat
returns:: code::this:: since this is a Float.

method:: as32Bits
returns:: an Integer which is the bit pattern of this as a 32bit single precision float

method:: high32Bits
returns:: an Integer which is the bit pattern of high 32-bits of the 64-bit double precision floating point value

method:: low32Bits
returns:: an Integer which is the bit pattern of high 32-bits of the 64-bit double precision floating point value

method:: asCompileString
returns:: a string that when interpreted matches the receiver, if the number is within the range given in code::storeOn::.

code::
a = 2.81882773638;
a.asCompileString.interpret == a;
::

method:: asStringPrec
Returns a string representation of the number, with the desired precision (i.e. number of significant figures).
discussion::
code::
// example:
pi
pi.asStringPrec(3)
pi.asStringPrec(6)
(pi * 0.0001).asStringPrec(3)
7.4.asStringPrec(5)
7.4.asStringPrec(50)
::

subsection::Using Floats as replacement for Integers

In SuperCollider, Floats are 64-bit wide. Because  an link::Classes/Integer::  is 32-bit, it can only capture integers in the range code::-2147483648 .. +2147483647::, or about code:: 2 x 10^9 ::.

Therefore, in some situations it can be useful to calculate with floats also when only whole numbers are needed. You can use 64-bit floats for integer calculations up to code::± 9007199254740992:: (code::2^53::, or about code::9 x 10^15::). Sometimes one can go even further (see example below).


code::
// compare factorial:
f = { |x| if(x < 2) { x } { x * f.(x - 1) } };
f.(14); // integer
f.(18) // integer overflow: -898433024
f.(18.0) // float is ok.
// check the ratio between adjacent factorials:
f.(18.0) / f.(17.0) == 18 // true
// 18 is already the largest possible factorial representable in 64-bit float (< 2^53)
{:x, x<-(1.0..40), f.(x) < (2 ** 53) }.all.last
::


Here is a classical example for an algorithm:

code::
// euclidean algorithm
(
g = { |a, b|
	if(b == 0) {
		a
	} {
		g.(b, mod(a, b))
	}
}
)

// check if a power of two

x = 2147483647 * 3;
g.(x, 3); // wrong, returns 1
x = 2147483647.0 * 3;
g.(x, 3); // correct, returns 3

x = 2007199254740992.0 * 3;
g.(x, 3); // correct, returns 3
x = 9007199254740992.0 * 3;
g.(x, 3); // still happens to be correct, but better not count on it …
::



Testing the limits of 64-bit float (2^53)

code::
a = 2 ** 53 - 1
b = a + 1;
c = a + 2;
b - a // correct (1)
c - a // incorrect (also 1)


// How high you can go depends on the calculation:
// here we divide two numbers that follow each other
// and it is correct up to f.(170), about 7.25e+306.
f = { |x| if(x < 2) { x } { x * f.(x - 1) } };
{:x, x<-(1.0..180), f.(x) / f.(x - 1) == x }.all.last

::