]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/python/python-soappy-0.11.3/fpconst.py
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / python / python-soappy-0.11.3 / fpconst.py
1 """Utilities for handling IEEE 754 floating point special values
2
3 This python module implements constants and functions for working with
4 IEEE754 double-precision special values.  It provides constants for
5 Not-a-Number (NaN), Positive Infinity (PosInf), and Negative Infinity
6 (NegInf), as well as functions to test for these values.
7
8 The code is implemented in pure python by taking advantage of the
9 'struct' standard module. Care has been taken to generate proper
10 results on both big-endian and little-endian machines. Some efficiency
11 could be gained by translating the core routines into C.
12
13 See <http://babbage.cs.qc.edu/courses/cs341/IEEE-754references.html>
14 for reference material on the IEEE 754 floating point standard.
15
16 Further information on this package is available at
17 <http://software.biostat.washington.edu/statsoft/snake/fpconst>.
18
19 Author:    Gregory R. Warnes <gregory_r_warnes@groton.pfizer.com>
20 Date::     2003-04-08
21 Copyright: (c) 2003, Pfizer, Inc.
22 """
23
24 __version__ = "0.6.0"
25 ident = "$Id: fpconst.py,v 1.8 2003/05/12 15:14:00 warnes Exp $"
26
27 import struct
28
29 # check endianess
30 _big_endian = struct.pack('i',1)[0] != '\x01'
31
32 # and define appropriate constants
33 if(_big_endian): 
34     _HW = 0
35     _LW = 1
36     
37     NaN = struct.unpack('d', '\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF')[0]
38     PosInf = struct.unpack('d', '\x7F\xF0\x00\x00\x00\x00\x00\x00')[0]
39     NegInf = -PosInf
40     
41 else:
42     _HW = 1
43     _LW = 0
44
45     NaN = struct.unpack('d', '\x00\x00\x00\x00\x00\x00\xf8\xff')[0]
46     PosInf = struct.unpack('d', '\x00\x00\x00\x00\x00\x00\xf0\x7f')[0]
47     NegInf = -PosInf
48
49 def _double_as_longs(dval):
50     "Use struct.unpack to decode a double precision float into two longs"
51     tmp = struct.unpack('ll',struct.pack('d', dval))
52     return (tmp[_HW], tmp[_LW])
53
54
55 ##
56 ## Functions to extract components of the IEEE 754 floating point format
57 ##
58
59 def _sign(dval):
60     "Extract the sign bit from a double-precision floating point value"
61     ll = _double_as_longs(dval)
62     return ll[0] >> 31 & 0x01 
63
64 def _exponent(dval):
65     """Extract the exponentent bits from a double-precision floating
66     point value.
67
68     Note that for normalized values, the exponentdent bits have an offset
69     of 1023. As a consequence, the actual exponentent is obtained
70     by subtracting 1023 for the value returned by this function
71     """
72     ll = _double_as_longs(dval)
73     return (ll[0] >> 20) & 0x7ff
74
75 def _mantissa(dval):
76     """Extract the _mantissa bits from a double-precision floating
77     point value."""
78
79     ll = _double_as_longs(dval)
80     mantissa0 = (ll[0] & 0x000fffffL) << 32
81     mantissa1 = ll[1] 
82     return mantissa0 + mantissa1
83
84 ##
85 ## Functions to test for IEEE 754 special values
86 ##
87
88 def isNaN(value):
89     "Determine if the argument is a IEEE 754 NaN (Not a Number) value."
90     return (_exponent(value)==0x7ff and _mantissa(value)!=0) 
91
92 def isInf(value):
93     """Determine if the argument is an infinite IEEE 754 value (positive
94     or negative inifinity)"""
95     return (_exponent(value)==0x7ff and _mantissa(value)== 0)
96
97 def isFinite(value):
98     """Determine if the argument is an finite IEEE 754 value (i.e., is
99     not NaN, positive or negative inifinity)"""
100     return (_exponent(value)!=0x7ff)
101
102
103 def isPosInf(value):
104     "Determine if the argument is a IEEE 754 positive infinity value"
105     return (_sign(value)==0 and _exponent(value)==0x7ff and \
106             _mantissa(value)== 0) 
107
108 def isNegInf(value):
109     "Determine if the argument is a IEEE 754 negative infinity value"
110     return (_sign(value)==1 and _exponent(value)==0x7ff and \
111             _mantissa(value)== 0)