Commit Graph

86 Commits

Author SHA1 Message Date
Damien George
7cae17fac7 tests/float/builtin_float_hash: Add test to improve objfloat.c coverage. 2017-12-19 14:50:33 +11:00
Damien George
84895f1a21 py/parsenum: Improve parsing of floating point numbers.
This patch improves parsing of floating point numbers by converting all the
digits (integer and fractional) together into a number 1 or greater, and
then applying the correct power of 10 at the very end.  In particular the
multiple "multiply by 0.1" operations to build a fraction are now combined
together and applied at the same time as the exponent, at the very end.

This helps to retain precision during parsing of floats, and also includes
a check that the number doesn't overflow during the parsing.  One benefit
is that a float will have the same value no matter where the decimal point
is located, eg 1.23 == 123e-2.
2017-11-27 12:51:52 +11:00
Damien George
a07fc5b640 py/objfloat: Allow float() to parse anything with the buffer protocol.
This generalises and simplifies the code and follows CPython behaviour.
2017-11-21 15:01:38 +11:00
Damien George
08a196697c py/formatfloat: Don't print the negative sign of a NaN value.
NaN may have the sign bit set but it has no meaning, so don't print it out.
2017-10-10 16:01:13 +11:00
Damien George
25e140652b py/modmath: Add full checks for math domain errors.
This patch changes how most of the plain math functions are implemented:
there are now two generic math wrapper functions that take a pointer to a
math function (like sin, cos) and perform the necessary conversion to and
from MicroPython types.  This helps to reduce code size.  The generic
functions can also check for math domain errors in a generic way, by
testing if the result is NaN or infinity combined with finite inputs.

The result is that, with this patch, all math functions now have full
domain error checking (even gamma and lgamma) and code size has decreased
for most ports.  Code size changes in bytes for those with the math module
are:

   unix x64:  -432
unix nanbox:  -792
      stm32:   -88
    esp8266:   +12

Tests are also added to check domain errors are handled correctly.
2017-10-10 15:57:45 +11:00
Damien George
bdc6e86e07 py/objfloat: Support raising a negative number to a fractional power.
This returns a complex number, following CPython behaviour.  For ports that
don't have complex numbers enabled this will raise a ValueError which gives
a fail-safe for scripts that were written assuming complex numbers exist.
2017-09-26 12:57:51 +10:00
Damien George
d4b75f6b68 py/obj: Fix comparison of float/complex NaN with itself.
IEEE floating point is specified such that a comparison of NaN with itself
returns false, and Python respects these semantics.  This patch makes uPy
also have these semantics.  The fix has a minor impact on the speed of the
object-equality fast-path, but that seems to be unavoidable and it's much
more important to have correct behaviour (especially in this case where
the wrong answer for nan==nan is silently returned).
2017-09-04 14:16:27 +10:00
Paul Sokolovsky
9950865c39 py/objfloat: Fix binary ops with incompatible objects.
These are now returned as "operation not supported" instead of raising
TypeError. In particular, this fixes equality for float vs incompatible
types, which now properly results in False instead of exception. This
also paves the road to support reverse operation (e.g. __radd__) with
float objects.

This is achieved by introducing mp_obj_get_float_maybe(), similar to
existing mp_obj_get_int_maybe().
2017-09-02 23:05:24 +03:00
Damien George
04552ff71b py: Implement raising a big-int to a negative power.
Before this patch raising a big-int to a negative power would just return
0.  Now it returns a floating-point number with the correct value.
2017-07-25 11:49:22 +10:00
Damien George
6b8b56f859 py/modmath: Check for zero division in log with 2 args. 2017-07-04 02:15:11 +10:00
Damien George
6ed4581f54 py/formatfloat: Fix number of digits and exponent sign when rounding.
This patch fixes 2 things when printing a floating-point number that
requires rounding up of the mantissa:
- retain the correct precision; eg 0.99 becomes 1.0, not 1.00
- if the exponent goes from -1 to 0 then render it as +0, not -0
2017-06-13 13:36:56 +10:00
Paul Sokolovsky
e094200750 tests/float/builtin_float_minmax: PEP8 fixes. 2017-06-08 17:23:22 +03:00
Damien George
fde54350a8 tests/float: Convert "sys.exit()" to "raise SystemExit".
The latter is shorter and simpler because it doesn't require importing the
sys module.
2017-06-08 14:00:57 +10:00
Damien George
677fb31015 tests/float: Add tests for hashing float and complex numbers. 2017-04-04 12:14:34 +10:00
Paul Sokolovsky
28876d3902 tests/float/byte*_construct: Skip on missing array module. 2017-04-03 00:17:43 +03:00
Damien George
bfb48c1620 tests/float: Add tests for round() of inf, nan and large number. 2017-03-24 11:00:45 +11:00
Damien George
bacb52aa2d tests/float: Add tests for math funcs that return ints.
One should test bigint, inf and nan to make sure all cases are covered.
2017-03-23 23:54:10 +11:00
Paul Sokolovsky
a0cbc108ba tests/float: Make various tests skippable. 2017-03-09 00:11:05 +01:00
Paul Sokolovsky
88ffe0d5cc tests/string_format_modulo2: Split off intbig test. 2017-03-07 00:13:36 +01:00
Paul Sokolovsky
4b03941f5e tests/float2int*: Suffix with _intbig, don't run on any other int type.
I.e. they don't run successfully with MICROPY_LONGINT_IMPL_NONE
and MICROPY_LONGINT_IMPL_LONGLONG (the problem is that they generate
different output than CPython, TODO to fix that).
2017-03-06 16:30:12 +01:00
Paul Sokolovsky
121fb88988 float/float2int*: Make actually be parsable for MICROPY_LONGINT_IMPL_NONE.
The use of large literal numbers is a big no-no when it comes to writing
programs which work with different int representations. Also, some checks
are pretty adhoc (e.g using struct module to check for 64-bitness). This
change bases entire detection on sys.maxsize and integer operarions, and
thus more correct, even if longer.

Note that this change doesn't mean that any of these tests can pass with
anything but MPZ - even despite checking for various int representations,
the tests aren't written to be portable among them.
2017-03-06 16:23:09 +01:00
Paul Sokolovsky
325c4473a5 tests/float/complex1: Split out intbig test. 2017-03-06 15:46:01 +01:00
Damien George
8a39e18f5f tests/float: Add tests for zero to a negative power. 2017-02-03 00:04:13 +11:00
Damien George
213a718953 tests/float: Add test for assigning to attribute of complex number. 2017-01-19 23:37:21 +11:00
Rami Ali
64dc925c4a tests/float: Improve formatfloat.c test coverage using Python. 2017-01-05 12:31:05 +11:00
Rami Ali
531c206e8b tests: Add tests to improve coverage of runtime.c. 2016-12-21 15:44:41 +11:00
Damien George
49bf7617d0 tests/float/builtin_float_round: Test round() with second arg. 2016-12-20 14:01:10 +11:00
Damien George
290daa15d9 tests/float: Add test for parsing a float from an empty string. 2016-09-27 15:44:56 +10:00
Damien George
e1e7657277 py/formatfloat: Fix further cases of buffer overflow in formatting.
Includes extensive test cases to catch hopefully all cases where
buffer might overflow.
2016-03-29 22:07:15 +01:00
Damien George
cea6cf8a5e py/formatfloat: Fix buffer overflow when formatting tiny numbers. 2016-03-15 12:21:56 +00:00
Paul Sokolovsky
d973c1bc12 test/string_format_fp30: Variant of string_format for 30-bit stuffed float. 2016-03-06 06:10:40 +02:00
Paul Sokolovsky
50e0a7b9d4 test/float2int_fp30: Variant of float2int for 30-bit stuffed float. 2016-03-06 06:08:38 +02:00
Paul Sokolovsky
d4315a6caf float/string_format: Split large test in 2. 2016-02-13 17:18:55 +02:00
Paul Sokolovsky
dc587a3623 test/float2int: Make test output clearer. 2016-02-13 17:03:02 +02:00
Damien George
3b936a5f4c tests: Fix math_fun_special test so it passes with single prec float. 2016-01-08 17:58:02 +00:00
Damien George
b1fa907d56 tests: Allow float tests to run when MATH_SPECIAL_FUNCTIONS is disabled. 2016-01-08 14:27:21 +00:00
Michael Buesch
17298af61e py/modmath: Add domain error checking to sqrt, log, log2, log10.
These functions will raise 'ValueError: math domain error' on invalid
input.
2015-12-12 15:12:54 +00:00
Paul Sokolovsky
b64e0575fd tests/float/string_format: Add testcase for incorrect rounding for %f. 2015-11-22 18:08:49 +02:00
Damien George
e5635f4ab3 py: Catch all cases of integer (big and small) division by zero. 2015-10-01 22:48:48 +01:00
Damien George
a81539db25 tests: Add further tests for mpz code. 2015-10-01 18:49:37 +01:00
Dave Hylands
9d6128acdc stmhal: fix single precision float printing error
Fixes #1435.
2015-09-11 23:09:50 +03:00
Damien George
2b000474d9 py/lexer: Properly classify floats that look like hex numbers.
Eg 0e0 almost looks like a hex number but in fact is a float.
2015-09-07 17:33:44 +01:00
Damien George
1d350b8ac6 tests: Add a few tests for bool, bytearray, float to improve coverage. 2015-08-29 23:13:28 +01:00
Damien George
d007cb8903 tests: Add more tests to improve coverage, mostly testing exceptions. 2015-08-21 12:02:09 +01:00
Sebastian Plamauer
1e8ca3a3cf modbuiltins: Implement round() to precision. 2015-07-19 21:49:44 +03:00
Damien George
6f49520042 py: Implement second arg for math.log (optional value for base). 2015-06-13 22:35:25 +01:00
Damien George
9ede4dcfbb tests: Add some tests for printing floats to improve coverage. 2015-05-28 14:24:47 +00:00
Damien George
79474c6b16 py: Remove unnecessary extra handling of padding of nan/inf.
C's printf will pad nan/inf differently to CPython.  Our implementation
originally conformed to C, now it conforms to CPython's way.

Tests for this are also added in this patch.
2015-05-28 14:22:12 +00:00
stijn
861670ba2a py: Implement mp_format_float for doubles and use where appropriate
This allows using (almost) the same code for printing floats everywhere,
removes the dependency on sprintf and uses just snprintf and
applies an msvc-specific fix for snprintf in a single place so
nan/inf are now printed correctly.
2015-05-17 21:47:11 +01:00
stijn
709955b601 py: Fix printing of complex number when imaginary part is nan 2015-05-13 23:10:15 +01:00