-
Notifications
You must be signed in to change notification settings - Fork 7
/
README
324 lines (205 loc) · 9.07 KB
/
README
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
js-numbers: a Javascript implementation of Scheme's numeric tower
Developer:
Danny Yoo (dyoo@cs.wpi.edu)
License:
BSD
Summary: js-numbers implements the "numeric tower" commonly associated
with the Scheme language. The operations in this package
automatically coerse between fixnums, bignums, rationals, floating
point, and complex numbers.
Contributors: I want to thank the following people:
Zhe Zhang
Ethan Cecchetti
Ugur Cekmez
Other sources:
The bignum implementation (content from jsbn.js and jsbn2.js) used
in js-numbers comes from Tom Wu's JSBN library at:
http://www-cs-students.stanford.edu/~tjw/jsbn/
======================================================================
WARNING WARNING
This package is currently being factored out of an existing project,
Moby-Scheme. As such, the code here is in major flux, and this is
nowhere near ready from public consumption yet. We're still in the
middle of migrating over the test cases from Moby-Scheme over to this
package, and furthermore, I'm taking the time to redo some of the
implementation. So this is going to be buggy for a bit. Use at your
own risk.
======================================================================
Examples
[fill me in]
======================================================================
API
Loading js-numbers.js will define a toplevel namespace called
jsnums
which contains following constants and functions:
pi: scheme-number
e: scheme-number
nan: scheme-number
Not-A-Number
inf: scheme-number
infinity
negative_inf: scheme-number
negative infinity
negative_zero: scheme-number
The value -0.0.
zero: scheme-number
one: scheme-number
negative_one: scheme-number
i: scheme-number
The square root of -1.
negative_i: scheme-number
The negative of i.
fromString: string -> (scheme-number | false)
Convert from a string to a scheme-number. If we find the number is malformed,
returns false.
fromFixnum: javascript-number -> scheme-number
Convert from a javascript number to a scheme number. If the
number looks like an integer, represents as an exact integer.
Otherwise, represents as a float. If you need more precision over
the representation, use makeFloat or makeRational instead.
makeRational: javascript-number javascript-number? -> scheme-number
Low level constructor: Constructs a rational with the given
numerator and denominator. If only one argument is given, assumes
the denominator is 1. The numerator and denominator must be
integers.
makeFloat: javascript-number -> scheme-number
Low level constructor: constructs a floating-point number.
makeBignum: string -> scheme-number
Low level constructor: constructs a bignum.
makeComplex: scheme-number scheme-number? -> scheme-number
Constructs a complex number; the real and imaginary parts of the
input must be basic scheme numbers (i.e. not complex). If only one
argument is given, assumes the imaginary part is 0.
makeComplexPolar: scheme-number scheme-number -> scheme-number
Constructs a complex number; the radius and theta must be basic
scheme numbers (i.e. not complex).
isSchemeNumber: any -> boolean
Produces true if the thing is a scheme number.
isRational: scheme-number -> boolean
Produces true if the number is rational.
isReal: scheme-number -> boolean
Produces true if the number is a real.
isExact: scheme-number -> boolean
Produces true if the number is being represented exactly.
isInexact: scheme-number -> boolean
Produces true if the number is inexact.
isInteger: scheme-number -> boolean
Produces true if the number is an integer.
toFixnum: scheme-number -> javascript-number
Produces the javascript number closest in interpretation to the
given scheme-number.
toExact: scheme-number -> scheme-number
Converts the number to an exact scheme-number.
toInexact: scheme-number -> scheme-number
Converts the number to an inexact scheme-number.
add: scheme-number scheme-number -> scheme-number
Adds the two numbers together.
subtract: scheme-number scheme-number -> scheme-number
Subtracts the first number from the second.
mulitply: scheme-number scheme-number -> scheme-number
Multiplies the two numbers together.
divide: scheme-number scheme-number -> scheme-number
Divides the first number by the second.
equals: scheme-number scheme-number -> boolean
Produces true if the two numbers are equal.
eqv: scheme-number scheme-number -> boolean
Produces true if the two numbers are equivalent.
approxEquals: scheme-number scheme-number scheme-number -> boolean
Produces true if the two numbers are approximately equal, within the
bounds of the third argument.
greaterThanOrEqual: scheme-number scheme-number -> boolean
Produces true if the first number is greater than or equal to the second.
lessThanOrEqual: scheme-number scheme-number -> boolean
Produces true if the first number is less than or equal to the second.
greaterThan: scheme-number scheme-number -> boolean
Produces true if the first number is greater than the second.
lessThan: scheme-number scheme-number -> boolean
Produces true if the first number is less than the second.
expt: scheme-number scheme-number -> scheme-number
Produces the first number exponentiated to the second number.
exp: scheme-number -> scheme-number
Produces e exponentiated to the given number.
modulo: scheme-number scheme-number -> scheme-number
Produces the modulo of the two numbers.
numerator: scheme-number -> scheme-number
Produces the numerator of the rational number.
denominator: scheme-number -> scheme-number
Produces the denominator of the rational number.
quotient: scheme-number scheme-number -> scheme-number
Produces the quotient. Both inputs must be integers.
remainder: scheme-number scheme-number -> scheme-number
Produces the remainder. Both inputs must be integers.
sqrt: scheme-number -> scheme-number
Produces the square root.
abs: scheme-number -> scheme-number
Produces the absolute value.
floor: scheme-number -> scheme-number
Produces the floor.
round: scheme-number -> scheme-number
Produces the number rounded to the nearest integer.
ceiling: scheme-number -> scheme-number
Produces the ceiling.
conjugate: scheme-number -> scheme-number
Produces the complex conjugate.
magnitude: scheme-number -> scheme-number
Produces the complex magnitude.
log: scheme-number -> scheme-number
Produces the natural log (base e) of the given number.
angle: scheme-number -> scheme-number
Produces the complex angle.
cos: scheme-number -> scheme-number
Produces the cosine.
sin: scheme-number -> scheme-number
Produces the sin.
tan: scheme-number -> scheme-number
Produces the tangent.
asin: scheme-number -> scheme-number
Produces the arc sine.
acos: scheme-number -> scheme-number
Produces the arc cosine.
atan: scheme-number -> scheme-number
Produces the arc tangent.
cosh: scheme-number -> scheme-number
Produces the hyperbolic cosine.
sinh: scheme-number -> scheme-number
Produces the hyperbolic sine.
realPart: scheme-number -> scheme-number
Produces the real part of the complex number.
imaginaryPart: scheme-number -> scheme-number
Produces the imaginary part of the complex number.
sqr: scheme-number -> scheme-number
Produces the square.
integerSqrt: scheme-number -> scheme-number
Produces the integer square root.
gcd: scheme-number [scheme-number ...] -> scheme-number
Produces the greatest common divisor.
lcm: scheme-number [scheme-number ...] -> scheme-number
Produces the least common mulitple.
toRepeatedDecimal: scheme-number scheme-number {limit: number}? -> [string, string, string]
Produces a string representation of the decimal expansion; the first and second
argument must be integers. Returns an array of three parts: the portion before
the decimal, the non-repeating part, and then the repeating part.
If the expansion goes beyond the limit (by default, 256 characters), then
the expansion will be cut off, and the third portion will be '...'.
======================================================================
Test suite
Open tests/index.html, which should run our test suite over all the
public functions in js-numbers.
If you notice a good test case is missing, please let the developer
know, and we'll be happy to add it in.
======================================================================
TODO
* Absorb implementations of:
atan2, cosh, sinh, sgn
* Add real documentation.
======================================================================
Related work
There appears to be another Scheme numeric tower implementation that
just came out in the last month or so, by Matt Might and John Tobey:
https://github.com/jtobey/javascript-bignum
http://silentmatt.com/biginteger/
======================================================================
History
February 2010: initial refactoring from the moby-scheme source tree.
June 2010: got implementation of integer-sqrt from Ugur Cekmez;
brought in some fixes from Ethan Cecchetti.