Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d18ad58

Browse files
committed
Added frexp, ldexp, modf, fmod.
1 parent 3d3037d commit d18ad58

1 file changed

Lines changed: 75 additions & 12 deletions

File tree

Modules/mathmodule.c

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ FUNC1(math_cosh, cosh)
8787
FUNC1(math_exp, exp)
8888
FUNC1(math_fabs, fabs)
8989
FUNC1(math_floor, floor)
90-
#if 0
90+
#ifndef AMOEBA
9191
/* XXX This one is not in the Amoeba library yet, so what the heck... */
9292
FUNC2(math_fmod, fmod)
9393
#endif
@@ -104,12 +104,77 @@ FUNC1(math_sqrt, sqrt)
104104
FUNC1(math_tan, tan)
105105
FUNC1(math_tanh, tanh)
106106

107-
#if 0
108-
/* What about these? */
109-
double frexp(double x, int *i);
110-
double ldexp(double x, int n);
111-
double modf(double x, double *i);
112-
#endif
107+
double frexp(double, int *);
108+
double ldexp(double, int);
109+
double modf(double, double *);
110+
111+
static object *
112+
math_frexp(self, args)
113+
object *self;
114+
object *args;
115+
{
116+
object *v;
117+
double x;
118+
int i;
119+
if (!getdoublearg(args, &x))
120+
return NULL;
121+
errno = 0;
122+
x = frexp(x, &i);
123+
if (errno != 0)
124+
return err_errno(RuntimeError);
125+
v = newtupleobject(2);
126+
if (v != NULL) {
127+
settupleitem(v, 0, newfloatobject(x));
128+
settupleitem(v, 1, newintobject((long)i));
129+
if (err_occurred()) {
130+
DECREF(v);
131+
v = NULL;
132+
}
133+
}
134+
return v;
135+
}
136+
137+
static object *
138+
math_ldexp(self, args)
139+
object *self;
140+
object *args;
141+
{
142+
double x, y;
143+
/* Cheat -- allow float as second argument */
144+
if (!get2doublearg(args, &x, &y))
145+
return NULL;
146+
errno = 0;
147+
x = ldexp(x, (int)y);
148+
if (errno != 0)
149+
return err_errno(RuntimeError);
150+
else
151+
return newfloatobject(x);
152+
}
153+
154+
static object *
155+
math_modf(self, args)
156+
object *self;
157+
object *args;
158+
{
159+
object *v;
160+
double x, y;
161+
if (!getdoublearg(args, &x))
162+
return NULL;
163+
errno = 0;
164+
x = modf(x, &y);
165+
if (errno != 0)
166+
return err_errno(RuntimeError);
167+
v = newtupleobject(2);
168+
if (v != NULL) {
169+
settupleitem(v, 0, newfloatobject(x));
170+
settupleitem(v, 1, newfloatobject(y));
171+
if (err_occurred()) {
172+
DECREF(v);
173+
v = NULL;
174+
}
175+
}
176+
return v;
177+
}
113178

114179
static struct methodlist math_methods[] = {
115180
{"acos", math_acos},
@@ -122,16 +187,14 @@ static struct methodlist math_methods[] = {
122187
{"exp", math_exp},
123188
{"fabs", math_fabs},
124189
{"floor", math_floor},
125-
#if 0
190+
#ifndef AMOEBA
126191
{"fmod", math_fmod},
127-
{"frexp", math_freqp},
128-
{"ldexp", math_ldexp},
129192
#endif
193+
{"frexp", math_frexp},
194+
{"ldexp", math_ldexp},
130195
{"log", math_log},
131196
{"log10", math_log10},
132-
#if 0
133197
{"modf", math_modf},
134-
#endif
135198
{"pow", math_pow},
136199
{"sin", math_sin},
137200
{"sinh", math_sinh},

0 commit comments

Comments
 (0)