Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / mod.cpp
blob55bcd9d8339adbedba9008a418d1a147ce3dd993
1 #include "stdafx.h"
3 #include "defs.h"
5 void mod(void);
7 void
8 eval_mod(void)
10 push(cadr(p1));
11 eval();
12 push(caddr(p1));
13 eval();
14 mod();
17 void
18 mod(void)
20 int n;
22 save();
24 p2 = pop();
25 p1 = pop();
27 if (iszero(p2))
28 stop("mod function: divide by zero");
30 if (!isnum(p1) || !isnum(p2)) {
31 push_symbol(MOD);
32 push(p1);
33 push(p2);
34 list(3);
35 restore();
36 return;
39 if (isdouble(p1)) {
40 push(p1);
41 n = pop_integer();
42 if (n == (int) 0x80000000)
43 stop("mod function: cannot convert float value to integer");
44 push_integer(n);
45 p1 = pop();
48 if (isdouble(p2)) {
49 push(p2);
50 n = pop_integer();
51 if (n == (int) 0x80000000)
52 stop("mod function: cannot convert float value to integer");
53 push_integer(n);
54 p2 = pop();
57 if (!isinteger(p1) || !isinteger(p2))
58 stop("mod function: integer arguments expected");
60 p3 = alloc();
61 p3->k = NUM;
62 p3->u.q.a = mmod(p1->u.q.a, p2->u.q.a);
63 p3->u.q.b = mint(1);
64 push(p3);
66 restore();
69 #if SELFTEST
71 static char *s[] = {
73 "mod(2.0,3.0)",
74 "2",
76 "mod(-2.0,3.0)",
77 "-2",
79 "mod(2.0,-3.0)",
80 "2",
82 "mod(-2.0,-3.0)",
83 "-2",
85 "mod(2,3)",
86 "2",
88 "mod(-2,3)",
89 "-2",
91 "mod(2,-3)",
92 "2",
94 "mod(-2,-3)",
95 "-2",
97 "mod(a,b)",
98 "mod(a,b)",
100 "mod(2.0,0.0)",
101 "Stop: mod function: divide by zero",
103 "mod(2,0)",
104 "Stop: mod function: divide by zero",
106 "mod(1.2,2)",
107 "Stop: mod function: cannot convert float value to integer",
109 "mod(1/2,3)",
110 "Stop: mod function: integer arguments expected",
112 "mod(15,8.0)",
113 "7",
116 void
117 test_mod(void)
119 test(__FILE__, s, sizeof s / sizeof (char *));
122 #endif