ruby-calc 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rubocop.yml +52 -0
- data/.rubocop_todo.yml +11 -0
- data/.travis.yml +15 -0
- data/.yardopts +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +497 -0
- data/Rakefile +23 -0
- data/bin/console +10 -0
- data/bin/install_calc.sh +18 -0
- data/bin/makefile.patch +48 -0
- data/bin/setup +7 -0
- data/bin/todo.rb +374 -0
- data/ext/calc/c.c +775 -0
- data/ext/calc/calc.c +192 -0
- data/ext/calc/calc.h +71 -0
- data/ext/calc/config.c +239 -0
- data/ext/calc/convert.c +193 -0
- data/ext/calc/extconf.rb +29 -0
- data/ext/calc/math_error.c +72 -0
- data/ext/calc/numeric.c +623 -0
- data/ext/calc/q.c +2755 -0
- data/lib/calc.rb +214 -0
- data/lib/calc/c.rb +371 -0
- data/lib/calc/import.rb +6 -0
- data/lib/calc/numeric.rb +208 -0
- data/lib/calc/q.rb +628 -0
- data/lib/calc/version.rb +3 -0
- data/ruby-calc.gemspec +29 -0
- metadata +167 -0
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require "rake/extensiontask"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
task build: :compile
|
7
|
+
|
8
|
+
Rake::ExtensionTask.new("calc") do |ext|
|
9
|
+
ext.lib_dir = "lib/calc"
|
10
|
+
end
|
11
|
+
|
12
|
+
Rake::TestTask.new do |t|
|
13
|
+
t.pattern = "test/test_*.rb"
|
14
|
+
t.libs << "test"
|
15
|
+
end
|
16
|
+
|
17
|
+
task test: :compile
|
18
|
+
task default: :test
|
19
|
+
|
20
|
+
task :indent do
|
21
|
+
system("indent -kr -l95 -nut -nce -psl ext/calc/*.[hc]")
|
22
|
+
system("rm ext/calc/*.[hc]~")
|
23
|
+
end
|
data/bin/console
ADDED
data/bin/install_calc.sh
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#! /bin/sh
|
2
|
+
# installs calc into /usr/local
|
3
|
+
|
4
|
+
set -x
|
5
|
+
|
6
|
+
version=calc-${CALC_VERSION-2.12.5.4}
|
7
|
+
patch=$PWD/$(dirname $0)/makefile.patch
|
8
|
+
|
9
|
+
cd /tmp
|
10
|
+
wget -nv http://www.isthe.com/chongo/src/calc/${version}.tar.bz2
|
11
|
+
tar xjf ${version}.tar.bz2
|
12
|
+
cd $version
|
13
|
+
patch -p0 < $patch
|
14
|
+
if [ "$CI" = "true" ]; then
|
15
|
+
make && sudo make install
|
16
|
+
else
|
17
|
+
make && make check && sudo make install
|
18
|
+
fi
|
data/bin/makefile.patch
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- Makefile.orig 2015-11-06 17:52:22.984273873 +1030
|
2
|
+
+++ Makefile 2015-11-06 18:00:02.632273873 +1030
|
3
|
+
@@ -610,25 +610,25 @@
|
4
|
+
# LIBDIR= /usr/lib
|
5
|
+
# CALC_SHAREDIR= /usr/share/calc
|
6
|
+
#
|
7
|
+
-#BINDIR= /usr/local/bin
|
8
|
+
+BINDIR= /usr/local/bin
|
9
|
+
#BINDIR= /dev/env/DJDIR/bin
|
10
|
+
-BINDIR= /usr/bin
|
11
|
+
+#BINDIR= /usr/bin
|
12
|
+
|
13
|
+
-#LIBDIR= /usr/local/lib
|
14
|
+
+LIBDIR= /usr/local/lib
|
15
|
+
#LIBDIR= /dev/env/DJDIR/lib
|
16
|
+
-LIBDIR= /usr/lib
|
17
|
+
+#LIBDIR= /usr/lib
|
18
|
+
|
19
|
+
-#CALC_SHAREDIR= /usr/local/lib/calc
|
20
|
+
+CALC_SHAREDIR= /usr/local/lib/calc
|
21
|
+
#CALC_SHAREDIR= /dev/env/DJDIR/share/calc
|
22
|
+
-CALC_SHAREDIR= /usr/share/calc
|
23
|
+
+#CALC_SHAREDIR= /usr/share/calc
|
24
|
+
|
25
|
+
# NOTE: Do not set CALC_INCDIR to /usr/include or /usr/local/include!!!
|
26
|
+
# Always be sure that the CALC_INCDIR path ends in /calc to avoid
|
27
|
+
# conflicts with system or other application include files!!!
|
28
|
+
#
|
29
|
+
-#CALC_INCDIR= /usr/local/include/calc
|
30
|
+
+CALC_INCDIR= /usr/local/include/calc
|
31
|
+
#CALC_INCDIR= /dev/env/DJDIR/include/calc
|
32
|
+
-CALC_INCDIR= ${INCDIR}/calc
|
33
|
+
+#CALC_INCDIR= ${INCDIR}/calc
|
34
|
+
|
35
|
+
# By default, these values are based CALC_SHAREDIR, INCDIR, BINDIR
|
36
|
+
# ---------------------------------------------------------------
|
37
|
+
@@ -696,9 +696,9 @@
|
38
|
+
# Use MANDIR= to disable installation of the calc man (source) page.
|
39
|
+
#
|
40
|
+
#MANDIR=
|
41
|
+
-#MANDIR= /usr/local/man/man1
|
42
|
+
+MANDIR= /usr/local/man/man1
|
43
|
+
#MANDIR= /usr/man/man1
|
44
|
+
-MANDIR= /usr/share/man/man1
|
45
|
+
+#MANDIR= /usr/share/man/man1
|
46
|
+
#MANDIR= /dev/env/DJDIR/man/man1
|
47
|
+
#MANDIR= /usr/man/u_man/man1
|
48
|
+
#MANDIR= /usr/contrib/man/man1
|
data/bin/setup
ADDED
data/bin/todo.rb
ADDED
@@ -0,0 +1,374 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "calc"
|
5
|
+
|
6
|
+
q = Calc::Q(1)
|
7
|
+
c = Calc::C(1, 1)
|
8
|
+
all = [Calc, q, c]
|
9
|
+
rat = [Calc, q]
|
10
|
+
builtins = Calc::Q(0)
|
11
|
+
builtins_done = Calc::Q(0)
|
12
|
+
exclude = [:singleton_method_added, :to_bn]
|
13
|
+
|
14
|
+
# this is a list of all calc builtin functions which we want to implement.
|
15
|
+
# doesn't include functions where there is a ruby way to do it already (files,
|
16
|
+
# strings, arrays, hashes, matrices).
|
17
|
+
# each array is a symbol for the method, then a list of things which should
|
18
|
+
# respond to it.
|
19
|
+
#
|
20
|
+
# rand/random related functions are also excluded as there are other ruby
|
21
|
+
# methods / libs for good random number generation.
|
22
|
+
[
|
23
|
+
[:abs, all],
|
24
|
+
# access - use File::Stat
|
25
|
+
[:acos, all],
|
26
|
+
[:acosh, all],
|
27
|
+
[:acot, all],
|
28
|
+
[:acoth, all],
|
29
|
+
[:acsc, all],
|
30
|
+
[:acsch, all],
|
31
|
+
[:agd, all],
|
32
|
+
# append - use Arrays
|
33
|
+
[:appr, all],
|
34
|
+
[:arg, all],
|
35
|
+
# argv - use ARGV
|
36
|
+
[:asec, all],
|
37
|
+
[:asech, all],
|
38
|
+
[:asin, all],
|
39
|
+
[:asinh, all],
|
40
|
+
# assoc - use {}
|
41
|
+
[:atan, all],
|
42
|
+
[:atan2, rat],
|
43
|
+
[:atanh, all],
|
44
|
+
[:avg, Calc],
|
45
|
+
# base - use Calc.config
|
46
|
+
# base2 - unlikely to implement as config item
|
47
|
+
[:bernoulli, rat],
|
48
|
+
[:bit, rat],
|
49
|
+
# blk - not sure why you'd need these in a ruby script
|
50
|
+
# blkcpy
|
51
|
+
# bklfree
|
52
|
+
# blocks
|
53
|
+
[:bround, all],
|
54
|
+
[:btrunc, rat],
|
55
|
+
# calc_tty
|
56
|
+
# calcleve
|
57
|
+
# calcpath
|
58
|
+
[:catalan, rat],
|
59
|
+
[:ceil, all],
|
60
|
+
[:cfappr, rat],
|
61
|
+
[:cfsim, rat],
|
62
|
+
[:char, rat],
|
63
|
+
# cmdbuf use ARGV
|
64
|
+
[:cmp, all],
|
65
|
+
[:comb, all],
|
66
|
+
# config - implement on Calc module
|
67
|
+
[:conj, all],
|
68
|
+
# copy - block stuff
|
69
|
+
[:cos, all],
|
70
|
+
[:cosh, all],
|
71
|
+
[:cot, all],
|
72
|
+
[:coth, all],
|
73
|
+
# count - use Array#count{} etc
|
74
|
+
# cp - use Vector#cross_product (from matrix lib)
|
75
|
+
[:csc, all],
|
76
|
+
[:csch, all],
|
77
|
+
# ctime - use Time.now / Time.now.ctime
|
78
|
+
# custom - n/a no plans to access custom compiled functions yet
|
79
|
+
# delete - use Array#delete
|
80
|
+
[:den, rat],
|
81
|
+
# det - use Matrix#det
|
82
|
+
[:digit, rat],
|
83
|
+
[:digits, rat],
|
84
|
+
# display - use Calc.config
|
85
|
+
# dp - use Vector#dot
|
86
|
+
# epsilon - use Calc.config
|
87
|
+
# errcount - only useful in command line calc
|
88
|
+
# errmax - only useful in command line calc
|
89
|
+
# errno - no libcalc interface
|
90
|
+
# error - use raise and ruby exceptions
|
91
|
+
[:estr, all],
|
92
|
+
[:euler, rat],
|
93
|
+
# eval - too hard for now :)
|
94
|
+
[:exp, all],
|
95
|
+
[:factor, rat],
|
96
|
+
[:fcnt, rat],
|
97
|
+
[:fib, rat],
|
98
|
+
# forall - use ruby loops
|
99
|
+
[:frem, rat],
|
100
|
+
[:fact, rat],
|
101
|
+
# fclose and other file related funcs: use File
|
102
|
+
# feof
|
103
|
+
# ferror
|
104
|
+
# fflush
|
105
|
+
# fgetc
|
106
|
+
# fgetfield
|
107
|
+
# fgetfile
|
108
|
+
# fgetline
|
109
|
+
# fgets
|
110
|
+
# fgetstr
|
111
|
+
# files
|
112
|
+
[:floor, rat],
|
113
|
+
# fopen
|
114
|
+
# fpathopen
|
115
|
+
# fprintf
|
116
|
+
# fputc
|
117
|
+
# fputs
|
118
|
+
# fputstr
|
119
|
+
# free - not necessary in ruby
|
120
|
+
[:freebernoulli, Calc],
|
121
|
+
[:freeeuler, Calc],
|
122
|
+
# freeglobals - not necessary
|
123
|
+
# freeredc - no REDC
|
124
|
+
# freestatics - not necessary
|
125
|
+
# freopen - more File stuff
|
126
|
+
# fscan
|
127
|
+
# fscanf
|
128
|
+
# fseek
|
129
|
+
# fsize
|
130
|
+
# ftell
|
131
|
+
[:frac, all],
|
132
|
+
[:gcd, rat],
|
133
|
+
[:gcdrem, rat],
|
134
|
+
[:gd, all],
|
135
|
+
# getenv - use ENV
|
136
|
+
# hash - not necessary
|
137
|
+
# head - use Array
|
138
|
+
[:highbit, rat],
|
139
|
+
[:hmean, Calc],
|
140
|
+
[:hnrmod, Calc],
|
141
|
+
[:hypot, rat],
|
142
|
+
[:ilog, all],
|
143
|
+
[:ilog10, all],
|
144
|
+
[:ilog2, all],
|
145
|
+
[:im, all],
|
146
|
+
# indices - use ruby Hash/Matrix
|
147
|
+
# inputlevel - calc / eval related
|
148
|
+
# insert - use Array
|
149
|
+
[:int, all],
|
150
|
+
[:inverse, all],
|
151
|
+
[:iroot, rat],
|
152
|
+
# mostly the is* don't make sense in ruby, since only numbers are mapped to
|
153
|
+
# ruby types. the ones we do have are named as predicates (iseven-> even?)
|
154
|
+
# isassoc
|
155
|
+
# isatty
|
156
|
+
# isblk
|
157
|
+
# isconfig
|
158
|
+
# isdefined
|
159
|
+
# iserror 1 where a value is an error
|
160
|
+
[:iseven, all],
|
161
|
+
# isfile
|
162
|
+
# ishash
|
163
|
+
# isident
|
164
|
+
[:isint, all],
|
165
|
+
# islist
|
166
|
+
# ismat
|
167
|
+
[:ismult, rat],
|
168
|
+
# isnull
|
169
|
+
# isnum
|
170
|
+
# isobj
|
171
|
+
# isobjtype
|
172
|
+
[:isodd, all],
|
173
|
+
# isoctet
|
174
|
+
[:isprime, rat],
|
175
|
+
# isptr
|
176
|
+
[:isqrt, rat],
|
177
|
+
# isrand
|
178
|
+
# israndom
|
179
|
+
[:isreal, rat],
|
180
|
+
[:isrel, rat],
|
181
|
+
# isstr
|
182
|
+
# issimple
|
183
|
+
[:issq, rat],
|
184
|
+
# istype
|
185
|
+
[:jacobi, rat],
|
186
|
+
# join - use Array#join
|
187
|
+
[:lcm, rat],
|
188
|
+
[:lcmfact, rat],
|
189
|
+
[:lfactor, rat],
|
190
|
+
# links - interesting but not necessary
|
191
|
+
# list - use Array
|
192
|
+
[:ln, all],
|
193
|
+
[:log, all],
|
194
|
+
[:lowbit, rat],
|
195
|
+
[:ltol, rat],
|
196
|
+
# makelist - use Array
|
197
|
+
# matdim, etc - use ruby Matrix library
|
198
|
+
# matfill
|
199
|
+
# matmax
|
200
|
+
# matmin
|
201
|
+
# matsum
|
202
|
+
# mattrace
|
203
|
+
# mattrans
|
204
|
+
[:max, Calc],
|
205
|
+
# memsize - not exposed by libcalc
|
206
|
+
[:meq, rat],
|
207
|
+
[:min, Calc],
|
208
|
+
[:minv, rat],
|
209
|
+
[:mmin, all],
|
210
|
+
[:mne, rat],
|
211
|
+
[:mod, all],
|
212
|
+
# modify - use Array/Matrix
|
213
|
+
# name - block stuff
|
214
|
+
[:near, rat],
|
215
|
+
# newerror - use ruby exceptions
|
216
|
+
[:nextcand, rat],
|
217
|
+
[:nextprime, rat],
|
218
|
+
[:norm, all],
|
219
|
+
# null - use ruby nil
|
220
|
+
[:num, rat],
|
221
|
+
# ord - use String#ord
|
222
|
+
# isupper etc - use String
|
223
|
+
# islower
|
224
|
+
# isalnum
|
225
|
+
# isalpha
|
226
|
+
# iscntrl
|
227
|
+
# isdigit
|
228
|
+
# isgraph
|
229
|
+
# isprint
|
230
|
+
# ispunct
|
231
|
+
# isspace
|
232
|
+
# isxdigit
|
233
|
+
# param - use ruby method arguments
|
234
|
+
[:perm, rat],
|
235
|
+
[:prevcand, rat],
|
236
|
+
[:prevprime, rat],
|
237
|
+
[:pfact, rat],
|
238
|
+
[:pi, Calc],
|
239
|
+
[:pix, rat],
|
240
|
+
[:places, rat],
|
241
|
+
[:pmod, rat],
|
242
|
+
[:polar, Calc],
|
243
|
+
[:poly, Calc],
|
244
|
+
# pop - use Array
|
245
|
+
[:popcnt, rat],
|
246
|
+
[:power, all],
|
247
|
+
# protect - block stuff
|
248
|
+
[:ptest, rat],
|
249
|
+
# printf - use ruby printf
|
250
|
+
# prompt - use gets (or highline, etc)
|
251
|
+
# push - use array
|
252
|
+
# putenv - use ENV
|
253
|
+
[:quo, all],
|
254
|
+
[:quomod, rat],
|
255
|
+
# rand - no random stuff needed
|
256
|
+
# randbit
|
257
|
+
# random
|
258
|
+
# randombit
|
259
|
+
# randperm
|
260
|
+
# rcin - no REDC
|
261
|
+
# rcmul - no REDC
|
262
|
+
# rcout - no REDC
|
263
|
+
# rcpow - no REDC
|
264
|
+
# rcsq - no REDC
|
265
|
+
[:re, all],
|
266
|
+
# remove - use Array
|
267
|
+
# reverse - use Array/Matrix
|
268
|
+
# rewind - use File
|
269
|
+
# rm - use File
|
270
|
+
[:root, all],
|
271
|
+
[:round, all],
|
272
|
+
# rsearch - use Matrix
|
273
|
+
# runtime - use Process#times
|
274
|
+
# saveval - not required
|
275
|
+
[:scale, all],
|
276
|
+
# scan - use ruby i/o methods
|
277
|
+
# scanf
|
278
|
+
# search - use Array/Matrix methods
|
279
|
+
[:sec, all],
|
280
|
+
[:sech, all],
|
281
|
+
# seed - for random stuff, not needed
|
282
|
+
# segment - list/matrix related
|
283
|
+
# select
|
284
|
+
# setbit - not sure whta this is for
|
285
|
+
[:sgn, all],
|
286
|
+
# sha1 - use ruby digest module (actually, don't use sha1)
|
287
|
+
[:sin, all],
|
288
|
+
[:sinh, all],
|
289
|
+
# size - related to non-numeric types
|
290
|
+
# sizeof - not necessary
|
291
|
+
# sleep - use ruby sleep
|
292
|
+
# sort - use ruby sort
|
293
|
+
[:sqrt, all],
|
294
|
+
# srand - no random stuff
|
295
|
+
# srandom
|
296
|
+
[:ssq, Calc],
|
297
|
+
# stoponerror - not in link library
|
298
|
+
# str - use to_s
|
299
|
+
# strtoupper - use ruby Strings instead of this stuff
|
300
|
+
# strtolower
|
301
|
+
# strcat
|
302
|
+
# strcmp
|
303
|
+
# strcasecmp
|
304
|
+
# strcpy
|
305
|
+
# strerror
|
306
|
+
# strlen
|
307
|
+
# strncmp
|
308
|
+
# strncasecmp
|
309
|
+
# strncpy
|
310
|
+
# strpos
|
311
|
+
# strprintf
|
312
|
+
# strscan
|
313
|
+
# strscanf
|
314
|
+
# substr
|
315
|
+
[:sum, Calc],
|
316
|
+
# swap - not necessary, use: a,b=b,a
|
317
|
+
# system - use ruby system
|
318
|
+
# systime - use Process#times
|
319
|
+
# tail - use Array
|
320
|
+
[:tan, all],
|
321
|
+
[:tanh, all],
|
322
|
+
# test - not needed; truthiness in ruby is different. for Q/C, use !zero?
|
323
|
+
# time - use Time.now.to_i
|
324
|
+
[:trunc, rat],
|
325
|
+
# ungetc - i/o stuff
|
326
|
+
# usertime - use Process#times
|
327
|
+
[:version, Calc],
|
328
|
+
[:xor, rat]
|
329
|
+
].each do |func, (*things)|
|
330
|
+
builtins += 1
|
331
|
+
missing = [*things].reject do |thing|
|
332
|
+
thing.respond_to?(func)
|
333
|
+
end
|
334
|
+
if missing.any?
|
335
|
+
puts("Expected method #{ func } on " + missing.map do |thing|
|
336
|
+
thing.is_a?(Module) ? "module #{ thing }" : "class #{ thing.class }"
|
337
|
+
end.join(", "))
|
338
|
+
else
|
339
|
+
builtins_done += 1
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
# check for compatibility with built in ruby equivalents
|
344
|
+
|
345
|
+
rat_methods = Rational(1, 2).methods - exclude
|
346
|
+
com_methods = Complex(1, 2).methods - exclude
|
347
|
+
fix_methods = 1.methods - exclude
|
348
|
+
q_methods = Calc::Q(1).methods
|
349
|
+
c_methods = Calc::C(1, 1).methods
|
350
|
+
|
351
|
+
((rat_methods & fix_methods) - q_methods).sort.each do |m|
|
352
|
+
puts "Calc::Q needs method #{ m } for Rational and Fixnum compatibility"
|
353
|
+
end
|
354
|
+
|
355
|
+
(rat_methods - fix_methods - q_methods).sort.each do |m|
|
356
|
+
puts "Calc::Q needs method #{ m } for Rational compatibility"
|
357
|
+
end
|
358
|
+
|
359
|
+
(fix_methods - rat_methods - q_methods).sort.each do |m|
|
360
|
+
puts "Calc::Q needs method #{ m } for Fixnum compatibility"
|
361
|
+
end
|
362
|
+
|
363
|
+
(com_methods - c_methods).sort.each do |m|
|
364
|
+
puts "Calc::C needs method #{ m } for Complex compatibility"
|
365
|
+
end
|
366
|
+
|
367
|
+
[
|
368
|
+
["ruby-calc builtins", builtins_done, builtins],
|
369
|
+
["Rational compatibility", (rat_methods & q_methods).size, rat_methods.size.to_f],
|
370
|
+
["Fixnum compatibility", (fix_methods & q_methods).size, fix_methods.size.to_f],
|
371
|
+
["Complex compatibility", (com_methods & c_methods).size, com_methods.size.to_f]
|
372
|
+
].each do |goal, done, total|
|
373
|
+
printf "%25s: %3d/%3d (%3.1f%%)\n", goal, done, total, done / total * 100
|
374
|
+
end
|