summaryrefslogtreecommitdiff
path: root/py/parsenum.c
AgeCommit message (Collapse)Author
2019-09-26py: Rename MP_QSTR_NULL to MP_QSTRnull to avoid intern collisions.Josh Lloyd
Fixes #5140.
2018-09-20py/parsenum: Avoid rounding errors with negative powers-of-10.Romain Goyet
This patches avoids multiplying with negative powers-of-10 when parsing floating-point values, when those powers-of-10 can be exactly represented as a positive power. When represented as a positive power and used to divide, the resulting float will not have any rounding errors. The issue is that mp_parse_num_decimal will sometimes not give the closest floating representation of the input string. Eg for "0.3", which can't be represented exactly in floating point, mp_parse_num_decimal gives a slightly high (by 1LSB) result. This is because it computes the answer as 3 * 0.1, and since 0.1 also can't be represented exactly, multiplying by 3 multiplies up the rounding error in the 0.1. Computing it as 3 / 10, as now done by the change in this commit, gives an answer which is as close to the true value of "0.3" as possible.
2018-06-12py/lexer: Add support for underscores in numeric literals.Damien George
This is a very convenient feature introduced in Python 3.6 by PEP 515.
2018-05-22py/parsenum: Adjust braces so they are balanced.Damien George
2018-05-21py/parsenum: Avoid undefined behavior parsing floats with large exponents.Jeff Epler
Fuzz testing combined with the undefined behavior sanitizer found that parsing unreasonable float literals like 1e+9999999999999 resulted in undefined behavior due to overflow in signed integer arithmetic, and a wrong result being returned.
2018-05-21py/parsenum: Use int instead of mp_int_t for parsing float exponent.Damien George
There is no need to use the mp_int_t type which may be 64-bits wide, there is enough bit-width in a normal int to parse reasonable exponents. Using int helps to reduce code size for 64-bit ports, especially nan-boxing builds. (Similarly for the "dig" variable which is now an unsigned int.)
2018-02-08py/parsenum: Fix parsing of floats that are close to subnormal.Damien George
Prior to this patch, a float literal that was close to subnormal would have a loss of precision when parsed. The worst case was something like float('10000000000000000000e-326') which returned 0.0.
2017-11-27py/parsenum: Improve parsing of floating point numbers.Damien George
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-07-31all: Use the name MicroPython consistently in commentsAlexander Steffen
There were several different spellings of MicroPython present in comments, when there should be only one.
2017-03-28py: Use mp_raise_TypeError/mp_raise_ValueError helpers where possible.Damien George
Saves 168 bytes on bare-arm.
2016-12-28py/parsenum: Fix warning for signed/unsigned comparison.Damien George
2016-12-28py/parsenum: Simplify and generalise decoding of digit values.Damien George
This function should be able to parse integers with any value for the base, because it is called by int('xxx', base).
2016-11-03py: Add MICROPY_FLOAT_CONST macro for defining float constants.Damien George
All float constants in the core should use this macro to prevent unnecessary creation of double-precision floats, which makes code less efficient.
2016-10-17py: Use mp_raise_msg helper function where appropriate.Damien George
Saves the following number of bytes of code space: 176 for bare-arm, 352 for minimal, 272 for unix x86-64, 140 for stmhal, 120 for esp8266.
2016-03-29py/parsenum: Use pow function to apply exponent to decimal number.Damien George
Pow is already a dependency when compiling with floats, so may as well use it here to reduce code size and speed up the conversion for most cases.
2016-03-14py/parsenum: Fix compiler warnings for no decl and signed comparison.Damien George
2016-03-14py/parsenum: Use size_t to count bytes, and int for type of base arg.Damien George
size_t is the proper type to count number of bytes in a string. The base argument does not need to be a full mp_uint_t, int is enough.
2015-11-29py: Wrap all obj-ptr conversions in MP_OBJ_TO_PTR/MP_OBJ_FROM_PTR.Damien George
This allows the mp_obj_t type to be configured to something other than a pointer-sized primitive type. This patch also includes additional changes to allow the code to compile when sizeof(mp_uint_t) != sizeof(void*), such as using size_t instead of mp_uint_t, and various casts.
2015-10-01py/parsenum: Provide detailed error for int parsing with escaped bytes.Damien George
This patch adds more fine grained error message control for errors when parsing integers (now has terse, normal and detailed). When detailed is enabled, the error now escapes bytes when printing them so they can be more easily seen.
2015-06-23py: Clarify comment in parsenum.c about ValueError vs SyntaxError.Damien George
2015-06-23py: Change exception type to ValueError when error reporting is terse.Daniel Campora
Addresses issue #1347
2015-05-30py/parsenum.c: Rename "raise" func to "raise_exc" to avoid name clash.Damien George
"raise" is a common word that was found to exist in a vendor's stdlib.
2015-03-16py: Fix printing of error message when parsing malformed integer.Damien George
2015-02-08py: Parse big-int/float/imag constants directly in parser.Damien George
Previous to this patch, a big-int, float or imag constant was interned (made into a qstr) and then parsed at runtime to create an object each time it was needed. This is wasteful in RAM and not efficient. Now, these constants are parsed straight away in the parser and turned into objects. This allows constants with large numbers of digits (so addresses issue #1103) and takes us a step closer to #722.
2015-01-01py: Move to guarded includes, everywhere in py/ core.Damien George
Addresses issue #1022.
2014-11-06py: Use shorter, static error msgs when ERROR_REPORTING_TERSE enabled.Damien George
Going from MICROPY_ERROR_REPORTING_NORMAL to MICROPY_ERROR_REPORTING_TERSE now saves 2020 bytes ROM for ARM Thumb2, and 2200 bytes ROM for 32-bit x86. This is about a 2.5% code size reduction for bare-arm.
2014-07-03parser: Convert (u)int to mp_(u)int_t.Damien George
2014-07-03Rename machine_(u)int_t to mp_(u)int_t.Damien George
See discussion in issue #50.
2014-06-21py: Include mpconfig.h before all other includes.Paul Sokolovsky
It defines types used by all other headers. Fixes #691.
2014-06-20py: Separate MICROPY_PY_BUILTINS_COMPLEX from MICROPY_PY_BUILTINS_FLOAT.Paul Sokolovsky
One thing is wanting to do 1 / 2 and get something else but 0, and quite another - doing rocket science ;-).
2014-06-14parsenum: Signedness issues.Paul Sokolovsky
char can be signedness, and using signedness types is dangerous - it can lead to negative offsets when doing table lookups. We apparently should just ban char usage.
2014-06-01Rename bultins config variables to MICROPY_PY_BUILTINS_*.Damien George
This renames: MICROPY_PY_FROZENSET -> MICROPY_PY_BUILTINS_FROZENSET MICROPY_PY_PROPERTY -> MICROPY_PY_BUILTINS_PROPERTY MICROPY_PY_SLICE -> MICROPY_PY_BUILTINS_SLICE MICROPY_ENABLE_FLOAT -> MICROPY_PY_BUILTINS_FLOAT See issue #35 for discussion.
2014-05-28py: Fix check of small-int overflow when parsing ints.Damien George
Also unifies use of SMALL_INT_FITS macro across parser and runtime.
2014-05-28py: Implement long int parsing in int(...).Damien George
Addresses issue #627.
2014-05-03Add license header to (almost) all files.Damien George
Blanket wide to all .c and .h files. Some files originating from ST are difficult to deal with (license wise) so it was left out of those. Also merged modpyb.h, modos.h, modstm.h and modtime.h in stmhal/.
2014-04-09Remove exception name from inside the exception messageAndrew Scheller
This prevents micropython printing exception messages like ImportError: ImportError: No module named 'foo'
2014-04-05py: Change nlr_jump to nlr_raise, to aid in debugging.Damien George
This does not affect code size or performance when debugging turned off. To address issue #420.
2014-03-21py: Put back proper ValueError for badly parsed integers.Damien George
2014-03-21py: Improve mp_parse_num_integer; make it self contained.Damien George
2014-03-21py: Allow 'complex()' to take a string as first argument.Damien George
2014-03-21py: Implement parsing of infinity and nan for floats.Damien George
2014-03-17py: Clean up includes.xbe
Remove unnecessary includes. Add includes that improve portability.
2014-03-01py: Implement bit-shift and not operations for mpz.Damien George
Implement not, shl and shr in mpz library. Add function to create mpzs on the stack, used for memory efficiency when rhs is a small int. Factor out code to parse base-prefix of number into a dedicated function.
2014-02-22py: Put number parsing code together in parsenum.c.Damien George