Page 1 of 1
math module status
Posted: Mon Jun 30, 2014 7:06 am
by fma
Hi,
I tried playing with the math module, but I get strange results.
From unix port, I can't import this module:
Code: Select all
>>> import math
cannot open file math.py
From pyb, some functions does not work, and always return 0.0:
Code: Select all
>>> math.asin(0.5)
0.0
>>> math.acos(0.5)
0.0
>>> math.acosh(0.5)
0.0 # should give ValueError: math domain error
I found some issues in the git repo, but no clear description of the math module...
Re: math module status
Posted: Mon Jun 30, 2014 7:20 am
by dhylands
Hmm. That seems to work for me.
Code: Select all
581 >./micropython
Micro Python v1.1.1-60-g8993fb6-dirty on 2014-06-30; UNIX version
>>> import math
>>> math.cos(0.5)
0.87758256189037276
>>> math.acos(0)
1.5707963267948966
>>> math.acos(1)
0.0
>>> math.acos(0.87758256189037276)
0.49999999999999828
>>> math.acos(0.5)
1.0471975511965979
>>>
Which version of the tree do you have? I just checked out the latest and built it:
Code: Select all
583 >git log -1
commit 8993fb6cf0677ce980ab56cbad326e4e6bc47811
Author: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Sat Jun 28 02:25:04 2014 +0300
py: Add protection against printing too nested or recursive data structures.
With a test which cannot be automatically validated so far.
Re: math module status
Posted: Mon Jun 30, 2014 7:50 am
by fma
Ok, it looks like my unix version was out of date! Works fine now.
But it does not with the latest release of pyb firmware (pybv10-2014-06-27-v1.1.1-52-g3c8ce38.dfu); I still get 0.0 for most functions...
Re: math module status
Posted: Mon Jun 30, 2014 5:39 pm
by blmorris
At this point for the stmhal port many math functions are still implemented only as placeholders in math.c; lines 99-166 of math.c are as follows:
Code: Select all
// TODO we need import these functions from some library (eg musl or newlib)
float acoshf(float x) { return 0.0; }
float asinhf(float x) { return 0.0; }
float atanhf(float x) { return 0.0; }
float tanf(float x) { return 0.0; }
float acosf(float x) { return 0.0; }
float asinf(float x) { return 0.0; }
float atanf(float x) { return 0.0; }
float atan2f(float x, float y) { return 0.0; }
float fmodf(float x, float y) { return 0.0; }
float tgammaf(float x) { return 0.0; }
float lgammaf(float x) { return 0.0; }
float erff(float x) { return 0.0; }
float erfcf(float x) { return 0.0; }
float modff(float x, float *y) { return 0.0; }
float frexpf(float x, int *exp) { return 0.0; }
float ldexpf(float x, int exp) { return 0.0; }
I assume that the unix port just pulls these functions from glibc or whichever standard C library is available on the system, but I think the issue with the ARM/stmhal port is that these libraries are GPL'd and therefore are not license-compatible with Micro Python's MIT license (I could be mangling this explanation…) so each of these functions needs to be pulled and adapted from a compatible source. Looking at the functions that have already been implemented in math.c, I assume that this is busywork that nobody has gotten to yet - not that I am dismissing the difficulty, because I really don't know… maybe I should just give it a shot when I get a little time, but I hope someone else gets to it before that happens
Edited to add: It appears to this code novice that the basic and complex math modules are implemented in py/modmath.c and py/modcmath.c; depending on which port is being compiled (unix or stmhal) the functions are pulled either from math.h or else are defined in stmhal/math.c
-Bryan
Re: math module status
Posted: Mon Jun 30, 2014 6:12 pm
by fma
Thanks for the explanation!
Re: math module status
Posted: Mon Jun 30, 2014 11:21 pm
by pfalcon
I assume that this is busywork that nobody has gotten to yet - not that I am dismissing the difficulty, because I really don't know… maybe I should just give it a shot when I get a little time
That should be just the case, and you're welcome to look into that - newlib should be good source for those functions (but check license statement in each file you'd import).