aboutsummaryrefslogtreecommitdiff
path: root/py/objfloat.c
AgeCommit message (Collapse)Author
2022-05-03py/objfloat: Explain why mp_obj_malloc isn't used.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-01-23py/modmath: Add math.tau, math.nan and math.inf constants.stijn
Configurable by the new MICROPY_PY_MATH_CONSTANTS option.
2021-02-04py: Rename BITS_PER_BYTE to MP_BITS_PER_BYTE.Damien George
To give this macro a standard MP_ prefix. Signed-off-by: Damien George <damien@micropython.org>
2020-09-11py: Fix handling of NaN in certain pow implementations.stijn
Adds a new compile-time option MICROPY_PY_MATH_POW_FIX_NAN for use with toolchains that don't handle pow-of-NaN correctly.
2020-09-11py/objfloat: Fix handling of negative float to power of nan.Damien George
Prior to this commit, pow(-2, float('nan')) would return (nan+nanj), or raise an exception on targets that don't support complex numbers. This is fixed to return simply nan, as CPython does. Signed-off-by: Damien George <damien@micropython.org>
2020-04-18all: Fix implicit floating point promotion.stijn
Initially some of these were found building the unix coverage variant on MacOS because that build uses clang and has -Wdouble-promotion enabled, and clang performs more vigorous promotion checks than gcc. Additionally the codebase has been compiled with clang and msvc (the latter with warning level 3), and with MICROPY_FLOAT_IMPL_FLOAT to find the rest of the conversions. Fixes are implemented either as explicit casts, or by using the correct type, or by using one of the utility functions to handle floating point casting; these have been moved from nativeglue.c to the public API.
2020-04-05all: Use MP_ERROR_TEXT for all error messages.Jim Mussared
2020-02-28all: Reformat C and Python source code with tools/codeformat.py.Damien George
This is run with uncrustify 0.70.1, and black 19.10b0.
2020-02-28py: Removing dangling "else" to improve code format consistency.Damien George
2020-02-18py: Factor out definition of mp_float_union_t to one location.Damien George
2020-02-11py: Expand type equality flags to 3 separate ones, fix bool/namedtuple.Damien George
Both bool and namedtuple will check against other types for equality; int, float and complex for bool, and tuple for namedtuple. So to make them work after the recent commit 3aab54bf434e7f025a91ea05052f1bac439fad8c they would need MP_TYPE_FLAG_NEEDS_FULL_EQ_TEST set. But that makes all bool and namedtuple equality checks less efficient because mp_obj_equal_not_equal() could no longer short-cut x==x, and would need to try __ne__. To improve this, this commit splits the MP_TYPE_FLAG_NEEDS_FULL_EQ_TEST flags into 3 separate flags to give types more fine-grained control over how their equality behaves. These new flags are then used to fix bool and namedtuple equality. Fixes issue #5615 and #5620.
2020-01-30py: Support non-boolean results for equality and inequality tests.Nicko van Someren
This commit implements a more complete replication of CPython's behaviour for equality and inequality testing of objects. This addresses the issues discussed in #5382 and a few other inconsistencies. Improvements over the old code include: - Support for returning non-boolean results from comparisons (as used by numpy and others). - Support for non-reflexive equality tests. - Preferential use of __ne__ methods and MP_BINARY_OP_NOT_EQUAL binary operators for inequality tests, when available. - Fallback to op2 == op1 or op2 != op1 when op1 does not implement the (in)equality operators. The scheme here makes use of a new flag, MP_TYPE_FLAG_NEEDS_FULL_EQ_TEST, in the flags word of mp_obj_type_t to indicate if various shortcuts can or cannot be used when performing equality and inequality tests. Currently four built-in classes have the flag set: float and complex are non-reflexive (since nan != nan) while bytearray and frozenszet instances can equal other builtin class instances (bytes and set respectively). The flag is also set for any new class defined by the user. This commit also includes a more comprehensive set of tests for the behaviour of (in)equality operators implemented in special methods.
2019-02-12py: Downcase all MP_OBJ_IS_xxx macros to make a more consistent C API.Damien George
These macros could in principle be (inline) functions so it makes sense to have them lower case, to match the other C API functions. The remaining macros that are upper case are: - MP_OBJ_TO_PTR, MP_OBJ_FROM_PTR - MP_OBJ_NEW_SMALL_INT, MP_OBJ_SMALL_INT_VALUE - MP_OBJ_NEW_QSTR, MP_OBJ_QSTR_VALUE - MP_OBJ_FUN_MAKE_SIG - MP_DECLARE_CONST_xxx - MP_DEFINE_CONST_xxx These must remain macros because they are used when defining const data (at least, MP_OBJ_NEW_SMALL_INT is so it makes sense to have MP_OBJ_SMALL_INT_VALUE also a macro). For those macros that have been made lower case, compatibility macros are provided for the old names so that users do not need to change their code immediately.
2018-09-27py/objfloat: Fix abs(-0.0) so it returns 0.0.Damien George
Nan and inf (signed and unsigned) are also handled correctly by using signbit (they were also handled correctly with "val<0", but that didn't handle -0.0 correctly). A test case is added for this behaviour.
2018-09-20py: Shorten error messages by using contractions and some rewording.Damien George
2018-05-21py/objfloat: Fix undefined integer behavior hashing negative zero.Jeff Epler
Under ubsan, when evaluating hash(-0.) the following diagnostic occurs: ../../py/objfloat.c:102:15: runtime error: negation of -9223372036854775808 cannot be represented in type 'mp_int_t' (aka 'long'); cast to an unsigned type to negate this value to itself So do just that, to tell the compiler that we want to perform this operation using modulo arithmetic rules.
2018-05-21py/objfloat: Fix undefined shifting behavior in high-quality float hash.Jeff Epler
When computing e.g. hash(0.4e3) with ubsan enabled, a diagnostic like the following would occur: ../../py/objfloat.c:91:30: runtime error: shift exponent 44 is too large for 32-bit type 'int' By casting constant "1" to the right type the intended value is preserved.
2018-02-08py/objfloat: Fix case of raising 0 to -infinity.Damien George
It was raising an exception but it should return infinity.
2017-11-21py/objfloat: Allow float() to parse anything with the buffer protocol.Damien George
This generalises and simplifies the code and follows CPython behaviour.
2017-10-04all: Remove inclusion of internal py header files.Damien George
Header files that are considered internal to the py core and should not normally be included directly are: py/nlr.h - internal nlr configuration and declarations py/bc0.h - contains bytecode macro definitions py/runtime0.h - contains basic runtime enums Instead, the top-level header files to include are one of: py/obj.h - includes runtime0.h and defines everything to use the mp_obj_t type py/runtime.h - includes mpstate.h and hence nlr.h, obj.h, runtime0.h, and defines everything to use the general runtime support functions Additional, specific headers (eg py/objlist.h) can be included if needed.
2017-09-26py/objfloat: Support raising a negative number to a fractional power.Damien George
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-18py/{objfloat,objcomplex}: Optimise MP_UNARY_OP_ABS by reusing variables.Damien George
2017-09-18py/modbuiltins: Implement abs() by dispatching to MP_UNARY_OP_ABS.Paul Sokolovsky
This allows user classes to implement __abs__ special method, and saves code size (104 bytes for x86_64), even though during refactor, an issue was fixed and few optimizations were made: * abs() of minimum (negative) small int value is calculated properly. * objint_longlong and objint_mpz avoid allocating new object is the argument is already non-negative.
2017-09-02py/objfloat: Fix binary ops with incompatible objects.Paul Sokolovsky
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-08-29all: Convert mp_uint_t to mp_unary_op_t/mp_binary_op_t where appropriateDamien George
The unary-op/binary-op enums are already defined, and there are no arithmetic tricks used with these types, so it makes sense to use the correct enum type for arguments that take these values. It also reduces code size quite a bit for nan-boxing builds.
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-04-21py: Reduce str/repr precision of float numbers when floats are 30-bit.Damien George
With 30-bit floats there aren't enough bits to faithfully print 7 decimal digits, so reduce the precision to 6 digits.
2017-04-12py/objfloat: Add implementation of high-quality float hashing.Damien George
Disabled by default.
2017-04-04py: Add very simple but correct hashing for float and complex numbers.Damien George
Hashing of float and complex numbers that are exact (real) integers should return the same integer hash value as hashing the corresponding integer value. Eg hash(1), hash(1.0) and hash(1+0j) should all be the same (this is how Python is specified: if x==y then hash(x)==hash(y)). This patch implements the simplest way of doing float/complex hashing by just converting the value to int and returning that value.
2017-03-29py: Change mp_uint_t to size_t for mp_obj_str_get_data len arg.Damien George
2017-02-03py/objfloat: Raise ZeroDivisionError for 0 to negative power.Damien George
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-05-12py/objfloat, py/modmath: Ensure M_PI and M_E defined.Colin Hogben
In some compliation enviroments (e.g. mbed online compiler) with strict standards compliance, <math.h> does not define constants such as M_PI. Provide fallback definitions of M_E and M_PI where needed.
2016-01-11py: Change first arg of type.make_new from mp_obj_t to mp_obj_type_t*.Damien George
The first argument to the type.make_new method is naturally a uPy type, and all uses of this argument cast it directly to a pointer to a type structure. So it makes sense to just have it a pointer to a type from the very beginning (and a const pointer at that). This patch makes such a change, and removes all unnecessary casting to/from mp_obj_t.
2016-01-11py: Change type of .make_new and .call args: mp_uint_t becomes size_t.Damien George
This patch changes the type signature of .make_new and .call object method slots to use size_t for n_args and n_kw (was mp_uint_t. Makes code more efficient when mp_uint_t is larger than a machine word. Doesn't affect ports when size_t and mp_uint_t have the same size.
2015-11-29py: Add support for 64-bit NaN-boxing object model, on 32-bit machine.Damien George
To use, put the following in mpconfigport.h: #define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_D) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE) typedef int64_t mp_int_t; typedef uint64_t mp_uint_t; #define UINT_FMT "%llu" #define INT_FMT "%lld" Currently does not work with native emitter enabled.
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-20py: Add object repr "C", where 30-bit floats are stuffed in obj word.Damien George
This new object representation puts floats into the object word instead of on the heap, at the expense of reducing their precision to 30 bits. It only makes sense when the word size is 32-bits.
2015-10-20py: Make float representation configurable with object representation.Damien George
2015-10-20py: Move float e/pi consts to objfloat and make mp_obj_float_t private.Damien George
2015-10-20py: Add mp_obj_is_float function (macro) and use it where appropriate.Damien George
2015-10-11py: Rename MP_BOOL() to mp_obj_new_bool() for consistency in naming.Paul Sokolovsky
2015-06-13py: Add MP_BINARY_OP_DIVMOD to simplify and consolidate divmod builtin.Damien George
2015-05-17py: Implement mp_format_float for doubles and use where appropriatestijn
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-04-22py: Fix printing of "inf" and "nan" floating point values.Damien George
2015-04-16py: Overhaul and simplify printf/pfenv mechanism.Damien George
Previous to this patch the printing mechanism was a bit of a tangled mess. This patch attempts to consolidate printing into one interface. All (non-debug) printing now uses the mp_print* family of functions, mainly mp_printf. All these functions take an mp_print_t structure as their first argument, and this structure defines the printing backend through the "print_strn" function of said structure. Printing from the uPy core can reach the platform-defined print code via two paths: either through mp_sys_stdout_obj (defined pert port) in conjunction with mp_stream_write; or through the mp_plat_print structure which uses the MP_PLAT_PRINT_STRN macro to define how string are printed on the platform. The former is only used when MICROPY_PY_IO is defined. With this new scheme printing is generally more efficient (less layers to go through, less arguments to pass), and, given an mp_print_t* structure, one can call mp_print_str for efficiency instead of mp_printf("%s", ...). Code size is also reduced by around 200 bytes on Thumb2 archs.
2015-03-14py, extmod: Remove include of unnecessary system headers.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-20py, unix: Allow to compile with -Wunused-parameter.Damien George
See issue #699.
2015-01-01py: Move to guarded includes, everywhere in py/ core.Damien George
Addresses issue #1022.