aboutsummaryrefslogtreecommitdiff
path: root/make/numbers.mk
blob: b0fa29a31269b6b7bad073c8a488fb2e50ddbeb8 (plain)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#
# mal (Make a Lisp) number types
#

ifndef __mal_numbers_included
__mal_numbers_included := true

_TOP_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
include $(_TOP_DIR)util.mk

LIST20_X := x x x x x x x x x x x x x x x x x x x x
LIST100_X := $(foreach x,$(LIST20_X),X X X X X)
LIST100_0 := $(foreach x,$(LIST20_X),0 0 0 0 0)
LIST100_9 := $(foreach x,$(LIST20_X),9 9 9 9 9)

###
### general numeric utility functions
###

int_encode = $(strip $(call _reverse,\
               $(eval __temp := $(1))\
               $(foreach a,0 1 2 3 4 5 6 7 8 9,\
                 $(eval __temp := $$(subst $$a,$$a$$(SPACE),$(__temp))))$(__temp)))

int_decode = $(strip $(call _join,$(call _reverse,$(1))))

# trim extaneous zero digits off the end (front of number)
_trim_zeros = $(if $(call _EQ,0,$(strip $(1))),0,$(if $(call _EQ,0,$(word 1,$(1))),$(call _trim_zeros,$(wordlist 2,$(words $(1)),$(1))),$(1)))
trim_zeros = $(strip $(if $(call _EQ,0,$(strip $(1))),$(1),$(call _reverse,$(call _trim_zeros,$(call _reverse,$(1))))))

# drop the last element of a list of words/digits
drop_last = $(call _reverse,$(wordlist 2,$(words $(1)),$(call _reverse,$(1))))

### utility function tests

#$(info $(filter-out 1,$(filter 1%,1 132 456)))
#$(info (int_encode 13): [$(call int_encode,13)])
#$(info (int_encode 156463): [$(call int_encode,156463)])
#$(info (int_decode (int_encode 156463)): [$(call int_decode,$(call int_encode,156463))])

#$(info trim_zeros(0 0 0): [$(call trim_zeros,0 0 0)])


### 
### comparisons
###

# compare two digits and return 'true' if digit 1 is less than or
# equal to digit 2
_lte_digit = $(strip \
              $(if $(call _EQ,$(1),$(2)),\
                true,\
                $(if $(call _EQ,0,$(1)),\
                  true,\
                  $(if $(wordlist $(1),$(2),$(LIST20_X)),\
                    true,\
                    ))))

# compare two lists of digits (MSB->LSB) of equal length and return
# 'true' if number 1 is less than number 2
_lte_digits = $(strip \
                $(if $(strip $(1)),\
                  $(if $(call _EQ,$(word 1,$(1)),$(word 1,$(2))),\
                    $(call _lte_digits,$(wordlist 2,$(words $(1)),$(1)),$(wordlist 2,$(words $(2)),$(2))),\
                    $(if $(call _lte_digit,$(word 1,$(1)),$(word 1,$(2))),true,)),\
                  true))

### lte/less than or equal to

int_lte_encoded = $(strip \
                    $(foreach len1,$(words $(1)),$(foreach len2,$(words $(2)),\
                      $(if $(call _EQ,$(len1),$(len2)),\
                        $(call _lte_digits,$(call _reverse,$(1)),$(call _reverse,$(2))),\
                        $(if $(wordlist $(len1),$(len2),$(LIST100_X)),\
                          true,\
                          )))))

int_lte = $(call int_lte_encoded,$(call int_encode,$(1)),$(call int_encode,$(2)))

### lt/less than

int_lt_encoded = $(strip \
                   $(if $(call _EQ,$(strip $(1)),$(strip $(2))),\
                     ,\
                     $(call int_lte_encoded,$(1),$(2))))

int_lt = $(call int_lt_encoded,$(call int_encode,$(1)),$(call int_encode,$(2)))

### gte/greater than or equal to

int_gte_encoded = $(strip \
                $(if $(call _EQ,$(strip $(1)),$(strip $(2))),\
                  true,\
                  $(if $(call int_lte_encoded,$(1),$(2)),,true)))

int_gte = $(call int_gte_encoded,$(call int_encode,$(1)),$(call int_encode,$(2)))

### gt/greater than

int_gt_encoded = $(strip \
               $(if $(call _EQ,$(strip $(1)),$(strip $(2))),\
                 ,\
                 $(call int_gte_encoded,$(1),$(2))))

int_gt = $(call int_gt_encoded,$(call int_encode,$(1)),$(call int_encode,$(2)))

#$(info _lte_digit,7,8: [$(call _lte_digit,7,8)])
#$(info _lte_digit,8,8: [$(call _lte_digit,8,8)])
#$(info _lte_digit,2,1: [$(call _lte_digit,2,1)])
#$(info _lte_digit,0,0: [$(call _lte_digit,0,0)])
#$(info _lte_digit,0,1: [$(call _lte_digit,0,1)])
#$(info _lte_digit,1,0: [$(call _lte_digit,1,0)])

#$(info _lte_digits,1 2 3,1 2 4: [$(call _lte_digits,1 2 3,1 2 4)])
#$(info _lte_digits,1 2 4,1 2 4: [$(call _lte_digits,1 2 4,1 2 4)])
#$(info _lte_digits,1 2 5,1 2 4: [$(call _lte_digits,1 2 5,1 2 4)])
#$(info _lte_digits,4 1,9 0: [$(call _lte_digits,4 1,9 0)])

#$(info int_lte_encoded,1,1: [$(call int_lte_encoded,1,1)])
#$(info int_lte_encoded,1,2: [$(call int_lte_encoded,1,2)])
#$(info int_lte_encoded,2,1: [$(call int_lte_encoded,2,1)])
#$(info int_lte_encoded,0,3: [$(call int_lte_encoded,0,3)])
#$(info int_lte_encoded,3,0: [$(call int_lte_encoded,3,0)])
#$(info int_lte_encoded,1 4,0 9: [$(call int_lte_encoded,1 4,0 9)])
#$(info int_lte_encoded,4 3 2 1,4 3 2 1: [$(call int_lte_encoded,4 3 2 1,4 3 2 1)])
#$(info int_lte_encoded,5 3 2 1,4 3 2 1: [$(call int_lte_encoded,5 3 2 1,4 3 2 1)])
#$(info int_lte_encoded,4 3 2 1,5 3 2 1: [$(call int_lte_encoded,4 3 2 1,5 3 2 1)])

#$(info int_lte,1,1: [$(call int_lte,1,1)])
#$(info int_lte,1,2: [$(call int_lte,1,2)])
#$(info int_lte,2,1: [$(call int_lte,2,1)])
#$(info int_lte,0,3: [$(call int_lte,0,3)])
#$(info int_lte,3,0: [$(call int_lte,3,0)])
#$(info int_lte,1234,1234: [$(call int_lte,1234,1234)])
#$(info int_lte,1235,1234: [$(call int_lte,1235,1234)])
#$(info int_lte,1234,1235: [$(call int_lte,1234,1235)])
#
#$(info int_lt,1,1: [$(call int_lt,1,1)])
#$(info int_lt,1,2: [$(call int_lt,1,2)])
#$(info int_lt,2,1: [$(call int_lt,2,1)])
#$(info int_lt,0,3: [$(call int_lt,0,3)])
#$(info int_lt,3,0: [$(call int_lt,3,0)])
#$(info int_lt,1234,1234: [$(call int_lt,1234,1234)])
#$(info int_lt,1235,1234: [$(call int_lt,1235,1234)])
#$(info int_lt,1234,1235: [$(call int_lt,1234,1235)])
#
#$(info int_gte,1,1: [$(call int_gte,1,1)])
#$(info int_gte,1,2: [$(call int_gte,1,2)])
#$(info int_gte,2,1: [$(call int_gte,2,1)])
#$(info int_gte,0,3: [$(call int_gte,0,3)])
#$(info int_gte,3,0: [$(call int_gte,3,0)])
#$(info int_gte,1234,1234: [$(call int_gte,1234,1234)])
#$(info int_gte,1235,1234: [$(call int_gte,1235,1234)])
#$(info int_gte,1234,1235: [$(call int_gte,1234,1235)])
#
#$(info int_gt,1,1: [$(call int_gt,1,1)])
#$(info int_gt,1,2: [$(call int_gt,1,2)])
#$(info int_gt,2,1: [$(call int_gt,2,1)])
#$(info int_gt,0,3: [$(call int_gt,0,3)])
#$(info int_gt,3,0: [$(call int_gt,3,0)])
#$(info int_gt,1234,1234: [$(call int_gt,1234,1234)])
#$(info int_gt,1235,1234: [$(call int_gt,1235,1234)])
#$(info int_gt,1234,1235: [$(call int_gt,1234,1235)])


###
### addition
###


# add_digits_with_carry
_add_digit = $(words $(if $(strip $(1)),$(wordlist 1,$(1),$(LIST20_X)),) \
                     $(if $(strip $(2)),$(wordlist 1,$(2),$(LIST20_X)),))

# add one to a single digit
_inc_digit = $(words $(wordlist 1,$(if $(1),$(1),0),$(LIST20_X)) x)

# add two encoded numbers digit by digit without resolving carries
# (each digit will be larger than 9 if there is a carry value)
_add = $(if $(1)$(2),$(call _add_digit,$(word 1,$(1)),$(word 1,$(2))) $(call _add,$(wordlist 2,$(words $(1)),$(1)),$(wordlist 2,$(words $(2)),$(2))),)

# take the result of _add and resolve the carry values digit by digit
_resolve_carries = $(strip \
             $(if $(1),\
               $(foreach num,$(word 1,$(1)),\
                 $(if $(filter-out 1,$(filter 1%,$(num))),\
                   $(call _resolve_carries,$(call _inc_digit,$(word 2,$(1))) $(wordlist 3,$(words $(1)),$(1)),$(2) $(patsubst 1%,%,$(num))),\
                   $(call _resolve_carries,$(wordlist 2,$(words $(1)),$(1)),$(2) $(num)))),\
               $(2)))

# add two encoded numbers, returns encoded number
int_add_encoded = $(call _resolve_carries,$(call _add,$(1),$(2)))

# add two unencoded numbers, returns unencoded number
int_add = $(call int_decode,$(call int_add_encoded,$(call int_encode,$(1)),$(call int_encode,$(2))))

### addition tests

#$(info _add_digit(7,6,1): [$(call _add_digit,7,6,1)])
#$(info _add_digit(7,6,0): [$(call _add_digit,7,6,0)])
#$(info _add_digit(7,6,0): [$(call _add_digit,7,6,0)])
#$(info _carries(12 14 15): [$(call _carries,12 14 15)])
#$(info _inc_digit(0): $(call _inc_digit,0))
#$(info _inc_digit(1): $(call _inc_digit,1))
#$(info _inc_digit(9): $(call _inc_digit,9))
#$(info _inc_digit(18): $(call _inc_digit,18))
#$(info int_add_encoded(0,0): [$(call int_add_encoded,0,0)])

#$(info int_add(1,2): [$(call int_add,1,2)])
#$(info int_add(9,9): [$(call int_add,9,9)])
#$(info int_add(0,9): [$(call int_add,0,9)])
#$(info int_add(9,0): [$(call int_add,9,0)])
#$(info int_add(0,0): [$(call int_add,0,0)])
#$(info int_add(123,456): [$(call int_add,123,456)])
#$(info int_add(678,789): [$(call int_add,678,789)])
#$(info int_add(1,12): [$(call int_add,1,12)])
#$(info int_add(123,5): [$(call int_add,123,5)])
#$(info int_add(123456,9): [$(call int_add,123456,9)])
#$(info int_add(999999991,9): [$(call int_add,999999991,9)])

###
### subtraction
###

_get_zeros = $(if $(call _EQ,0,$(word 1,$(1))),$(call _get_zeros,$(wordlist 2,$(words $(1)),$(1)),$(2) 0),$(2))

# return a 9's complement of a single digit
_complement9 = $(strip \
                 $(if $(call _EQ,0,$(1)),9,\
                 $(if $(call _EQ,1,$(1)),8,\
                 $(if $(call _EQ,2,$(1)),7,\
                 $(if $(call _EQ,3,$(1)),6,\
                 $(if $(call _EQ,4,$(1)),5,\
                 $(if $(call _EQ,5,$(1)),4,\
                 $(if $(call _EQ,6,$(1)),3,\
                 $(if $(call _EQ,7,$(1)),2,\
                 $(if $(call _EQ,8,$(1)),1,\
                 $(if $(call _EQ,9,$(1)),0)))))))))))

# return a 10's complement of a single digit
_complement10 = $(call _inc_digit,$(call _complement9,$(1)))

# 
_complement_rest = $(if $(strip $(1)),\
                     $(strip \
                       $(call _complement10,$(word 1,$(1))) \
                       $(foreach digit,$(wordlist 2,$(words $(1)),$(1)),\
                         $(call _complement9,$(digit)))),)

# return the complement of a number
_complement = $(strip $(call _get_zeros,$(1)) \
                      $(call _complement_rest,$(wordlist $(call _inc_digit,$(words $(call _get_zeros,$(1)))),$(words $(1)),$(1))))

# subtracted encoded number 2 from encoded number 1 and return and
# encoded number result
int_sub_encoded = $(strip \
                $(if $(call _EQ,0,$(strip $(2))),\
                  $(1),\
                  $(call trim_zeros,\
                    $(call drop_last,\
                      $(call int_add_encoded,\
                        $(1),\
                        $(wordlist 1,$(words $(1)),$(call _complement,$(2)) $(LIST100_9)))))))

# subtract unencoded number 2 from unencoded number 1 and return
# unencoded result
int_sub = $(call int_decode,$(call int_sub_encoded,$(call int_encode,$(1)),$(call int_encode,$(2))))

### subtraction tests

#$(info _get_zeros(5 7): [$(call _get_zeros,5 7)])
#$(info _get_zeros(0 0 0 2): [$(call _get_zeros,0 0 0 2)])
#$(info _get_zeros(0 0 0 2 5): [$(call _get_zeros,0 0 0 2 5)])

#$(info _complement(0): [$(call _complement,0)])
#$(info _complement(1): [$(call _complement,1)])
#$(info _complement(9): [$(call _complement,9)])
#$(info _complement(5 7): [$(call _complement,5 7)])
#$(info _complement(0 0 0 2): [$(call _complement,0 0 0 2)])
#$(info _complement(0 0 0 5 4 3 2 1): [$(call _complement,0 0 0 5 4 3 2 1)])

#$(info int_sub_encoded(0 0 1, 3 1): [$(call int_sub_encoded,0 0 1,3 1)])
#$(info int_sub_encoded(2, 2): [$(call int_sub_encoded,2,2)])

#$(info int_sub(2,1): [$(call int_sub,2,1)])
#$(info int_sub(2,0): [$(call int_sub,2,0)])
#$(info int_sub(2,2): [$(call int_sub,2,2)])
#$(info int_sub(100,13): [$(call int_sub,100,13)])
#$(info int_sub(100,99): [$(call int_sub,100,99)])
#$(info int_sub(91,19): [$(call int_sub,91,19)])


###
### multiplication
###

# multiply two digits
#_mult_digit = $(words $(foreach x,$(1),$(2)))
_mult_digit = $(strip \
                $(words $(foreach x,$(wordlist 1,$(1),$(LIST20_X)),\
                        $(wordlist 1,$(2),$(LIST20_X)))))

# multipy every digit of number 1 with number 2
# params: digits, digit, indent_zeros, results
_mult_row = $(if $(strip $(1)),$(call _mult_row,$(wordlist 2,$(words $(1)),$(1)),$(2),$(3)0,$(4) $(call _mult_digit,$(word 1,$(1)),$(2))$(3)),$(4))

# multiply every digit of number 2 with every digit of number 1 adding
# correct zero padding to the end of each result
# params: digits, digits, indent_zeros, results
_mult_each = $(if $(strip $(2)),$(call _mult_each,$(1),$(wordlist 2,$(words $(2)),$(2)),$(3)0,$(4) $(call _mult_row,$(1),$(word 1,$(2)),$(3))),$(4))

# add up a bunch of unencoded numbers. Basically reduce into the first number
_add_many = $(if $(word 2,$(1)),$(call _add_many,$(call int_add,$(word 1,$(1)),$(word 2,$(1))) $(wordlist  3,$(words $(1)),$(1))),$(1))

# multiply two encoded numbers, returns encoded number
int_mult_encoded = $(call trim_zeros,$(call int_encode,$(call _add_many,$(call _mult_each,$(1),$(2)))))

# multiply two unencoded numbers, returns unencoded number
int_mult = $(call int_decode,$(call int_mult_encoded,$(call int_encode,$(1)),$(call int_encode,$(2))))

#$(info _mult_digit(8,6): [$(call _mult_digit,8,6)])
#$(info _mult_digit(7,6): [$(call _mult_digit,7,6)])
#$(info _mult_row(8,6): [$(call _mult_row,8,6)])
#$(info _mult_row(8 7,6): [$(call _mult_row,8 7,6)])
#$(info _mult_row(8 7 3,6): [$(call _mult_row,8 7 3,6)])
#$(info _mult_each(8 7 6, 4 3 2): [$(call _mult_each,8 7 6,4 3 2)])
#$(info _add_many(123 234 345 456): [$(call _add_many,123 234 345 456)])

#$(info int_mult_encoded(8 7 3,6): [$(call int_mult_encoded,8 7 3,6)])
#$(info int_mult_encoded(8 7 3,0): [$(call int_mult_encoded,8 7 3,0)])

#$(info int_mult(378,6): [$(call int_mult,378,6)])
#$(info int_mult(678,234): [$(call int_mult,678,234)])
#$(info int_mult(1,23456): [$(call int_mult,1,23456)])
#$(info int_mult(0,23456): [$(call int_mult,0,23456)])
#$(info int_mult(0,0): [$(call int_mult,0,0)])

###
### division
###

# return list of zeros needed to pad number 2 to the same length as number 1
_zero_pad = $(strip $(wordlist 1,$(call int_sub,$(words $(1)),$(words $(2))),$(LIST100_0)))

# num1, num2, zero pad, result_accumulator
# algorithm:
# - B = pad with zeros to make same digit length as A
# - loop
#    - if (B <= A)
#        - A = subtract B from A
#        - C = C + 10^(B pad.length)
#    - else
#        - if B.length < origin B.length: break
#        - chop least significant digit of B
_div = $(strip \
         $(if $(call int_lte_encoded,$(3) $(2),$(1)),\
           $(call _div,$(call int_sub_encoded,$(1),$(3) $(2)),$(2),$(3),$(call int_add_encoded,$(4),$(3) 1)),\
           $(if $(3),\
             $(call _div,$(1),$(2),$(wordlist 2,$(words $(3)),$(3)),$(4)),\
             $(4))))

# divide two encoded numbers, returns encoded number
int_div_encoded = $(strip \
                $(if $(call _EQ,0,$(1)),\
                  0,\
                  $(if $(call _EQ,$(1),$(2)),\
                    1,\
                    $(if $(call int_gt_encoded,$(2),$(1)),\
                      0,\
                      $(call _div,$(1),$(2),$(call _zero_pad,$(1),$(2)),0)))))

# divide two unencoded numbers, returns unencoded number
int_div = $(call int_decode,$(call int_div_encoded,$(call int_encode,$(1)),$(call int_encode,$(2))))

### division tests

#$(info _zero_pad(1 2 3 4,1 3): [$(call _zero_pad,1 2 3 4,1 3)])
#$(info _zero_pad(1 2,1 3): [$(call _zero_pad,1 2,1 3)])
#$(info _zero_pad(2,1 3): [$(call _zero_pad,1 2,1 3)])
#
#$(info int_div_encoded(2,1): [$(call int_div_encoded,2,1)])
#$(info int_div_encoded(3,1): [$(call int_div_encoded,3,1)])
#$(info int_div_encoded(3,2): [$(call int_div_encoded,3,2)])
#$(info int_div_encoded(0,7): [$(call int_div_encoded,0,7)])
#$(info int_div_encoded(0 3,0 2): [$(call int_div_encoded,0 3,0 2)])
#$(info int_div_encoded(0 3,5): [$(call int_div_encoded,0 3,5)])
#
#$(info int_div(5,1): [$(call int_div,5,1)])
#$(info int_div(5,2): [$(call int_div,5,2)])
#$(info int_div(123,7): [$(call int_div,123,7)])
#$(info int_div(100,7): [$(call int_div,100,7)])


### combination tests

# (/ (- (+ 515 (* 222 311)) 300) 41)
#$(info int_mult,222,311: [$(call int_mult,222,311)])
#$(info int_add(515,69042): [$(call int_add,515,69042)])
#$(info int_sub(69557,300): [$(call int_sub,69557,300)])
#$(info int_div(69257,41): [$(call int_div,69257,41)])

###############################################################

all:
	@true

endif

# vim: ts=2 et