python/cpython

/* Float object implementation */

/* XXX There have to be overflow checks here, alternatively or now not it’s arduous to test

for any roughly float exception with out shedding portability. */

#encompass “Python.h”

#encompass

#encompass

/*(*)

class float “PyObject *” “&PyFloat_Type”

(**)*/

/*(***)*/

#encompass “hospital/floatobject.c.h”

/* Special free checklist

free_list is a singly-linked checklist of on hand PyFloatObjects, linked

via abuse of their ob_type participants.

*/

#ifndef PyFloat_MAXFREELIST

#account for PyFloat_MAXFREELIST (***************************************************************************************)

#endif

static int numfree=0;

static PyFloatObject *free_list=NULL;

double

PyFloat_GetMax(void)

{

return DBL_MAX;

}

double

PyFloat_GetMin(void)

{

return DBL_MIN;

}

static PyTypeObject FloatInfoType;

PyDoc_STRVAR(floatinfo__doc__,

“sys.float_infon

n

A structseq keeping data in regards to the float style. It incorporates low leveln

data in regards to the precision and inner representation. Please researchn

your machine’s :file:`float.h` for added data.”);

static PyStructSequence_Field floatinfo_fields(****)={

{“max”, “DBL_MAX — maximum representable finite float”},

{“max_exp”, “DBL_MAX_EXP — maximum int e such that radix**(e-1) ”

“is representable”},

{“max_(***********************************************************************************************************************)_exp”, “DBL_MAX_(***********************************************************************************************************************)_EXP — maximum int e such that (***********************************************************************************************************************)**e ”

“is representable”},

{“min”, “DBL_MIN — Minimum certain normalized float”},

{“min_exp”, “DBL_MIN_EXP — minimum int e such that radix**(e-1) ”

“is a normalized float”},

{“min_(***********************************************************************************************************************)_exp”, “DBL_MIN_(***********************************************************************************************************************)_EXP — minimum int e such that (***********************************************************************************************************************)**e is ”

“a normalized”},

{“dig”, “DBL_DIG — digits”},

{“mant_dig”, “DBL_MANT_DIG — mantissa digits”},

{“epsilon”, “DBL_EPSILON — Distinction between 1 and the next ”

“representable float”},

{“radix”, “FLT_RADIX — radix of exponent”},

{“rounds”, “FLT_ROUNDS — rounding mode”},

{0}

};

static PyStructSequence_Desc floatinfo_desc={

“sys.float_info”, /* name */

floatinfo__doc__, /* doc */

floatinfo_fields, /* fields */

(**********************************************************************************************************************)

};

PyObject *

PyFloat_GetInfo(void)

{

PyObject* floatinfo;

int pos=0;

floatinfo=PyStructSequence_New(&FloatInfoType);

if (floatinfo==NULL) {

return NULL;

}

#account for SetIntFlag(flag)

PyStructSequence_SET_ITEM(floatinfo, pos++, PyLong_FromLong(flag))

#account for SetDblFlag(flag)

PyStructSequence_SET_ITEM(floatinfo, pos++, PyFloat_FromDouble(flag))

SetDblFlag(DBL_MAX);

SetIntFlag(DBL_MAX_EXP);

SetIntFlag(DBL_MAX_(***********************************************************************************************************************)_EXP);

SetDblFlag(DBL_MIN);

SetIntFlag(DBL_MIN_EXP);

SetIntFlag(DBL_MIN_(***********************************************************************************************************************)_EXP);

SetIntFlag(DBL_DIG);

SetIntFlag(DBL_MANT_DIG);

SetDblFlag(DBL_EPSILON);

SetIntFlag(FLT_RADIX);

SetIntFlag(FLT_ROUNDS);

#undef SetIntFlag

#undef SetDblFlag

if (PyErr_Occurred()) {

Py_CLEAR(floatinfo);

return NULL;

}

return floatinfo;

}

PyObject *

PyFloat_FromDouble(double fval)

{

PyFloatObject *op=free_list;

if (op !=NULL) {

free_list=(PyFloatObject *) Py_TYPE(op);

numfree–;

} else {

op=(PyFloatObject*) PyObject_MALLOC(sizeof(PyFloatObject));

if (!op)

return PyErr_NoMemory();

}

/* Inline PyObject_New */

(void)PyObject_INIT(op, &PyFloat_Type);

op->ob_fval=fval;

return (PyObject *) op;

}

static PyObject *

float_from_string_inner(const char *s, Py_ssize_t len, void *obj)

{

double x;

const char *discontinue;

const char *final=s + len;

/* strip residence */

whereas (stp_name);

return NULL;

}

end result=_Py_string_to_number_with_underscores(s, len, “float”, v, v,

float_from_string_inner);

PyBuffer_Release(&see);

Py_XDECREF(s_buffer);

return end result;

}

static void

float_dealloc(PyFloatObject *op)

{

if (PyFloat_CheckExact(op)) {

if (numfree>=PyFloat_MAXFREELIST) {

PyObject_FREE(op);

return;

}

numfree++;

Py_TYPE(op)=(struct _typeobject *)free_list;

free_list=op;

}

else

Py_TYPE(op)->tp_free((PyObject *)op);

}

double

PyFloat_AsDouble(PyObject *op)

{

PyNumberMethods *nb;

PyObject *res;

double val;

if (op==NULL) {

PyErr_BadArgument();

return -1;

}

if (PyFloat_Check(op)) {

return PyFloat_AS_DOUBLE(op);

}

nb=Py_TYPE(op)->tp_as_number;

if (nb==NULL || nb->nb_float==NULL) {

if (nb && nb->nb_index) {

PyObject *res=PyNumber_Index(op);

if (!res) {

return -1;

}

double val=PyLong_AsDouble(res);

Py_DECREF(res);

return val;

}

PyErr_Format(PyExc_TypeError, “have to be unswerving number, now not %.(*******************************************************************************************************)s”,

op->ob_type->tp_name);

return -1;

}

res=(*nb->nb_float) (op);

if (res==NULL) {

return -1;

}

if (!PyFloat_CheckExact(res)) {

if (!PyFloat_Check(res)) {

PyErr_Format(PyExc_TypeError,

“%.(*******************************************************************************************************)s.__float__ returned non-float (style %.(*******************************************************************************************************)s)”,

op->ob_type->tp_name, res->ob_type->tp_name);

Py_DECREF(res);

return -1;

}

if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,

“%.(*******************************************************************************************************)s.__float__ returned non-float (style %.(*******************************************************************************************************)s). ”

“The potential to return an instance of a strict subclass of float ”

“is deprecated, and would possibly perchance well perchance additionally be removed in a future version of Python.”,

op->ob_type->tp_name, res->ob_type->tp_name)) {

Py_DECREF(res);

return -1;

}

}

val=PyFloat_AS_DOUBLE(res);

Py_DECREF(res);

return val;

}

/* Macro and helper that convert PyObject obj to a C double and retailer

the notice in dbl. If conversion to double raises an exception, obj is

topic to NULL, and the characteristic invoking this macro returns NULL. If

obj is now not of float or int style, Py_NotImplemented is incref’ed,

saved in obj, and returned from the characteristic invoking this macro.

*/

#account for CONVERT_TO_DOUBLE(obj, dbl)

if (PyFloat_Check(obj))

dbl=PyFloat_AS_DOUBLE(obj);

else if (convert_to_double(&(obj), &(dbl))0.0)==(iv>1.0))

return PyFloat_FromDouble(fabs(iw)); /* return inf */

else

return PyFloat_FromDouble(0.0);

}

if (Py_IS_INFINITY(iv)) {

/* (+-inf)**w is: inf for w certain, 0 for w detrimental; in

* both cases, we now must add the correct sign if w is

* an weird and wonderful integer.

*/

int iw_is_odd=DOUBLE_IS_ODD_INTEGER(iw);

if (iw>0.0)

return PyFloat_FromDouble(iw_is_odd ? iv : fabs(iv));

else

return PyFloat_FromDouble(iw_is_odd ?

copysign(0.0, iv) : 0.0);

}

if (iv==0.0) { /* 0**w is: 0 for w certain, 1 for w zero

(already dealt with above), and an error

if w is detrimental. */

int iw_is_odd=DOUBLE_IS_ODD_INTEGER(iw);

if (iwob_fval);

}

static PyObject *

float_abs(PyFloatObject *v)

{

return PyFloat_FromDouble(fabs(v->ob_fval));

}

static int

float_bool(PyFloatObject *v)

{

return v->ob_fval !=0.0;

}

/*(*)

float.is_integer

Return True if the float is an integer.

(**)*/

static PyObject *

float_is_integer_impl(PyObject *self)

/*(******)*/

{

double x=PyFloat_AsDouble(self);

PyObject *o;

if (x==-1.0 && PyErr_Occurred())

return NULL;

if (!Py_IS_FINITE(x))

Py_RETURN_FALSE;

errno=0;

PyFPE_START_PROTECT(“is_integer”, return NULL)

o=(floor(x)==x) ? Py_True : Py_False;

PyFPE_END_PROTECT(x)

if (errno !=0) {

PyErr_SetFromErrno(errno==ERANGE ? PyExc_OverflowError :

PyExc_ValueError);

return NULL;

}

Py_INCREF(o);

return o;

}

#if 0

static PyObject *

float_is_inf(PyObject *v)

{

double x=PyFloat_AsDouble(v);

if (x==-1.0 && PyErr_Occurred())

return NULL;

return PyBool_FromLong((long)Py_IS_INFINITY(x));

}

static PyObject *

float_is_nan(PyObject *v)

{

double x=PyFloat_AsDouble(v);

if (x==-1.0 && PyErr_Occurred())

return NULL;

return PyBool_FromLong((long)Py_IS_NAN(x));

}

static PyObject *

float_is_finite(PyObject *v)

{

double x=PyFloat_AsDouble(v);

if (x==-1.0 && PyErr_Occurred())

return NULL;

return PyBool_FromLong((long)Py_IS_FINITE(x));

}

#endif

/*(*)

float.__trunc__

Return the Integral closest to x between 0 and x.

(**)*/

static PyObject *

float___trunc___impl(PyObject *self)

/*(*******)*/

{

double x=PyFloat_AsDouble(self);

double wholepart; /* integral share of x, rounded toward 0 */

(void)modf(x, &wholepart);

/* Are attempting to procure out low-rate if this suits in a Python int. The strive

* to solid to long have to be unswerving, as C doesn’t account for what

* occurs if the double is too tall to slot in a protracted. Some rare

* techniques elevate an exception then (RISCOS was talked about as one,

* and any individual the utilize of a non-default option on Sun also bumped into

* that). Repeat that checking for>=and=0) {

if (ndigits>(******************************************************************************************************************)) {

/* pow1 and pow2 are every unswerving from overflow, however

pow1*pow2 ~=pow((***********************************************************************************************************************).0, ndigits) would possibly perchance well perchance overflow */

pow1=pow((***********************************************************************************************************************).0, (double)(ndigits-(******************************************************************************************************************)));

pow2=1e(******************************************************************************************************************);

}

else {

pow1=pow((***********************************************************************************************************************).0, (double)ndigits);

pow2=1.0;

}

y=(x*pow1)*pow2;

/* if y overflows, then rounded notice is precisely x */

if (!Py_IS_FINITE(y))

return PyFloat_FromDouble(x);

}

else {

pow1=pow((***********************************************************************************************************************).0, (double)-ndigits);

pow2=1.0; /* unused; silences a gcc compiler warning */

y=x / pow1;

}

z=round(y);

if (fabs(y-z)==0.5)

/* halfway between two integers; utilize round-half of-even */

z=2.0*round(y/2.0);

if (ndigits>=0)

z=(z / pow2) / pow1;

else

z *=pow1;

/* if computation resulted in overflow, elevate OverflowError */

if (!Py_IS_FINITE(z)) {

PyErr_SetString(PyExc_OverflowError,

“overflow came about at some level of round”);

return NULL;

}

return PyFloat_FromDouble(z);

}

#endif /* PY_NO_SHORT_FLOAT_REPR */

/* round a Python float v to the closest multiple of (***********************************************************************************************************************)**-ndigits */

/*(*)

float.__round__

ndigits as o_ndigits: object=NULL

/

Return the Integral closest to x, rounding half of toward even.

When an argument is handed, work love constructed-in round(x, ndigits).

(**)*/

static PyObject *

float___round___impl(PyObject *self, PyObject *o_ndigits)

/*(********)*/

{

double x, rounded;

Py_ssize_t ndigits;

x=PyFloat_AsDouble(self);

if (o_ndigits==NULL || o_ndigits==Py_None) {

/* single-argument round or with None ndigits:

* round to nearest integer */

rounded=round(x);

if (fabs(x-rounded)==0.5)

/* halfway case: round to even */

rounded=2.0*round(x/2.0);

return PyLong_FromDouble(rounded);

}

/* make clear 2d argument as a Py_ssize_t; clips on overflow */

ndigits=PyNumber_AsSsize_t(o_ndigits, NULL);

if (ndigits==-1 && PyErr_Occurred())

return NULL;

/* nans and infinities round to themselves */

if (!Py_IS_FINITE(x))

return PyFloat_FromDouble(x);

/* Handle impolite values for ndigits. For ndigits>NDIGITS_MAX, x

continuously rounds to itself. For ndigits (************************) NDIGITS_MAX)

/* return x */

return PyFloat_FromDouble(x);

else if (ndigits (*************************)ob_fval);

return v;

}

/*(*)

float.conjugate

Return self, the advanced conjugate of any float.

(**)*/

static PyObject *

float_conjugate_impl(PyObject *self)

/*(*********)*/

{

return float_float(self);

}

/* turn ASCII hex characters into integer values and vice versa */

static char

char_from_hex(int x)

{

dispute(0 LONG_MAX/2 then the notice of the hex string is

* guaranteed to overflow (provided or now not it’s nonzero)

*

* (2) if exp (**************************)=0)

s++;

coeff_end=s-1;

}

else

coeff_end=s;

/* ndigits=total # of hex digits; fdigits=# after level */

ndigits=coeff_end – coeff_start;

fdigits=coeff_end – s_store;

if (ndigits==0)

goto parse_error;

if (ndigits>Py_MIN(DBL_MIN_EXP – DBL_MANT_DIG – LONG_MIN/2,

LONG_MAX/2 + 1 – DBL_MAX_EXP)/4)

goto insane_length_error;

/* (**********) */

if (*s==’p’ || *s==’P’) {

s++;

exp_start=s;

if (*s==’-‘ || *s==’+’)

s++;

if (!(‘0’ LONG_MAX/2)

goto overflow_error;

/* Regulate exponent for fractional share. */

exp=exp – 4*((long)fdigits);

/* top_exp=1 extra than exponent of most sig. bit of coefficient */

top_exp=exp + 4*((long)ndigits – 1);

for (digit=HEX_DIGIT(ndigits-1); digit !=0; digit /=2)

top_exp++;

/* catch nearly all nonextreme cases of overflow and underflow here */

if (top_exp (***************************) DBL_MAX_EXP)

goto overflow_error;

/* lsb=exponent of least most essential bit of the *rounded* notice.

That is top_exp – DBL_MANT_DIG except end result’s subnormal. */

lsb=Py_MAX(top_exp, (long)DBL_MIN_EXP) – DBL_MANT_DIG;

x=0.0;

if (exp>=lsb) {

/* no rounding required */

for (i=ndigits-1; i>=0; i–)

x=(*******************************************************************************************************************).0*x + HEX_DIGIT(i);

x=ldexp(x, (int)(exp));

goto completed;

}

/* rounding required. key_digit is the index of the hex digit

containing the first bit to be rounded away. */

half_eps=1 key_digit; i–)

x=(*******************************************************************************************************************).0*x + HEX_DIGIT(i);

digit=HEX_DIGIT(key_digit);

x=(*******************************************************************************************************************).0*x + (double)(digit & ((*******************************************************************************************************************)-2*half_eps));

/* round-half of-even: round up if bit lsb-1 is 1 and at the very least one of

bits lsb, lsb-2, lsb-3, lsb-4, … is 1. */

if ((digit & half_eps) !=0) {

round_up=0;

if ((digit & (3*half_eps-1)) !=0 ||

(half_eps==8 && (HEX_DIGIT(key_digit+1) & 1) !=0))

round_up=1;

else

for (i=key_digit-1; i>=0; i–)

if (HEX_DIGIT(i) !=0) {

round_up=1;

shatter;

}

if (round_up) {

x +=2*half_eps;

if (top_exp==DBL_MAX_EXP &&

x==ldexp((double)(2*half_eps), DBL_MANT_DIG))

/* overflow corner case: pre-rounded notice nb_lshift(numerator, py_exponent));

if (numerator==NULL)

goto error;

}

else {

Py_SETREF(denominator,

long_methods->nb_lshift(denominator, py_exponent));

if (denominator==NULL)

goto error;

}

result_pair=PyTuple_Pack(2, numerator, denominator);

error:

Py_XDECREF(py_exponent);

Py_XDECREF(denominator);

Py_XDECREF(numerator);

return result_pair;

}

static PyObject *

float_subtype_new(PyTypeObject *style, PyObject *x);

/*(*)

@classmethod

float.__new__ as float_new

x: object(c_default=”_PyLong_Zero”)=0

/

Convert a string or number to a floating level number, if likely.

(**)*/

static PyObject *

float_new_impl(PyTypeObject *style, PyObject *x)

/*(***********)*/

{

if (style !=&PyFloat_Type)

return float_subtype_new(style, x); /* Wimp out */

/* If or now not it’s a string, however now not a string subclass, utilize

PyFloat_FromString. */

if (PyUnicode_CheckExact(x))

return PyFloat_FromString(x);

return PyNumber_Float(x);

}

/* Wimpy, late procedure to tp_new requires subtypes of float:

first create a normal float from whatever arguments we got,

then allocate a subtype instance and initialize its ob_fval

from the humble float. The conventional float is then thrown away.

*/

static PyObject *

float_subtype_new(PyTypeObject *style, PyObject *x)

{

PyObject *tmp, *newobj;

dispute(PyType_IsSubtype(style, &PyFloat_Type));

tmp=float_new_impl(&PyFloat_Type, x);

if (tmp==NULL)

return NULL;

dispute(PyFloat_Check(tmp));

newobj=style->tp_alloc(style, 0);

if (newobj==NULL) {

Py_DECREF(tmp);

return NULL;

}

((PyFloatObject *)newobj)->ob_fval=((PyFloatObject *)tmp)->ob_fval;

Py_DECREF(tmp);

return newobj;

}

/*(*)

float.__getnewargs__

(**)*/

static PyObject *

float___getnewargs___impl(PyObject *self)

/*(************)*/

{

return Py_BuildValue(“(d)”, ((PyFloatObject *)self)->ob_fval);

}

/* this is for the advantage of the pack/unpack routines under */

typedef enum {

unknown_format, ieee_big_endian_format, ieee_little_endian_format

} float_format_type;

static float_format_type double_format, float_format;

static float_format_type detected_double_format, detected_float_format;

/*(*)

@classmethod

float.__getformat__

typestr: str

Must be ‘double’ or ‘float’.

/

You doubtlessly don’t desire to utilize this characteristic.

It exists primarily to be primitive in Python’s test suite.

This characteristic returns whichever of ‘unknown’, ‘IEEE, tall-endian’ or ‘IEEE,

petite-endian’ simplest describes the layout of floating level numbers primitive by the

C style named by typestr.

(**)*/

static PyObject *

float___getformat___impl(PyTypeObject *style, const char *typestr)

/*(*************)*/

{

float_format_type r;

if (strcmp(typestr, “double”)==0) {

r=double_format;

}

else if (strcmp(typestr, “float”)==0) {

r=float_format;

}

else {

PyErr_SetString(PyExc_ValueError,

“__getformat__() argument 1 have to be ”

“‘double’ or ‘float'”);

return NULL;

}

swap (r) {

case unknown_format:

return PyUnicode_FromString(“unknown”);

case ieee_little_endian_format:

return PyUnicode_FromString(“IEEE, petite-endian”);

case ieee_big_endian_format:

return PyUnicode_FromString(“IEEE, tall-endian”);

default:

Py_FatalError(“insane float_format or double_format”);

return NULL;

}

}

/*(*)

@classmethod

float.__set_format__

typestr: str

Must be ‘double’ or ‘float’.

fmt: str

Must be one of ‘unknown’, ‘IEEE, tall-endian’ or ‘IEEE, petite-endian’,

and likewise can solely be most definitely the most latter two if it looks to

match the underlying C reality.

/

You doubtlessly don’t desire to utilize this characteristic.

It exists primarily to be primitive in Python’s test suite.

Override the automatic option of C-level floating level style.

This affects how floats are transformed to and from binary strings.

(**)*/

static PyObject *

float___set_format___impl(PyTypeObject *style, const char *typestr,

const char *fmt)

/*(**************)*/

{

float_format_type f;

float_format_type detected;

float_format_type *p;

if (strcmp(typestr, “double”)==0) {

p=&double_format;

detected=detected_double_format;

}

else if (strcmp(typestr, “float”)==0) {

p=&float_format;

detected=detected_float_format;

}

else {

PyErr_SetString(PyExc_ValueError,

“__setformat__() argument 1 must ”

“be ‘double’ or ‘float'”);

return NULL;

}

if (strcmp(fmt, “unknown”)==0) {

f=unknown_format;

}

else if (strcmp(fmt, “IEEE, petite-endian”)==0) {

f=ieee_little_endian_format;

}

else if (strcmp(fmt, “IEEE, tall-endian”)==0) {

f=ieee_big_endian_format;

}

else {

PyErr_SetString(PyExc_ValueError,

“__setformat__() argument 2 have to be ”

“‘unknown’, ‘IEEE, petite-endian’ or ”

“‘IEEE, tall-endian'”);

return NULL;

}

if (f !=unknown_format && f !=detected) {

PyErr_Format(PyExc_ValueError,

“can solely topic %s layout to ‘unknown’ or the ”

“detected platform notice”, typestr);

return NULL;

}

*p=f;

Py_RETURN_NONE;

}

static PyObject *

float_getreal(PyObject *v, void *closure)

{

return float_float(v);

}

static PyObject *

float_getimag(PyObject *v, void *closure)

{

return PyFloat_FromDouble(0.0);

}

/*(*)

float.__format__

format_spec: unicode

/

Formats the float in accordance with format_spec.

(**)*/

static PyObject *

float___format___impl(PyObject *self, PyObject *format_spec)

/*(***************)*/

{

_PyUnicodeWriter creator;

int ret;

_PyUnicodeWriter_Init(&creator);

ret=_PyFloat_FormatAdvancedWriter(

&creator,

self,

format_spec, 0, PyUnicode_GET_LENGTH(format_spec));

if (ret==-1) {

_PyUnicodeWriter_Dealloc(&creator);

return NULL;

}

return _PyUnicodeWriter_Finish(&creator);

}

static PyMethodDef float_methods(****)={

FLOAT_CONJUGATE_METHODDEF

FLOAT___TRUNC___METHODDEF

FLOAT___ROUND___METHODDEF

FLOAT_AS_INTEGER_RATIO_METHODDEF

FLOAT_FROMHEX_METHODDEF

FLOAT_HEX_METHODDEF

FLOAT_IS_INTEGER_METHODDEF

#if 0

{“is_inf”, (PyCFunction)float_is_inf, METH_NOARGS,

“Return True if the float is definite or detrimental endless.”},

{“is_finite”, (PyCFunction)float_is_finite, METH_NOARGS,

“Return True if the float is finite, neither endless nor NaN.”},

{“is_nan”, (PyCFunction)float_is_nan, METH_NOARGS,

“Return True if the float is now not a host (NaN).”},

#endif

FLOAT___GETNEWARGS___METHODDEF

FLOAT___GETFORMAT___METHODDEF

FLOAT___SET_FORMAT___METHODDEF

FLOAT___FORMAT___METHODDEF

{NULL, NULL} /* sentinel */

};

static PyGetSetDef float_getset(****)={

{“unswerving”,

float_getreal, (setter)NULL,

“the unswerving share of a fancy number”,

NULL},

{“imag”,

float_getimag, (setter)NULL,

“the imaginary share of a fancy number”,

NULL},

{NULL} /* Sentinel */

};

static PyNumberMethods float_as_number={

float_add, /* nb_add */

float_sub, /* nb_subtract */

float_mul, /* nb_multiply */

float_rem, /* nb_remainder */

float_divmod, /* nb_divmod */

float_pow, /* nb_power */

(unaryfunc)float_neg, /* nb_negative */

float_float, /* nb_positive */

(unaryfunc)float_abs, /* nb_absolute */

(inquiry)float_bool, /* nb_bool */

0, /* nb_invert */

0, /* nb_lshift */

0, /* nb_rshift */

0, /* nb_and */

0, /* nb_xor */

0, /* nb_or */

float___trunc___impl, /* nb_int */

0, /* nb_reserved */

float_float, /* nb_float */

0, /* nb_inplace_add */

0, /* nb_inplace_subtract */

0, /* nb_inplace_multiply */

0, /* nb_inplace_remainder */

0, /* nb_inplace_power */

0, /* nb_inplace_lshift */

0, /* nb_inplace_rshift */

0, /* nb_inplace_and */

0, /* nb_inplace_xor */

0, /* nb_inplace_or */

float_floor_div, /* nb_floor_divide */

float_div, /* nb_true_divide */

0, /* nb_inplace_floor_divide */

0, /* nb_inplace_true_divide */

};

PyTypeObject PyFloat_Type={

PyVarObject_HEAD_INIT(&PyType_Type, 0)

“float”,

sizeof(PyFloatObject),

0,

(destructor)float_dealloc, /* tp_dealloc */

0, /* tp_vectorcall_offset */

0, /* tp_getattr */

0, /* tp_setattr */

0, /* tp_as_async */

(reprfunc)float_repr, /* tp_repr */

&float_as_number, /* tp_as_number */

0, /* tp_as_sequence */

0, /* tp_as_mapping */

(hashfunc)float_hash, /* tp_hash */

0, /* tp_call */

0, /* tp_str */

PyObject_GenericGetAttr, /* tp_getattro */

0, /* tp_setattro */

0, /* tp_as_buffer */

Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */

float_new__doc__, /* tp_doc */

0, /* tp_traverse */

0, /* tp_clear */

float_richcompare, /* tp_richcompare */

0, /* tp_weaklistoffset */

0, /* tp_iter */

0, /* tp_iternext */

float_methods, /* tp_methods */

0, /* tp_members */

float_getset, /* tp_getset */

0, /* tp_base */

0, /* tp_dict */

0, /* tp_descr_get */

0, /* tp_descr_set */

0, /* tp_dictoffset */

0, /* tp_init */

0, /* tp_alloc */

float_new, /* tp_new */

};

int

_PyFloat_Init(void)

{

/* We strive to search out out if this machine is the utilize of IEEE

floating level formats by peering at the bits of some

reasonably chosen values. If it appears to be love we’re on an

IEEE platform, the float packing/unpacking routines can

good reproduction bits, if now not they resort to arithmetic & shifts

and masks. The shifts & masks procedure works on all finite

values, however what occurs to infinities, NaNs and signed

zeroes on packing is an accident, and making an are attempting to unpack

a NaN or an infinity will elevate an exception.

Repeat that if we’re on some whacked-out platform which uses

IEEE formats however is rarely always strictly petite-endian or tall-

endian, we can topple wait on to the transportable shifts & masks

procedure. */

#if SIZEOF_DOUBLE==8

{

double x=(*************************************).0;

if (memcmp(&x, “x(*********************************************************************************************************)x3fxffx(*****************************************************************************************************************************)x(****************************************************************************************************************************)x(**************************************************************************************************************************)x(*************************************************************************************************************************)x(************************************************************************************************************************)”, 8)==0)

detected_double_format=ieee_big_endian_format;

else if (memcmp(&x, “x(************************************************************************************************************************)x(*************************************************************************************************************************)x(**************************************************************************************************************************)x(****************************************************************************************************************************)x(*****************************************************************************************************************************)xffx3fx(*********************************************************************************************************)”, 8)==0)

detected_double_format=ieee_little_endian_format;

else

detected_double_format=unknown_format;

}

#else

detected_double_format=unknown_format;

#endif

#if SIZEOF_FLOAT==4

{

float y=(****************************************).0;

if (memcmp(&y, “x4bx7fx(*****************************************************************************************************************************)x(****************************************************************************************************************************)”, 4)==0)

detected_float_format=ieee_big_endian_format;

else if (memcmp(&y, “x(****************************************************************************************************************************)x(*****************************************************************************************************************************)x7fx4b”, 4)==0)

detected_float_format=ieee_little_endian_format;

else

detected_float_format=unknown_format;

}

#else

detected_float_format=unknown_format;

#endif

double_format=detected_double_format;

float_format=detected_float_format;

/* Init float recordsdata */

if (FloatInfoType.tp_name==NULL) {

if (PyStructSequence_InitType2(&FloatInfoType, &floatinfo_desc) (****************************) 0.5) || (f – bits==0.5 && bits % 2)) {

* bits++;

* }

*/

int

_PyFloat_Pack2(double x, unsigned char *p, int le)

{

unsigned char sign;

int e;

double f;

unsigned short bits;

int incr=1;

if (x==0.0) {

sign=(copysign(1.0, x)==-1.0);

e=0;

bits=0;

}

else if (Py_IS_INFINITY(x)) {

sign=(x>(**************************************************************************************************************)) {

/* And it also progagated out of the next (**************************************************************************************************************) bits. */

fhi=0;

++e;

if (e>=(***********************************************************))

goto Overflow;

}

}

/* First byte */

*p=(sign>4);

p +=incr;

/* 2nd byte */

*p=(unsigned char) (((e & 0xF)>(****************************************************************************************************************)));

p +=incr;

/* Third byte */

*p=(fhi>>(*******************************************************************************************************************)) & 0xFF;

p +=incr;

/* Fourth byte */

*p=(fhi>>8) & 0xFF;

p +=incr;

/* Fifth byte */

*p=fhi & 0xFF;

p +=incr;

/* Sixth byte */

*p=(flo>>(*******************************************************************************************************************)) & 0xFF;

p +=incr;

/* Seventh byte */

*p=(flo>>8) & 0xFF;

p +=incr;

/* Eighth byte */

*p=flo & 0xFF;

/* p +=incr; */

/* Performed */

return 0;

Overflow:

PyErr_SetString(PyExc_OverflowError,

“float too immense to pack with d layout”);

return -1;

}

else {

const unsigned char *s=(unsigned char*)&x;

int i, incr=1;

if ((double_format==ieee_little_endian_format && !le)

|| (double_format==ieee_big_endian_format && le)) {

p +=7;

incr=-1;

}

for (i=0; i (*********************************)>7) & 1;

e=(*p & 0x7C)>>2;

f=(*p & 0x(**************************************************************************************************************************))>7) & 1;

e=(*p & 0x7F)>7) & 1;

f=(*p & 0x7F) 7) & 1;

e=(*p & 0x7F)>4) & 0xF;

fhi=(*p & 0xF) (**********************************)(***********************************)Learn Extra(************************************)

Euch gefällt was ihr seht? Nehmt euch doch einen kurzen Moment und unterstützt uns auf Patreon!
python/cpython 1