1 /**
2   Mirror _floatobject.h
3   */
4 module deimos.python.floatobject;
5 
6 import deimos.python.pyport;
7 import deimos.python.object;
8 import deimos.python.unicodeobject;
9 import deimos.python.pythonrun;
10 import core.stdc.stdio;
11 
12 extern(C):
13 // Python-header-file: Include/floatobject.h:
14 
15 /// subclass of PyObject
16 struct PyFloatObject {
17     mixin PyObject_HEAD;
18     /// _
19     double ob_fval;
20 }
21 
22 /// _
23 mixin(PyAPI_DATA!"PyTypeObject PyFloat_Type");
24 
25 // D translation of C macro:
26 /// _
27 int PyFloat_Check()(PyObject *op) {
28     return PyObject_TypeCheck(op, &PyFloat_Type);
29 }
30 // D translation of C macro:
31 /// _
32 int PyFloat_CheckExact()(PyObject *op) {
33     return Py_TYPE(op) == &PyFloat_Type;
34 }
35 
36 version(Python_2_6_Or_Later){
37     /// Availability: >= 2.6
38     double PyFloat_GetMax();
39     /// Availability: >= 2.6
40     double PyFloat_GetMin();
41     /// Availability: >= 2.6
42     PyObject* PyFloat_GetInfo();
43 }
44 
45 version(Python_3_0_Or_Later) {
46     /// Availability: 3.*
47     PyObject* PyFloat_FromString(PyObject*);
48 }else{
49     /** Return Python float from string PyObject.  Second argument ignored on
50       input, and, if non-NULL, NULL is stored into *junk (this tried to serve a
51       purpose once but can't be made to work as intended). */
52     /// Availability: 2.*
53     PyObject* PyFloat_FromString(PyObject*, char** junk);
54 }
55 /** Return Python float from C double. */
56 PyObject* PyFloat_FromDouble(double);
57 
58 /** Extract C double from Python float.  The macro version trades safety for
59    speed. */
60 double PyFloat_AsDouble(PyObject*);
61 /// ditto
62 double PyFloat_AS_DOUBLE()(PyObject* op) {
63     return (cast(PyFloatObject*)op).ob_fval;
64 }
65 version(Python_3_0_Or_Later) {
66 }else{
67     /** Write repr(v) into the char buffer argument, followed by null byte.  The
68       buffer must be "big enough"; >= 100 is very safe.
69       PyFloat_AsReprString(buf, x) strives to print enough digits so that
70       PyFloat_FromString(buf) then reproduces x exactly. */
71     /// Availability: 2.*
72     void PyFloat_AsReprString(char*, PyFloatObject* v);
73     /** Write str(v) into the char buffer argument, followed by null byte.  The
74        buffer must be "big enough"; >= 100 is very safe.  Note that it's
75        unusual to be able to get back the float you started with from
76        PyFloat_AsString's result -- use PyFloat_AsReprString() if you want to
77        preserve precision across conversions. */
78     /// Availability: 2.*
79     void PyFloat_AsString(char*, PyFloatObject* v);
80 }
81 
82 /** _PyFloat_{Pack,Unpack}{4,8}
83  *
84  * The struct and pickle (at least) modules need an efficient platform-
85  * independent way to store floating-point values as byte strings.
86  * The Pack routines produce a string from a C double, and the Unpack
87  * routines produce a C double from such a string.  The suffix (4 or 8)
88  * specifies the number of bytes in the string.
89  *
90  * On platforms that appear to use (see _PyFloat_Init()) IEEE-754 formats
91  * these functions work by copying bits.  On other platforms, the formats the
92  * 4- byte format is identical to the IEEE-754 single precision format, and
93  * the 8-byte format to the IEEE-754 double precision format, although the
94  * packing of INFs and NaNs (if such things exist on the platform) isn't
95  * handled correctly, and attempting to unpack a string containing an IEEE
96  * INF or NaN will raise an exception.
97  *
98  * On non-IEEE platforms with more precision, or larger dynamic range, than
99  * 754 supports, not all values can be packed; on non-IEEE platforms with less
100  * precision, or smaller dynamic range, not all values can be unpacked.  What
101  * happens in such cases is partly accidental (alas).
102  *
103  * The pack routines write 4 or 8 bytes, starting at p.  le is a bool
104  * argument, true if you want the string in little-endian format (exponent
105  * last, at p+3 or p+7), false if you want big-endian format (exponent
106  * first, at p).
107  * Return value:  0 if all is OK, -1 if error (and an exception is
108  * set, most likely OverflowError).
109  * There are two problems on non-IEEE platforms:
110  * 1):  What this does is undefined if x is a NaN or infinity.
111  * 2):  -0.0 and +0.0 produce the same string.
112  */
113 int _PyFloat_Pack4(double x, ubyte* p, int le);
114 /// ditto
115 int _PyFloat_Pack8(double x, ubyte* p, int le);
116 
117 version(Python_3_0_Or_Later) {
118     /// Availability: 3.*
119     int _PyFloat_Repr(double x, char* p, size_t len);
120 }
121 
122 version(Python_2_6_Or_Later){
123     /** Used to get the important decimal digits of a double */
124     /// Availability: >= 2.6
125     int _PyFloat_Digits(char* buf, double v, int* signum);
126     /// Availability: >= 2.6
127     void _PyFloat_DigitsInit();
128     /** The unpack routines read 4 or 8 bytes, starting at p.  le is a bool
129      * argument, true if the string is in little-endian format (exponent
130      * last, at p+3 or p+7), false if big-endian (exponent first, at p).
131      * Return value:  The unpacked double.  On error, this is -1.0 and
132      * PyErr_Occurred() is true (and an exception is set, most likely
133      * OverflowError).  Note that on a non-IEEE platform this will refuse
134      * to unpack a string that represents a NaN or infinity.
135      */
136     double _PyFloat_Unpack4(const(ubyte)* p, int le);
137     /// ditto
138     double _PyFloat_Unpack8(const(ubyte)* p, int le);
139     /** free list api */
140     /// Availability: >= 2.6
141     int PyFloat_ClearFreeList();
142 }
143 
144 version(Python_2_7_Or_Later) {
145     /// Availability: >= 2.7
146     void _PyFloat_DebugMallocStats(FILE* out_);
147 }
148 version(Python_3_0_Or_Later) {
149     /// Availability: 3.*
150     PyObject* _PyFloat_FormatAdvanced(PyObject* obj,
151             Py_UNICODE* format_spec,
152             Py_ssize_t format_spec_len);
153 }else{
154     version(Python_2_7_Or_Later) {
155         /** Round a C double x to the closest multiple of 10**-ndigits.  
156           Returns a Python float on success, or NULL (with an appropriate 
157           exception set) on failure.  Used in builtin_round in bltinmodule.c. 
158          */
159         /// Availability: >= 2.7
160         PyObject* _Py_double_round(double x, int ndigits);
161     }
162     version(Python_2_6_Or_Later) {
163         /** Format the object based on the format_spec, as defined in PEP 3101
164            (Advanced String Formatting). */
165         /// Availability: >= 2.6
166         PyObject* _PyFloat_FormatAdvanced(PyObject* obj,
167                 char* format_spec,
168                 Py_ssize_t format_spec_len);
169     }
170 }