- Rebuilt TinyPy

- Non-working trash is cleaned.
- Updated from latest git version. 
- Fixed modules pygame math and others. 
- Removed old modules added new ones.
- All samples work except "net"

git-svn-id: svn://kolibrios.org@8535 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
superturbocat2001
2021-01-12 23:18:45 +00:00
parent 44a5c1b211
commit b29cc6670d
59 changed files with 11516 additions and 6128 deletions

View File

@@ -0,0 +1,65 @@
#include "math.c"
/*
* init math module, namely, set its dictionary
*/
void math_init(TP)
{
/*
* new a module dict for math
*/
tp_obj math_mod = tp_dict(tp);
/*
* initialize pi and e
*/
math_pi = tp_number(M_PI);
math_e = tp_number(M_E);
/*
* bind math functions to math module
*/
tp_set(tp, math_mod, tp_string("pi"), math_pi);
tp_set(tp, math_mod, tp_string("e"), math_e);
tp_set(tp, math_mod, tp_string("acos"), tp_fnc(tp, math_acos));
tp_set(tp, math_mod, tp_string("asin"), tp_fnc(tp, math_asin));
tp_set(tp, math_mod, tp_string("atan"), tp_fnc(tp, math_atan));
tp_set(tp, math_mod, tp_string("atan2"), tp_fnc(tp, math_atan2));
tp_set(tp, math_mod, tp_string("ceil"), tp_fnc(tp, math_ceil));
tp_set(tp, math_mod, tp_string("cos"), tp_fnc(tp, math_cos));
tp_set(tp, math_mod, tp_string("cosh"), tp_fnc(tp, math_cosh));
tp_set(tp, math_mod, tp_string("degrees"), tp_fnc(tp, math_degrees));
tp_set(tp, math_mod, tp_string("exp"), tp_fnc(tp, math_exp));
tp_set(tp, math_mod, tp_string("fabs"), tp_fnc(tp, math_fabs));
tp_set(tp, math_mod, tp_string("floor"), tp_fnc(tp, math_floor));
tp_set(tp, math_mod, tp_string("fmod"), tp_fnc(tp, math_fmod));
tp_set(tp, math_mod, tp_string("frexp"), tp_fnc(tp, math_frexp));
tp_set(tp, math_mod, tp_string("hypot"), tp_fnc(tp, math_hypot));
tp_set(tp, math_mod, tp_string("ldexp"), tp_fnc(tp, math_ldexp));
tp_set(tp, math_mod, tp_string("log"), tp_fnc(tp, math_log));
tp_set(tp, math_mod, tp_string("log10"), tp_fnc(tp, math_log10));
tp_set(tp, math_mod, tp_string("modf"), tp_fnc(tp, math_modf));
tp_set(tp, math_mod, tp_string("pow"), tp_fnc(tp, math_pow));
tp_set(tp, math_mod, tp_string("radians"), tp_fnc(tp, math_radians));
tp_set(tp, math_mod, tp_string("sin"), tp_fnc(tp, math_sin));
tp_set(tp, math_mod, tp_string("sinh"), tp_fnc(tp, math_sinh));
tp_set(tp, math_mod, tp_string("sqrt"), tp_fnc(tp, math_sqrt));
tp_set(tp, math_mod, tp_string("tan"), tp_fnc(tp, math_tan));
tp_set(tp, math_mod, tp_string("tanh"), tp_fnc(tp, math_tanh));
/*
* bind special attributes to math module
*/
tp_set(tp, math_mod, tp_string("__doc__"),
tp_string(
"This module is always available. It provides access to the\n"
"mathematical functions defined by the C standard."));
tp_set(tp, math_mod, tp_string("__name__"), tp_string("math"));
tp_set(tp, math_mod, tp_string("__file__"), tp_string(__FILE__));
/*
* bind to tiny modules[]
*/
tp_set(tp, tp->modules, tp_string("math"), math_mod);
}

View File

@@ -0,0 +1,366 @@
#include <math.h>
#include "tinypy.h"
#ifndef M_E
#define M_E 2.7182818284590452354
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include <errno.h>
/*
* template for tinypy math functions
* with one parameter.
*
* @cfunc is the coresponding function name in C
* math library.
*/
#define TP_MATH_FUNC1(cfunc) \
static tp_obj math_##cfunc(TP) { \
double x = TP_NUM(); \
double r = 0.0; \
\
errno = 0; \
r = cfunc(x); \
if (errno == EDOM || errno == ERANGE) { \
tp_raise(tp_None, tp_printf(tp, "%s(x): x=%f " \
"out of range", __func__, x)); \
} \
\
return (tp_number(r)); \
}
/*
* template for tinypy math functions
* with two parameters.
*
* @cfunc is the coresponding function name in C
* math library.
*/
#define TP_MATH_FUNC2(cfunc) \
static tp_obj math_##cfunc(TP) { \
double x = TP_NUM(); \
double y = TP_NUM(); \
double r = 0.0; \
\
errno = 0; \
r = cfunc(x, y); \
if (errno == EDOM || errno == ERANGE) { \
tp_raise(tp_None, tp_printf(tp, "%s(x, y): x=%f,y=%f " \
"out of range", __func__, x, y)); \
} \
\
return (tp_number(r)); \
}
/*
* PI definition: 3.1415926535897931
*/
static tp_obj math_pi;
/*
* E definition: 2.7182818284590451
*/
static tp_obj math_e;
/*
* acos(x)
*
* return arc cosine of x, return value is measured in radians.
* if x falls out -1 to 1, raise out-of-range exception.
*/
TP_MATH_FUNC1(acos)
/*
* asin(x)
*
* return arc sine of x, measured in radians, actually [-PI/2, PI/2]
* if x falls out of -1 to 1, raise out-of-range exception
*/
TP_MATH_FUNC1(asin)
/*
* atan(x)
*
* return arc tangent of x, measured in radians,
*/
TP_MATH_FUNC1(atan)
/*
* atan2(x, y)
*
* return arc tangent of x/y, measured in radians.
* unlike atan(x/y), both the signs of x and y
* are considered to determine the quaderant of
* the result.
*/
TP_MATH_FUNC2(atan2)
/*
* ceil(x)
*
* return the ceiling of x, i.e, the smallest
* integer >= x.
*/
TP_MATH_FUNC1(ceil)
/*
* cos(x)
*
* return cosine of x. x is measured in radians.
*/
TP_MATH_FUNC1(cos)
/*
* cosh(x)
*
* return hyperbolic cosine of x.
*/
TP_MATH_FUNC1(cosh)
/*
* degrees(x)
*
* converts angle x from radians to degrees.
* NOTE: this function is introduced by python,
* so we cannot wrap it directly in TP_MATH_FUNC1(),
* here the solution is defining a new
* C function - degrees().
*/
static const double degToRad =
3.141592653589793238462643383 / 180.0;
static double degrees(double x)
{
return (x / degToRad);
}
TP_MATH_FUNC1(degrees)
/*
* exp(x)
*
* return the value e raised to power of x.
* e is the base of natural logarithms.
*/
TP_MATH_FUNC1(exp)
/*
* fabs(x)
*
* return the absolute value of x.
*/
TP_MATH_FUNC1(fabs)
/*
* floor(x)
*
* return the floor of x, i.e, the largest integer <= x
*/
TP_MATH_FUNC1(floor)
/*
* fmod(x, y)
*
* return the remainder of dividing x by y. that is,
* return x - n * y, where n is the quotient of x/y.
* NOTE: this function relies on the underlying platform.
*/
TP_MATH_FUNC2(fmod)
/*
* frexp(x)
*
* return a pair (r, y), which satisfies:
* x = r * (2 ** y), and r is normalized fraction
* which is laid between 1/2 <= abs(r) < 1.
* if x = 0, the (r, y) = (0, 0).
*/
static tp_obj math_frexp(TP) {
double x = TP_NUM();
int y = 0;
double r = 0.0;
tp_obj rList = tp_list(tp);
errno = 0;
r = frexp(x, &y);
if (errno == EDOM || errno == ERANGE) {
tp_raise(tp_None, tp_printf(tp, "%s(x): x=%f, "
"out of range", __func__, x));
}
_tp_list_append(tp, rList.list.val, tp_number(r));
_tp_list_append(tp, rList.list.val, tp_number((tp_num)y));
return (rList);
}
/*
* hypot(x, y)
*
* return Euclidean distance, namely,
* sqrt(x*x + y*y)
*/
TP_MATH_FUNC2(hypot)
/*
* ldexp(x, y)
*
* return the result of multiplying x by 2
* raised to y.
*/
TP_MATH_FUNC2(ldexp)
/*
* log(x, [base])
*
* return logarithm of x to given base. If base is
* not given, return the natural logarithm of x.
* Note: common logarithm(log10) is used to compute
* the denominator and numerator. based on fomula:
* log(x, base) = log10(x) / log10(base).
*/
static tp_obj math_log(TP) {
double x = TP_NUM();
tp_obj b = TP_DEFAULT(tp_None);
double y = 0.0;
double den = 0.0; /* denominator */
double num = 0.0; /* numinator */
double r = 0.0; /* result */
if (b.type == TP_NONE)
y = M_E;
else if (b.type == TP_NUMBER)
y = (double)b.number.val;
else
tp_raise(tp_None, tp_printf(tp, "%s(x, [base]): base invalid", __func__));
errno = 0;
num = log10(x);
if (errno == EDOM || errno == ERANGE)
goto excep;
errno = 0;
den = log10(y);
if (errno == EDOM || errno == ERANGE)
goto excep;
r = num / den;
return (tp_number(r));
excep:
tp_raise(tp_None, tp_printf(tp, "%s(x, y): x=%f,y=%f "
"out of range", __func__, x, y));
}
/*
* log10(x)
*
* return 10-based logarithm of x.
*/
TP_MATH_FUNC1(log10)
/*
* modf(x)
*
* return a pair (r, y). r is the integral part of
* x and y is the fractional part of x, both holds
* the same sign as x.
*/
static tp_obj math_modf(TP) {
double x = TP_NUM();
double y = 0.0;
double r = 0.0;
tp_obj rList = tp_list(tp);
errno = 0;
r = modf(x, &y);
if (errno == EDOM || errno == ERANGE) {
tp_raise(tp_None, tp_printf(tp, "%s(x): x=%f, "
"out of range", __func__, x));
}
_tp_list_append(tp, rList.list.val, tp_number(r));
_tp_list_append(tp, rList.list.val, tp_number(y));
return (rList);
}
/*
* pow(x, y)
*
* return value of x raised to y. equivalence of x ** y.
* NOTE: conventionally, tp_pow() is the implementation
* of builtin function pow(); whilst, math_pow() is an
* alternative in math module.
*/
static tp_obj math_pow(TP) {
double x = TP_NUM();
double y = TP_NUM();
double r = 0.0;
errno = 0;
r = pow(x, y);
if (errno == EDOM || errno == ERANGE) {
tp_raise(tp_None, tp_printf(tp, "%s(x, y): x=%f,y=%f "
"out of range", __func__, x, y));
}
return (tp_number(r));
}
/*
* radians(x)
*
* converts angle x from degrees to radians.
* NOTE: this function is introduced by python,
* adopt same solution as degrees(x).
*/
static double radians(double x)
{
return (x * degToRad);
}
TP_MATH_FUNC1(radians)
/*
* sin(x)
*
* return sine of x, x is measured in radians.
*/
TP_MATH_FUNC1(sin)
/*
* sinh(x)
*
* return hyperbolic sine of x.
* mathematically, sinh(x) = (exp(x) - exp(-x)) / 2.
*/
TP_MATH_FUNC1(sinh)
/*
* sqrt(x)
*
* return square root of x.
* if x is negtive, raise out-of-range exception.
*/
TP_MATH_FUNC1(sqrt)
/*
* tan(x)
*
* return tangent of x, x is measured in radians.
*/
TP_MATH_FUNC1(tan)
/*
* tanh(x)
*
* return hyperbolic tangent of x.
* mathematically, tanh(x) = sinh(x) / cosh(x).
*/
TP_MATH_FUNC1(tanh)

View File

@@ -0,0 +1,176 @@
### This file is grabbed from Python's Lib/test/test_math.py
###---------------------------------------------------------
# Python test set -- math module
# XXXX Should not do tests around zero only
eps = 0.00001
#print('math module, testing with eps ', eps)
import math
def testit(name, value, expected):
if abs(value-expected) > eps:
msg = name + " returned " + str(value) + " expected " + str(expected)
raise msg
#print 'constants'
testit('pi', math.pi, 3.1415926)
testit('e', math.e, 2.7182818)
#print 'acos'
testit('acos(-1)', math.acos(-1), math.pi)
testit('acos(0)', math.acos(0), math.pi/2)
testit('acos(1)', math.acos(1), 0)
#print 'asin'
testit('asin(-1)', math.asin(-1), -math.pi/2)
testit('asin(0)', math.asin(0), 0)
testit('asin(1)', math.asin(1), math.pi/2)
#print 'atan'
testit('atan(-1)', math.atan(-1), -math.pi/4)
testit('atan(0)', math.atan(0), 0)
testit('atan(1)', math.atan(1), math.pi/4)
#print 'atan2'
testit('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
testit('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
testit('atan2(0, 1)', math.atan2(0, 1), 0)
testit('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
testit('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
#print 'ceil'
testit('ceil(0.5)', math.ceil(0.5), 1)
testit('ceil(1.0)', math.ceil(1.0), 1)
testit('ceil(1.5)', math.ceil(1.5), 2)
testit('ceil(-0.5)', math.ceil(-0.5), 0)
testit('ceil(-1.0)', math.ceil(-1.0), -1)
testit('ceil(-1.5)', math.ceil(-1.5), -1)
#print 'cos'
testit('cos(-pi/2)', math.cos(-math.pi/2), 0)
testit('cos(0)', math.cos(0), 1)
testit('cos(pi/2)', math.cos(math.pi/2), 0)
testit('cos(pi)', math.cos(math.pi), -1)
#print 'cosh'
testit('cosh(0)', math.cosh(0), 1)
testit('cosh(2)-2*(cosh(1)**2)', math.cosh(2)-2*(math.cosh(1)**2), -1) # Thanks to Lambert
#print 'degrees'
testit('degrees(pi)', math.degrees(math.pi), 180.0)
testit('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
testit('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
#print 'exp'
testit('exp(-1)', math.exp(-1), 1/math.e)
testit('exp(0)', math.exp(0), 1)
testit('exp(1)', math.exp(1), math.e)
#print 'fabs'
testit('fabs(-1)', math.fabs(-1), 1)
testit('fabs(0)', math.fabs(0), 0)
testit('fabs(1)', math.fabs(1), 1)
#print 'floor'
testit('floor(0.5)', math.floor(0.5), 0)
testit('floor(1.0)', math.floor(1.0), 1)
testit('floor(1.5)', math.floor(1.5), 1)
testit('floor(-0.5)', math.floor(-0.5), -1)
testit('floor(-1.0)', math.floor(-1.0), -1)
testit('floor(-1.5)', math.floor(-1.5), -2)
#print 'fmod'
testit('fmod(10,1)', math.fmod(10,1), 0)
testit('fmod(10,0.5)', math.fmod(10,0.5), 0)
testit('fmod(10,1.5)', math.fmod(10,1.5), 1)
testit('fmod(-10,1)', math.fmod(-10,1), 0)
testit('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
testit('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
#print 'frexp'
def testfrexp(name, value, expected):
mant = value[0]
exp = value[1]
emant = expected[0]
eexp = expected[1]
if abs(mant-emant) > eps or exp != eexp:
raise '%s returned (%f, %f), expected (%f, %f)'%\
(name, mant, exp, emant,eexp)
testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
testfrexp('frexp(0)', math.frexp(0), (0, 0))
testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
#print 'hypot'
testit('hypot(0,0)', math.hypot(0,0), 0)
testit('hypot(3,4)', math.hypot(3,4), 5)
#print 'ldexp'
testit('ldexp(0,1)', math.ldexp(0,1), 0)
testit('ldexp(1,1)', math.ldexp(1,1), 2)
testit('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
testit('ldexp(-1,1)', math.ldexp(-1,1), -2)
#print 'log'
testit('log(1/e)', math.log(1/math.e), -1)
testit('log(1)', math.log(1), 0)
testit('log(e)', math.log(math.e), 1)
testit('log(32,2)', math.log(32,2), 5)
testit('log(10**40, 10)', math.log(10**40, 10), 40)
testit('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
#print 'log10'
testit('log10(0.1)', math.log10(0.1), -1)
testit('log10(1)', math.log10(1), 0)
testit('log10(10)', math.log10(10), 1)
#print 'modf'
def testmodf(name, value, expected):
v1 = value[0]
v2 = value[1]
e1 = expected[0]
e2 = expected[1]
if abs(v1-e1) > eps or abs(v2-e2):
raise '%s returned (%f, %f), expected (%f, %f)'%\
(name, v1,v2, e1,e2)
testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
#print 'pow'
testit('pow(0,1)', math.pow(0,1), 0)
testit('pow(1,0)', math.pow(1,0), 1)
testit('pow(2,1)', math.pow(2,1), 2)
testit('pow(2,-1)', math.pow(2,-1), 0.5)
#print 'radians'
testit('radians(180)', math.radians(180), math.pi)
testit('radians(90)', math.radians(90), math.pi/2)
testit('radians(-45)', math.radians(-45), -math.pi/4)
#print 'sin'
testit('sin(0)', math.sin(0), 0)
testit('sin(pi/2)', math.sin(math.pi/2), 1)
testit('sin(-pi/2)', math.sin(-math.pi/2), -1)
#print 'sinh'
testit('sinh(0)', math.sinh(0), 0)
testit('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
testit('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
#print 'sqrt'
testit('sqrt(0)', math.sqrt(0), 0)
testit('sqrt(1)', math.sqrt(1), 1)
testit('sqrt(4)', math.sqrt(4), 2)
#print 'tan'
testit('tan(0)', math.tan(0), 0)
testit('tan(pi/4)', math.tan(math.pi/4), 1)
testit('tan(-pi/4)', math.tan(-math.pi/4), -1)
#print 'tanh'
testit('tanh(0)', math.tanh(0), 0)
testit('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
#print("OK: math module test pass")