Skip to content

Commit

Permalink
Add math functions to posix and metro-snek builds. Add tests.
Browse files Browse the repository at this point in the history
These implement most of the standard Python3 math functions, except
for math.frexp, math.fsum and math.modf

These are optional and not included in the snek-duino build

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Mar 26, 2019
1 parent a619e6b commit f5eab24
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 4 deletions.
4 changes: 3 additions & 1 deletion metro-snek/Makefile
Expand Up @@ -72,11 +72,13 @@ SNEK_LOCAL_SRC = \
ao_snek.c \
snek_altos.c \
snek_eeprom.c \
snek-math.c \
snek_gpio.c \
snek_io.c

SNEK_LOCAL_BUILTINS = \
snek-altos.builtin
snek-altos.builtin \
../snek-math.builtin

SNEK_ROOT = $(HOME)/src/snek

Expand Down
5 changes: 3 additions & 2 deletions posix/Makefile
Expand Up @@ -17,14 +17,15 @@ SNEK_ROOT=..
SNEK_LOCAL_SRC = \
snek-main.c \
snek-posix.c \
snek-math.c \
snek-curses.c

SNEK_LOCAL_INC = snek-posix.h
SNEK_LOCAL_BUILTINS = snek-posix.builtin
SNEK_LOCAL_BUILTINS = snek-posix.builtin ../snek-math.builtin

include $(SNEK_ROOT)/snek-install.defs

OPT?=-O0
OPT?=-O99

CFLAGS+=-DSNEK_MEM_INCLUDE_NAME $(OPT) -g -I. $(SNEK_CFLAGS) -Werror $(CPPFLAGS)

Expand Down
4 changes: 3 additions & 1 deletion snek-builtin.py
Expand Up @@ -79,7 +79,9 @@ def load_builtins(filename):
f = open(filename)
for line in f.readlines():
line = line.rstrip()
if line[0] == '#':
if len(line) == 0:
pass
elif line[0] == '#':
if len(line) > 1 and line[1] != ' ':
headers += [line]
else:
Expand Down
25 changes: 25 additions & 0 deletions snek-lex.c
Expand Up @@ -536,6 +536,31 @@ snek_lex(void)
case SNEK_BUILTIN_True:
snek_token_val.number = 1.0f;
RETURN(NUMBER);
#ifdef SNEK_BUILTIN_math_pi
case SNEK_BUILTIN_math_pi:
snek_token_val.number = M_PI;
RETURN(NUMBER);
#endif
#ifdef SNEK_BUILTIN_math_e
case SNEK_BUILTIN_math_e:
snek_token_val.number = M_E;
RETURN(NUMBER);
#endif
#ifdef SNEK_BUILTIN_math_tau
case SNEK_BUILTIN_math_tau:
snek_token_val.number = 2 * M_PI;
RETURN(NUMBER);
#endif
#ifdef SNEK_BUILTIN_math_inf
case SNEK_BUILTIN_math_inf:
snek_token_val.number = INFINITY;
RETURN(NUMBER);
#endif
#ifdef SNEK_BUILTIN_math_nan
case SNEK_BUILTIN_math_nan:
snek_token_val.number = NAN;
RETURN(NUMBER);
#endif
}

snek_token_val.id = id;
Expand Down
89 changes: 89 additions & 0 deletions snek-math.builtin
@@ -0,0 +1,89 @@
#
# Copyright © 2019 Keith Packard <keithp@keithp.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#

#
# Number-theoretic and representation functions
#
math.ceil, 1
math.copysign, 2
math.fabs, 1
math.factorial, 1
math.floor, 1
math.fmod, 2
# math.frexp, 1
# math.fsum, 1
math.gcd, 2
math.isclose, -1
rel_tol,-2
abs_tol,-2
math.isfinite, 1
math.isinf, 1
math.isnan, 1
math.ldexp, 2
# math.modf, 1
math.remainder, 2
math.trunc, 1
math.round, 1
#
# Power and logarithmic functions
#
math.exp, 1
math.expm1, 1
math.exp2, 1
math.log, 1
math.log1p, 1
math.log2, 1
math.log10, 1
math.pow, 2
math.cbrt, 1
#
# Trigonometric functions
#
math.acos, 1
math.asin, 1
math.atan, 1
math.atan2, 2
math.cos, 1
math.hypot, 2
math.sin, 1
math.tan, 1
#
# Angular conversion
#
math.degrees, 1
math.radians, 1
#
# Hyperbolic functions
#
math.acosh, 1
math.asinh, 1
math.atanh, 1
math.cosh, 1
math.sinh, 1
math.tanh, 1
#
# Special functions
#
math.erf, 1
math.erfc, 1
math.gamma, 1
math.lgamma, 1
#
# constants
#
math.pi, -2
math.e, -2
math.tau, -2
math.inf, -2
math.nan, -2
148 changes: 148 additions & 0 deletions snek-math.c
@@ -0,0 +1,148 @@
/*
* Copyright © 2019 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/

#include "snek.h"
#include <math.h>

#define make_builtin(name) snek_builtin_ ## name

#define m1(name,func) snek_poly_t make_builtin(name)(snek_poly_t a) { return snek_float_to_poly(func(snek_poly_to_float(a))); }
#define m2(name,func) snek_poly_t make_builtin(name)(snek_poly_t a, snek_poly_t b) { return snek_float_to_poly(func(snek_poly_to_float(a), snek_poly_to_float(b))); }

m1(math_ceil, ceilf)
m2(math_copysign, copysignf)
m1(math_fabs, fabsf)

static float
factorialf(float f)
{
float result = 1;
if (f > 35)
return (float) INFINITY;
for (float x = 2; x <= f; x++)
result *= x;
return result;
}

m1(math_factorial, factorialf)
m1(math_floor, floorf)
m2(math_fmod, fmodf)
/* frexp */
/* fsum */

static float
gcdf(float af, float bf)
{
int a = fabsf(af);
int b = fabsf(bf);

if (a == 0 || b == 0)
return 0.0f;
while (a > 1 && b > 1) {
int r = a % b;
a = b;
b = r;
}
return (float) a;
}

m2(math_gcd, gcdf)

snek_poly_t
snek_builtin_math_isclose(uint8_t nposition, uint8_t nnamed, snek_poly_t *args)
{
if (nposition != 2) {
snek_error("wrong number of args: wanted 2, got %d", nposition);
return SNEK_NULL;
}
float af = snek_poly_to_float(*args++);
float bf = snek_poly_to_float(*args++);
float rel_tol = 1e-6f;
float abs_tol = 0.0f;

while (nnamed--) {
snek_id_t id = (snek_id_t) ((*args++).f);
float v = snek_poly_to_float(*args++);
switch (id) {
case SNEK_BUILTIN_rel_tol:
rel_tol = v;
break;
case SNEK_BUILTIN_abs_tol:
abs_tol = v;
break;
}
}
float dist = fabsf(af - bf);
float tol = fmaxf(rel_tol * fmaxf(fabsf(af), fabsf(bf)), abs_tol);
return snek_bool_to_poly(dist <= tol);
}

m1(math_isfinite, isfinite)
m1(math_isinf, isinff)
m1(math_isnan, isnanf)
m2(math_ldexp, ldexpf)
/* modf */
m2(math_remainder, remainderf)
m1(math_trunc, truncf)
m1(math_round, roundf)

m1(math_exp, expf)
m1(math_expm1, expm1f)
m1(math_exp2, exp2f)
m1(math_log, logf)
m1(math_log1p, log1pf)
m1(math_log2, log2f)
m1(math_log10, log10f)
m2(math_pow, powf)

m1(math_acos, acosf)
m1(math_asin, asinf)
m1(math_atan, atanf)
m2(math_atan2, atan2f)
m1(math_cos, cosf)
m2(math_hypot, hypotf)
m1(math_sin, sinf)
m1(math_tan, tanf)

static float
degreesf(float x) { return x * 180.0f / M_PI; }

static float
radiansf(float x) { return x * M_PI / 180.0f; }

m1(math_degrees, degreesf)
m1(math_radians, radiansf)

m1(math_acosh, acoshf);
m1(math_asinh, asinhf);
m1(math_atanh, atanhf);
m1(math_cosh, coshf);
m1(math_sinh, sinhf);
m1(math_tanh, tanhf);

m1(math_erf, erff);
m1(math_erfc, erfcf);
m1(math_gamma, tgammaf);

static float
_lgammaf(float f)
{
int sgn;
return lgammaf_r(f, &sgn);
}

m1(math_lgamma, _lgammaf);

m1(math_cbrt, cbrtf)

1 change: 1 addition & 0 deletions test/Makefile
Expand Up @@ -27,6 +27,7 @@ TESTS = \
for-nested.py \
global.py \
if.py \
math.py \
op.py \
range.py \
slice.py \
Expand Down

0 comments on commit f5eab24

Please sign in to comment.