8
set_error_when_null_denominator false
11
let infinite_failure = "infinite or undefined rational number";;
13
testing_function "create_ratio"
16
let r = create_ratio (big_int_of_int 1) (big_int_of_int (-2)) in
17
test 1 eq_big_int (numerator_ratio r, big_int_of_int (-1)) &&
18
test 2 eq_big_int (denominator_ratio r, big_int_of_int 2)
21
let r = create_ratio (big_int_of_int 2) (big_int_of_int 3) in
22
test 3 eq_big_int (numerator_ratio r, big_int_of_int 2) &&
23
test 4 eq_big_int (denominator_ratio r, big_int_of_int 3)
26
set_normalize_ratio true
29
let r = create_ratio (big_int_of_int 12) (big_int_of_int (-16)) in
30
test 5 eq_big_int (numerator_ratio r, big_int_of_int (-3)) &&
31
test 6 eq_big_int (denominator_ratio r, big_int_of_int 4)
34
set_normalize_ratio false
37
let r = create_ratio (big_int_of_int 0) (big_int_of_int 0) in
38
test 7 eq_big_int (numerator_ratio r, big_int_of_int 0) &&
39
test 8 eq_big_int (denominator_ratio r, big_int_of_int 0)
42
testing_function "create_normalized_ratio"
45
let r = create_normalized_ratio (big_int_of_int 1) (big_int_of_int (-2)) in
46
test 1 eq_big_int (numerator_ratio r, big_int_of_int (-1)) &&
47
test 2 eq_big_int (denominator_ratio r, big_int_of_int 2)
50
let r = create_normalized_ratio (big_int_of_int 2) (big_int_of_int 3) in
51
test 3 eq_big_int (numerator_ratio r, big_int_of_int 2) &&
52
test 4 eq_big_int (denominator_ratio r, big_int_of_int 3)
55
set_normalize_ratio true
58
let r = create_normalized_ratio (big_int_of_int 12) (big_int_of_int (-16)) in
59
test 5 eq_big_int (numerator_ratio r, big_int_of_int (-12)) &&
60
test 6 eq_big_int (denominator_ratio r, big_int_of_int 16)
63
set_normalize_ratio false
66
let r = create_normalized_ratio (big_int_of_int 1) (big_int_of_int 0) in
67
test 7 eq_big_int (numerator_ratio r, big_int_of_int 1) &&
68
test 8 eq_big_int (denominator_ratio r, big_int_of_int 0)
71
let r = create_normalized_ratio (big_int_of_int 0) (big_int_of_int 0) in
72
test 9 eq_big_int (numerator_ratio r, big_int_of_int 0) &&
73
test 10 eq_big_int (denominator_ratio r, big_int_of_int 0)
76
testing_function "null_denominator"
80
eq (null_denominator (create_ratio (big_int_of_int 1) (big_int_of_int (-2))),
84
(null_denominator (create_ratio (big_int_of_int 1) zero_big_int),true)
88
testing_function "verify_null_denominator"
92
eq (verify_null_denominator (ratio_of_string "0/1"), false)
95
eq (verify_null_denominator (ratio_of_string "0/0"), true)
99
testing_function "sign_ratio"
103
eq_int (sign_ratio (create_ratio (big_int_of_int (-2)) (big_int_of_int (-3))),
107
eq_int (sign_ratio (create_ratio (big_int_of_int 2) (big_int_of_int (-3))),
111
eq_int (sign_ratio (create_ratio zero_big_int (big_int_of_int (-3))), 0)
114
testing_function "normalize_ratio"
117
let r = create_ratio (big_int_of_int 12) (big_int_of_int (-16)) in
118
ignore (normalize_ratio r);
119
test 1 eq_big_int (numerator_ratio r, big_int_of_int (-3)) &&
120
test 2 eq_big_int (denominator_ratio r, big_int_of_int 4)
123
let r = create_ratio (big_int_of_int (-1)) zero_big_int in
124
ignore (normalize_ratio r);
125
test 3 eq_big_int (numerator_ratio r, big_int_of_int (-1)) &&
126
test 4 eq_big_int (denominator_ratio r, zero_big_int)
129
testing_function "report_sign_ratio"
133
eq_big_int (report_sign_ratio
134
(create_ratio (big_int_of_int 2) (big_int_of_int (-3)))
139
eq_big_int (report_sign_ratio
140
(create_ratio (big_int_of_int 2) (big_int_of_int 3))
145
testing_function "is_integer_ratio"
149
(is_integer_ratio (create_ratio (big_int_of_int 2) (big_int_of_int (-1))),
153
(is_integer_ratio (create_ratio (big_int_of_int 2) (big_int_of_int 3)),
157
testing_function "add_ratio"
160
let r = add_ratio (create_ratio (big_int_of_int 1) (big_int_of_int 2))
161
(create_ratio (big_int_of_int 2) (big_int_of_int 3)) in
162
test 1 eq_big_int (numerator_ratio r, big_int_of_int 7) &&
163
test 2 eq_big_int (denominator_ratio r, big_int_of_int 6)
166
let r = add_ratio (create_ratio (big_int_of_int 2) (big_int_of_int 3))
167
(create_ratio (big_int_of_int 1) (big_int_of_int (-2))) in
168
test 3 eq_big_int (numerator_ratio r, big_int_of_int 1) &&
169
test 4 eq_big_int (denominator_ratio r, big_int_of_int 6)
172
let r = add_ratio (create_ratio (big_int_of_int 2) zero_big_int)
173
(create_ratio (big_int_of_int 1) (big_int_of_int (-2))) in
174
test 5 eq_big_int (numerator_ratio r, big_int_of_int 4) &&
175
test 6 eq_big_int (denominator_ratio r, zero_big_int)
178
let r = add_ratio (create_ratio (big_int_of_int 2) (big_int_of_int 3))
179
(create_ratio (big_int_of_int 1) zero_big_int) in
180
test 7 eq_big_int (numerator_ratio r, big_int_of_int 3) &&
181
test 8 eq_big_int (denominator_ratio r, zero_big_int)
184
let r = add_ratio (create_ratio (big_int_of_int 2) zero_big_int)
185
(create_ratio (big_int_of_int 1) zero_big_int) in
186
test 9 eq_big_int (numerator_ratio r, zero_big_int) &&
187
test 10 eq_big_int (denominator_ratio r, zero_big_int)
190
let r = add_ratio (create_ratio (big_int_of_string "12724951")
191
(big_int_of_string "26542080"))
192
(create_ratio (big_int_of_string "-1")
193
(big_int_of_string "81749606400")) in
194
test 11 eq_big_int (numerator_ratio r,
195
big_int_of_string "1040259735682744320") &&
196
test 12 eq_big_int (denominator_ratio r,
197
big_int_of_string "2169804593037312000")
201
(create_ratio (big_int_of_string "12724951")
202
(big_int_of_string "26542080"),
203
create_ratio (big_int_of_string "-1")
204
(big_int_of_string "81749606400")) in
206
let bi1 = mult_big_int (numerator_ratio r1) (denominator_ratio r2)
207
and bi2 = mult_big_int (numerator_ratio r2) (denominator_ratio r1)
211
big_int_of_string "1040259735709286400")
215
big_int_of_string "-26542080")
217
eq_big_int (mult_big_int (denominator_ratio r1) (denominator_ratio r2),
218
big_int_of_string "2169804593037312000")
220
eq_big_int (add_big_int bi1 bi2,
221
big_int_of_string "1040259735682744320")
224
testing_function "sub_ratio"
227
let r = sub_ratio (create_ratio (big_int_of_int 2) (big_int_of_int 3))
228
(create_ratio (big_int_of_int 1) (big_int_of_int 2)) in
229
test 1 eq_big_int (numerator_ratio r, big_int_of_int 1) &&
230
test 2 eq_big_int (denominator_ratio r, big_int_of_int 6)
233
let r = sub_ratio (create_ratio (big_int_of_int 2) zero_big_int)
234
(create_ratio (big_int_of_int 1) (big_int_of_int (-2))) in
235
test 3 eq_big_int (numerator_ratio r, big_int_of_int 4) &&
236
test 4 eq_big_int (denominator_ratio r, zero_big_int)
239
let r = sub_ratio (create_ratio (big_int_of_int 2) (big_int_of_int 3))
240
(create_ratio (big_int_of_int 1) zero_big_int) in
241
test 5 eq_big_int (numerator_ratio r, big_int_of_int (-3)) &&
242
test 6 eq_big_int (denominator_ratio r, zero_big_int)
245
let r = sub_ratio (create_ratio (big_int_of_int 2) zero_big_int)
246
(create_ratio (big_int_of_int 1) zero_big_int) in
247
test 7 eq_big_int (numerator_ratio r, zero_big_int) &&
248
test 8 eq_big_int (denominator_ratio r, zero_big_int)
251
testing_function "mult_ratio"
254
let r = mult_ratio (create_ratio (big_int_of_int 2) (big_int_of_int 3))
255
(create_ratio (big_int_of_int 7) (big_int_of_int 5)) in
256
test 1 eq_big_int (numerator_ratio r, big_int_of_int 14) &&
257
test 2 eq_big_int (denominator_ratio r, big_int_of_int 15)
260
let r = mult_ratio (create_ratio (big_int_of_int 2) zero_big_int)
261
(create_ratio (big_int_of_int 1) (big_int_of_int (-2))) in
262
test 3 eq_big_int (numerator_ratio r, big_int_of_int (-2)) &&
263
test 4 eq_big_int (denominator_ratio r, zero_big_int)
266
let r = mult_ratio (create_ratio (big_int_of_int 2) (big_int_of_int 3))
267
(create_ratio (big_int_of_int 1) zero_big_int) in
268
test 5 eq_big_int (numerator_ratio r, big_int_of_int 2) &&
269
test 6 eq_big_int (denominator_ratio r, zero_big_int)
272
let r = mult_ratio (create_ratio (big_int_of_int 2) zero_big_int)
273
(create_ratio (big_int_of_int 1) zero_big_int) in
274
test 7 eq_big_int (numerator_ratio r, big_int_of_int 2) &&
275
test 8 eq_big_int (denominator_ratio r, zero_big_int)
278
testing_function "div_ratio"
281
let r = div_ratio (create_ratio (big_int_of_int 2) (big_int_of_int 3))
282
(create_ratio (big_int_of_int 5) (big_int_of_int 7)) in
283
test 1 eq_big_int (numerator_ratio r, big_int_of_int 14) &&
284
test 2 eq_big_int (denominator_ratio r, big_int_of_int 15)
287
let r = div_ratio (create_ratio (big_int_of_int 2) zero_big_int)
288
(create_ratio (big_int_of_int 1) (big_int_of_int (-2))) in
289
test 3 eq_big_int (numerator_ratio r, big_int_of_int (-4)) &&
290
test 4 eq_big_int (denominator_ratio r, zero_big_int)
293
let r = div_ratio (create_ratio (big_int_of_int 2) (big_int_of_int 3))
294
(create_ratio (big_int_of_int 1) zero_big_int) in
295
test 5 eq_big_int (numerator_ratio r, zero_big_int) &&
296
test 6 eq_big_int (denominator_ratio r, big_int_of_int 3)
299
let r = div_ratio (create_ratio (big_int_of_int 2) zero_big_int)
300
(create_ratio (big_int_of_int 1) zero_big_int) in
301
test 7 eq_big_int (numerator_ratio r, zero_big_int) &&
302
test 8 eq_big_int (denominator_ratio r, zero_big_int)
305
testing_function "integer_ratio"
309
eq_big_int (integer_ratio
310
(create_ratio (big_int_of_int 5) (big_int_of_int 3)),
314
eq_big_int (integer_ratio
315
(create_ratio (big_int_of_int 5) (big_int_of_int (-3))),
319
eq_big_int (integer_ratio
320
(create_ratio (big_int_of_int 3) (big_int_of_int 2)),
324
eq_big_int (integer_ratio
325
(create_ratio (big_int_of_int 3) (big_int_of_int (-2))),
330
integer_ratio (create_ratio (big_int_of_int 3) zero_big_int)
331
(Failure("integer_ratio "^infinite_failure))
334
testing_function "floor_ratio"
338
eq_big_int (floor_ratio
339
(create_ratio (big_int_of_int 5) (big_int_of_int 3)),
343
eq_big_int (floor_ratio
344
(create_ratio (big_int_of_int 5) (big_int_of_int (-3))),
348
eq_big_int (floor_ratio
349
(create_ratio (big_int_of_int 3) (big_int_of_int 2)),
353
eq_big_int (floor_ratio
354
(create_ratio (big_int_of_int 3) (big_int_of_int (-2))),
358
failwith_test 5 floor_ratio (create_ratio (big_int_of_int 3) zero_big_int)
363
testing_function "round_ratio"
367
eq_big_int (round_ratio
368
(create_ratio (big_int_of_int 5) (big_int_of_int 3)),
372
eq_big_int (round_ratio
373
(create_ratio (big_int_of_int 5) (big_int_of_int (-3))),
377
eq_big_int (round_ratio
378
(create_ratio (big_int_of_int 3) (big_int_of_int 2)),
382
eq_big_int (round_ratio
383
(create_ratio (big_int_of_int 3) (big_int_of_int (-2))),
388
round_ratio (create_ratio (big_int_of_int 3) zero_big_int)
393
testing_function "ceiling_ratio"
397
eq_big_int (ceiling_ratio
398
(create_ratio (big_int_of_int 5) (big_int_of_int 3)),
402
eq_big_int (ceiling_ratio
403
(create_ratio (big_int_of_int 5) (big_int_of_int (-3))),
407
eq_big_int (ceiling_ratio
408
(create_ratio (big_int_of_int 3) (big_int_of_int 2)),
412
eq_big_int (ceiling_ratio
413
(create_ratio (big_int_of_int 3) (big_int_of_int (-2))),
417
eq_big_int (ceiling_ratio
418
(create_ratio (big_int_of_int 4) (big_int_of_int 2)),
422
ceiling_ratio (create_ratio (big_int_of_int 3) zero_big_int)
426
testing_function "eq_ratio"
430
eq_ratio (create_ratio (big_int_of_int 5) (big_int_of_int 3),
431
create_ratio (big_int_of_int (-20)) (big_int_of_int (-12)))
434
eq_ratio (create_ratio (big_int_of_int 1) zero_big_int,
435
create_ratio (big_int_of_int 2) zero_big_int)
438
let neq_ratio x y = not (eq_ratio x y);;
441
neq_ratio (create_ratio (big_int_of_int 1) zero_big_int,
442
create_ratio (big_int_of_int (-1)) zero_big_int)
445
neq_ratio (create_ratio (big_int_of_int 1) zero_big_int,
446
create_ratio zero_big_int zero_big_int)
449
eq_ratio (create_ratio zero_big_int zero_big_int,
450
create_ratio zero_big_int zero_big_int)
453
testing_function "compare_ratio"
457
eq_int (compare_ratio (create_ratio (big_int_of_int 0) (big_int_of_int 0))
458
(create_ratio (big_int_of_int 0) (big_int_of_int 0)),
462
eq_int (compare_ratio (create_ratio (big_int_of_int 0) (big_int_of_int 0))
463
(create_ratio (big_int_of_int 1) (big_int_of_int 0)),
467
eq_int (compare_ratio (create_ratio (big_int_of_int 0) (big_int_of_int 0))
468
(create_ratio (big_int_of_int (-1)) (big_int_of_int 0)),
472
eq_int (compare_ratio (create_ratio (big_int_of_int 1) (big_int_of_int 0))
473
(create_ratio (big_int_of_int 0) (big_int_of_int 0)),
477
eq_int (compare_ratio (create_ratio (big_int_of_int (-1)) (big_int_of_int 0))
478
(create_ratio (big_int_of_int 0) (big_int_of_int 0)),
482
eq_int (compare_ratio (create_ratio (big_int_of_int 0) (big_int_of_int 0))
483
(create_ratio (big_int_of_int 5) (big_int_of_int 3)),
487
eq_int (compare_ratio (create_ratio (big_int_of_int 5) (big_int_of_int 3))
488
(create_ratio (big_int_of_int 0) (big_int_of_int 0)),
492
eq_int (compare_ratio (create_ratio (big_int_of_int 0) (big_int_of_int 0))
493
(create_ratio (big_int_of_int (-5)) (big_int_of_int 3)),
497
eq_int (compare_ratio (create_ratio (big_int_of_int (-5)) (big_int_of_int 3))
498
(create_ratio (big_int_of_int 0) (big_int_of_int 0)),
502
eq_int (compare_ratio (create_ratio (big_int_of_int 0) (big_int_of_int 0))
503
(create_ratio (big_int_of_int 0) (big_int_of_int 1)),
507
eq_int (compare_ratio (create_ratio (big_int_of_int 0) (big_int_of_int 1))
508
(create_ratio (big_int_of_int 0) (big_int_of_int 0)),
512
eq_int (compare_ratio (create_ratio (big_int_of_int 1) (big_int_of_int 0))
513
(create_ratio (big_int_of_int 1) (big_int_of_int 0)),
517
eq_int (compare_ratio (create_ratio (big_int_of_int 1) (big_int_of_int 0))
518
(create_ratio (big_int_of_int 2) (big_int_of_int 0)),
522
eq_int (compare_ratio (create_ratio (big_int_of_int 1) (big_int_of_int 0))
523
(create_ratio (big_int_of_int (-1)) (big_int_of_int 0)),
527
eq_int (compare_ratio (create_ratio (big_int_of_int (-1)) (big_int_of_int 0))
528
(create_ratio (big_int_of_int 1) (big_int_of_int 0)),
532
eq_int (compare_ratio (create_ratio (big_int_of_int 5) (big_int_of_int 3))
533
(create_ratio (big_int_of_int 1) (big_int_of_int 0)),
537
eq_int (compare_ratio (create_ratio (big_int_of_int 1) (big_int_of_int 0))
538
(create_ratio (big_int_of_int 5) (big_int_of_int 3)),
542
eq_int (compare_ratio (create_ratio (big_int_of_int (-5)) (big_int_of_int 3))
543
(create_ratio (big_int_of_int 1) (big_int_of_int 0)),
547
eq_int (compare_ratio (create_ratio (big_int_of_int 1) (big_int_of_int 0))
548
(create_ratio (big_int_of_int (-5)) (big_int_of_int 3)),
552
eq_int (compare_ratio (create_ratio (big_int_of_int 1) (big_int_of_int 0))
553
(create_ratio (big_int_of_int 0) (big_int_of_int 3)),
557
eq_int (compare_ratio (create_ratio (big_int_of_int (-1)) (big_int_of_int 0))
558
(create_ratio (big_int_of_int (-1)) (big_int_of_int 0)),
562
eq_int (compare_ratio (create_ratio (big_int_of_int (-1)) (big_int_of_int 0))
563
(create_ratio (big_int_of_int (-2)) (big_int_of_int 0)),
567
eq_int (compare_ratio (create_ratio (big_int_of_int 5) (big_int_of_int 3))
568
(create_ratio (big_int_of_int (-1)) (big_int_of_int 0)),
572
eq_int (compare_ratio (create_ratio (big_int_of_int (-1)) (big_int_of_int 0))
573
(create_ratio (big_int_of_int 5) (big_int_of_int 3)),
577
eq_int (compare_ratio (create_ratio (big_int_of_int (-5)) (big_int_of_int 3))
578
(create_ratio (big_int_of_int (-1)) (big_int_of_int 0)),
582
eq_int (compare_ratio (create_ratio (big_int_of_int (-1)) (big_int_of_int 0))
583
(create_ratio (big_int_of_int (-5)) (big_int_of_int 3)),
587
eq_int (compare_ratio (create_ratio (big_int_of_int (-1)) (big_int_of_int 0))
588
(create_ratio (big_int_of_int 0) (big_int_of_int 3)),
592
eq_int (compare_ratio (create_ratio (big_int_of_int 5) (big_int_of_int 3))
593
(create_ratio (big_int_of_int 3) (big_int_of_int 2)),
597
eq_int (compare_ratio (create_ratio (big_int_of_int 3) (big_int_of_int 2))
598
(create_ratio (big_int_of_int 5) (big_int_of_int 3)),
602
eq_int (compare_ratio (create_ratio (big_int_of_int 5) (big_int_of_int 3))
603
(create_ratio (big_int_of_int (-3)) (big_int_of_int 2)),
607
eq_int (compare_ratio (create_ratio (big_int_of_int (-3)) (big_int_of_int 2))
608
(create_ratio (big_int_of_int 5) (big_int_of_int 3)),
612
eq_int (compare_ratio (create_ratio (big_int_of_int 3) (big_int_of_int 2))
613
(create_ratio (big_int_of_int 0) (big_int_of_int 3)),
617
eq_int (compare_ratio (create_ratio (big_int_of_int 0) (big_int_of_int 2))
618
(create_ratio (big_int_of_int 5) (big_int_of_int 3)),
622
eq_int (compare_ratio (create_ratio (big_int_of_int (-3)) (big_int_of_int 2))
623
(create_ratio (big_int_of_int 0) (big_int_of_int 3)),
627
eq_int (compare_ratio (create_ratio (big_int_of_int 0) (big_int_of_int 2))
628
(create_ratio (big_int_of_int (-5)) (big_int_of_int 3)),
632
eq_int (compare_ratio (create_ratio (big_int_of_int 0) (big_int_of_int 2))
633
(create_ratio (big_int_of_int 0) (big_int_of_int 3)),
637
testing_function "eq_big_int_ratio"
641
eq_big_int_ratio (big_int_of_int 3,
642
(create_ratio (big_int_of_int 3) (big_int_of_int 1)))
646
(not (eq_big_int_ratio (big_int_of_int 1)
647
(create_ratio (big_int_of_int 3) (big_int_of_int 1))),
653
(not (eq_big_int_ratio (big_int_of_int 1)
654
(create_ratio (big_int_of_int 3) (big_int_of_int 2))),
660
(not (eq_big_int_ratio (big_int_of_int 1)
661
(create_ratio (big_int_of_int 3) (big_int_of_int 0))),
667
(not (eq_big_int_ratio (big_int_of_int 1)
668
(create_ratio (big_int_of_int (-3)) (big_int_of_int 2))),
672
testing_function "compare_big_int_ratio"
676
eq_int (compare_big_int_ratio
678
(create_ratio (big_int_of_int 3) (big_int_of_int 0)), (-1))
681
eq_int (compare_big_int_ratio
683
(create_ratio (big_int_of_int 0) (big_int_of_int 0)), 0)
686
eq_int (compare_big_int_ratio
688
(create_ratio (big_int_of_int (-3)) (big_int_of_int 0)), 1)
691
eq_int (compare_big_int_ratio
692
(big_int_of_int (-1))
693
(create_ratio (big_int_of_int 3) (big_int_of_int 0)), (-1))
696
eq_int (compare_big_int_ratio
697
(big_int_of_int (-1))
698
(create_ratio (big_int_of_int 0) (big_int_of_int 0)), 0)
701
eq_int (compare_big_int_ratio
702
(big_int_of_int (-1))
703
(create_ratio (big_int_of_int (-3)) (big_int_of_int 0)), 1)
706
eq_int (compare_big_int_ratio
708
(create_ratio (big_int_of_int 1) (big_int_of_int 1)), 0)
711
eq_int (compare_big_int_ratio
713
(create_ratio (big_int_of_int 3) (big_int_of_int 2)), (-1))
716
eq_int (compare_big_int_ratio
718
(create_ratio (big_int_of_int 2) (big_int_of_int 3)), 1)
723
testing_function "int_of_ratio"
727
eq_int (int_of_ratio (create_ratio (big_int_of_int 4) (big_int_of_int 2)),
733
(create_ratio (big_int_of_int biggest_int) (big_int_of_int 1)),
738
int_of_ratio (create_ratio (big_int_of_int 4) (big_int_of_int 0))
739
(Failure "integer argument required")
743
int_of_ratio (create_ratio (succ_big_int (big_int_of_int biggest_int))
745
(Failure "integer argument required")
749
int_of_ratio (create_ratio (big_int_of_int 4) (big_int_of_int 3))
750
(Failure "integer argument required")
753
testing_function "ratio_of_int"
757
eq_ratio (ratio_of_int 3,
758
create_ratio (big_int_of_int 3) (big_int_of_int 1))
762
eq_ratio (ratio_of_nat (nat_of_int 2),
763
create_ratio (big_int_of_int 2) (big_int_of_int 1))
766
testing_function "nat_of_ratio"
769
let nat1 = nat_of_ratio (create_ratio (big_int_of_int 3) (big_int_of_int 1))
770
and nat2 = nat_of_int 3 in
772
eq (eq_nat nat1 0 (length_nat nat1) nat2 0 (length_nat nat2), true)
776
nat_of_ratio (create_ratio (big_int_of_int 3) (big_int_of_int 0))
777
(Failure "nat_of_ratio")
781
nat_of_ratio (create_ratio (big_int_of_int (-3)) (big_int_of_int 1))
782
(Failure "nat_of_ratio")
786
nat_of_ratio (create_ratio (big_int_of_int 3) (big_int_of_int 2))
787
(Failure "nat_of_ratio")
790
testing_function "ratio_of_big_int"
794
eq_ratio (ratio_of_big_int (big_int_of_int 3),
795
create_ratio (big_int_of_int 3) (big_int_of_int 1))
798
testing_function "big_int_of_ratio"
802
eq_big_int (big_int_of_ratio
803
(create_ratio (big_int_of_int 3) (big_int_of_int 1)),
807
eq_big_int (big_int_of_ratio
808
(create_ratio (big_int_of_int (-3)) (big_int_of_int 1)),
813
big_int_of_ratio (create_ratio (big_int_of_int 3) (big_int_of_int 0))
814
(Failure "big_int_of_ratio")
817
testing_function "string_of_ratio"
821
eq_string (string_of_ratio
822
(create_ratio (big_int_of_int 43) (big_int_of_int 35)),
826
eq_string (string_of_ratio
827
(create_ratio (big_int_of_int 42) (big_int_of_int 0)),
831
set_normalize_ratio_when_printing false
835
eq_string (string_of_ratio
836
(create_ratio (big_int_of_int 42) (big_int_of_int 35)),
840
set_normalize_ratio_when_printing true
844
eq_string (string_of_ratio
845
(create_ratio (big_int_of_int 42) (big_int_of_int 35)),
849
testing_function "ratio_of_string"
853
eq_ratio (ratio_of_string ("123/3456"),
854
create_ratio (big_int_of_int 123) (big_int_of_int 3456))
859
eq_ratio (ratio_of_string ("12.3/34.56"),
860
create_ratio (big_int_of_int 1230) (big_int_of_int 3456))
863
eq_ratio (ratio_of_string ("1.23/325.6"),
864
create_ratio (big_int_of_int 123) (big_int_of_int 32560))
867
eq_ratio (ratio_of_string ("12.3/345.6"),
868
create_ratio (big_int_of_int 123) (big_int_of_int 3456))
871
eq_ratio (ratio_of_string ("12.3/0.0"),
872
create_ratio (big_int_of_int 123) (big_int_of_int 0))
876
eq_ratio (ratio_of_string ("0/0"),
877
create_ratio (big_int_of_int 0) (big_int_of_int 0))
881
eq_ratio (ratio_of_string "1234567890",
882
create_ratio (big_int_of_string "1234567890") unit_big_int)
885
ratio_of_string "frlshjkurty" (Failure "invalid digit");;
888
testing_function "msd_ratio"
892
eq_int (msd_ratio (create_ratio (big_int_of_int 0) (big_int_of_int 1)),
896
eq_int (msd_ratio (create_ratio (big_int_of_int 1) (big_int_of_int 12)),
900
eq_int (msd_ratio (create_ratio (big_int_of_int 12) (big_int_of_int 1)),
904
eq_int (msd_ratio (create_ratio (big_int_of_int 1) (big_int_of_int 2)),
908
eq_int (msd_ratio (create_ratio (big_int_of_int 2) (big_int_of_int 1)),
912
eq_int (msd_ratio (create_ratio (big_int_of_int 25) (big_int_of_int 21)),
916
eq_int (msd_ratio (create_ratio (big_int_of_int 35) (big_int_of_int 21)),
920
eq_int (msd_ratio (create_ratio (big_int_of_int 215) (big_int_of_int 31)),
924
eq_int (msd_ratio (create_ratio (big_int_of_int 2) (big_int_of_int 30)),
928
eq_int (msd_ratio (create_ratio (big_int_of_int 2345)
929
(big_int_of_int 23456)),
933
eq_int (msd_ratio (create_ratio (big_int_of_int 2345)
934
(big_int_of_int 2346)),
938
eq_int (msd_ratio (create_ratio (big_int_of_int 2345)
939
(big_int_of_int 2344)),
943
eq_int (msd_ratio (create_ratio (big_int_of_int 23456)
944
(big_int_of_int 2345)),
948
eq_int (msd_ratio (create_ratio (big_int_of_int 23467)
949
(big_int_of_int 2345)),
953
msd_ratio (create_ratio (big_int_of_int 1) (big_int_of_int 0))
954
("msd_ratio "^infinite_failure)
957
msd_ratio (create_ratio (big_int_of_int (-1)) (big_int_of_int 0))
958
("msd_ratio "^infinite_failure)
961
msd_ratio (create_ratio (big_int_of_int 0) (big_int_of_int 0))
962
("msd_ratio "^infinite_failure)
964
*************************)
966
testing_function "round_futur_last_digit"
970
test 1 eq (round_futur_last_digit s 1 (pred (String.length s)),
972
test 2 eq_string (s, "+123466")
976
test 3 eq (round_futur_last_digit s 0 (String.length s), false) &&
977
test 4 eq_string (s, "123466")
981
test 5 eq (round_futur_last_digit s 1 (pred (String.length s)),
983
test 6 eq_string (s, "-123466")
987
test 7 eq (round_futur_last_digit s 1 (pred (String.length s)),
989
test 8 eq_string (s, "+123506")
993
test 9 eq (round_futur_last_digit s 0 (String.length s), false) &&
994
test 10 eq_string (s, "123506")
998
test 11 eq (round_futur_last_digit s 1 (pred (String.length s)),
1000
test 12 eq_string (s, "-123506")
1004
test 13 eq (round_futur_last_digit s 1 (pred (String.length s)),
1006
test 14 eq_string (s, "+006")
1010
test 15 eq (round_futur_last_digit s 0 (String.length s), true) &&
1011
test 16 eq_string (s, "006")
1015
test 17 eq (round_futur_last_digit s 1 (pred (String.length s)),
1017
test 18 eq_string (s, "-006")
1020
let s = "+6666666" in
1021
test 19 eq (round_futur_last_digit s 1 (pred (String.length s)),
1023
test 20 eq_string (s, "+6666676")
1026
let s = "6666666" in
1027
test 21 eq (round_futur_last_digit s 0 (String.length s), false) &&
1028
test 22 eq_string (s, "6666676")
1031
let s = "-6666666" in
1032
test 23 eq (round_futur_last_digit s 1 (pred (String.length s)),
1034
test 24 eq_string (s, "-6666676")
1037
testing_function "approx_ratio_fix"
1040
let s = approx_ratio_fix 5
1041
(create_ratio (big_int_of_int 2)
1042
(big_int_of_int 3)) in
1044
eq_string (s, "+0.66667")
1048
eq_string (approx_ratio_fix 5
1049
(create_ratio (big_int_of_int 20)
1050
(big_int_of_int 3)),
1054
eq_string (approx_ratio_fix 5
1055
(create_ratio (big_int_of_int 2)
1056
(big_int_of_int 30)),
1060
eq_string (approx_ratio_fix 5
1061
(create_ratio (big_int_of_string "999996")
1062
(big_int_of_string "1000000")),
1066
eq_string (approx_ratio_fix 5
1067
(create_ratio (big_int_of_string "299996")
1068
(big_int_of_string "100000")),
1072
eq_string (approx_ratio_fix 5
1073
(create_ratio (big_int_of_string "2999996")
1074
(big_int_of_string "1000000")),
1078
eq_string (approx_ratio_fix 4
1079
(create_ratio (big_int_of_string "299996")
1080
(big_int_of_string "100000")),
1084
eq_string (approx_ratio_fix 5
1085
(create_ratio (big_int_of_int 29996)
1086
(big_int_of_string "100000")),
1090
eq_string (approx_ratio_fix 5
1091
(create_ratio (big_int_of_int 0)
1092
(big_int_of_int 1)),
1096
(approx_ratio_fix 5) (create_ratio (big_int_of_int 1) (big_int_of_int 0))
1097
(Failure "approx_ratio_fix infinite or undefined rational number")
1100
(approx_ratio_fix 5) (create_ratio (big_int_of_int 0) (big_int_of_int 0))
1101
(Failure "approx_ratio_fix infinite or undefined rational number")
1106
eq_string (approx_ratio_fix 8
1107
(create_ratio (big_int_of_int 9603)
1108
(big_int_of_string "100000000000")),
1113
eq_string (approx_ratio_fix 1
1114
(create_ratio (big_int_of_int 94)
1115
(big_int_of_int 1000)),
1119
eq_string (approx_ratio_fix 1
1120
(create_ratio (big_int_of_int 49)
1121
(big_int_of_int 1000)),
1125
testing_function "approx_ratio_exp"
1129
eq_string (approx_ratio_exp 5
1130
(create_ratio (big_int_of_int 2)
1131
(big_int_of_int 3)),
1135
eq_string (approx_ratio_exp 5
1136
(create_ratio (big_int_of_int 20)
1137
(big_int_of_int 3)),
1141
eq_string (approx_ratio_exp 5
1142
(create_ratio (big_int_of_int 2)
1143
(big_int_of_int 30)),
1147
eq_string (approx_ratio_exp 5
1148
(create_ratio (big_int_of_string "999996")
1149
(big_int_of_string "1000000")),
1153
eq_string (approx_ratio_exp 5
1154
(create_ratio (big_int_of_string "299996")
1155
(big_int_of_string "100000")),
1159
eq_string (approx_ratio_exp 5
1160
(create_ratio (big_int_of_int 29996)
1161
(big_int_of_string "100000")),
1165
eq_string (approx_ratio_exp 5
1166
(create_ratio (big_int_of_int 0)
1167
(big_int_of_int 1)),
1171
(approx_ratio_exp 5) (create_ratio (big_int_of_int 1) (big_int_of_int 0))
1172
(Failure "approx_ratio_exp infinite or undefined rational number")
1175
(approx_ratio_exp 5) (create_ratio (big_int_of_int 0) (big_int_of_int 0))
1176
(Failure "approx_ratio_exp infinite or undefined rational number")