PageRenderTime 35ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/fpconst.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 163 lines | 155 code | 0 blank | 8 comment | 0 complexity | 6c9460f9c3f7f06480b46d6282505b01 MD5 | raw file
  1. """Utilities for handling IEEE 754 floating point special values
  2. This python module implements constants and functions for working with
  3. IEEE754 double-precision special values. It provides constants for
  4. Not-a-Number (NaN), Positive Infinity (PosInf), and Negative Infinity
  5. (NegInf), as well as functions to test for these values.
  6. The code is implemented in pure python by taking advantage of the
  7. 'struct' standard module. Care has been taken to generate proper
  8. results on both big-endian and little-endian machines. Some efficiency
  9. could be gained by translating the core routines into C.
  10. See <http://babbage.cs.qc.edu/courses/cs341/IEEE-754references.html>
  11. for reference material on the IEEE 754 floating point standard.
  12. Further information on this package is available at
  13. <http://www.analytics.washington.edu/statcomp/projects/rzope/fpconst/>.
  14. Author: Gregory R. Warnes <gregory_r_warnes@groton.pfizer.com>
  15. Date:: 2003-04-08
  16. Copyright: (c) 2003, Pfizer, Inc.
  17. """
  18. __version__ = "0.7.0"
  19. ident = "$Id: fpconst.py,v 1.12 2004/05/22 04:38:17 warnes Exp $"
  20. import struct, operator
  21. # check endianess
  22. _big_endian = struct.pack('i',1)[0] != '\x01'
  23. # and define appropriate constants
  24. if(_big_endian):
  25. NaN = struct.unpack('d', '\x7F\xF8\x00\x00\x00\x00\x00\x00')[0]
  26. PosInf = struct.unpack('d', '\x7F\xF0\x00\x00\x00\x00\x00\x00')[0]
  27. NegInf = -PosInf
  28. else:
  29. NaN = struct.unpack('d', '\x00\x00\x00\x00\x00\x00\xf8\xff')[0]
  30. PosInf = struct.unpack('d', '\x00\x00\x00\x00\x00\x00\xf0\x7f')[0]
  31. NegInf = -PosInf
  32. def _double_as_bytes(dval):
  33. "Use struct.unpack to decode a double precision float into eight bytes"
  34. tmp = list(struct.unpack('8B',struct.pack('d', dval)))
  35. if not _big_endian:
  36. tmp.reverse()
  37. return tmp
  38. ##
  39. ## Functions to extract components of the IEEE 754 floating point format
  40. ##
  41. def _sign(dval):
  42. "Extract the sign bit from a double-precision floating point value"
  43. bb = _double_as_bytes(dval)
  44. return bb[0] >> 7 & 0x01
  45. def _exponent(dval):
  46. """Extract the exponentent bits from a double-precision floating
  47. point value.
  48. Note that for normalized values, the exponent bits have an offset
  49. of 1023. As a consequence, the actual exponentent is obtained
  50. by subtracting 1023 from the value returned by this function
  51. """
  52. bb = _double_as_bytes(dval)
  53. return (bb[0] << 4 | bb[1] >> 4) & 0x7ff
  54. def _mantissa(dval):
  55. """Extract the _mantissa bits from a double-precision floating
  56. point value."""
  57. bb = _double_as_bytes(dval)
  58. mantissa = bb[1] & 0x0f << 48
  59. mantissa += bb[2] << 40
  60. mantissa += bb[3] << 32
  61. mantissa += bb[4]
  62. return mantissa
  63. def _zero_mantissa(dval):
  64. """Determine whether the mantissa bits of the given double are all
  65. zero."""
  66. bb = _double_as_bytes(dval)
  67. return ((bb[1] & 0x0f) | reduce(operator.or_, bb[2:])) == 0
  68. ##
  69. ## Functions to test for IEEE 754 special values
  70. ##
  71. def isNaN(value):
  72. "Determine if the argument is a IEEE 754 NaN (Not a Number) value."
  73. return (_exponent(value)==0x7ff and not _zero_mantissa(value))
  74. def isInf(value):
  75. """Determine if the argument is an infinite IEEE 754 value (positive
  76. or negative inifinity)"""
  77. return (_exponent(value)==0x7ff and _zero_mantissa(value))
  78. def isFinite(value):
  79. """Determine if the argument is an finite IEEE 754 value (i.e., is
  80. not NaN, positive or negative inifinity)"""
  81. return (_exponent(value)!=0x7ff)
  82. def isPosInf(value):
  83. "Determine if the argument is a IEEE 754 positive infinity value"
  84. return (_sign(value)==0 and _exponent(value)==0x7ff and \
  85. _zero_mantissa(value))
  86. def isNegInf(value):
  87. "Determine if the argument is a IEEE 754 negative infinity value"
  88. return (_sign(value)==1 and _exponent(value)==0x7ff and \
  89. _zero_mantissa(value))
  90. ##
  91. ## Functions to test public functions.
  92. ##
  93. def test_isNaN():
  94. assert( not isNaN(PosInf) )
  95. assert( not isNaN(NegInf) )
  96. assert( isNaN(NaN ) )
  97. assert( not isNaN( 1.0) )
  98. assert( not isNaN( -1.0) )
  99. def test_isInf():
  100. assert( isInf(PosInf) )
  101. assert( isInf(NegInf) )
  102. assert( not isInf(NaN ) )
  103. assert( not isInf( 1.0) )
  104. assert( not isInf( -1.0) )
  105. def test_isFinite():
  106. assert( not isFinite(PosInf) )
  107. assert( not isFinite(NegInf) )
  108. assert( not isFinite(NaN ) )
  109. assert( isFinite( 1.0) )
  110. assert( isFinite( -1.0) )
  111. def test_isPosInf():
  112. assert( isPosInf(PosInf) )
  113. assert( not isPosInf(NegInf) )
  114. assert( not isPosInf(NaN ) )
  115. assert( not isPosInf( 1.0) )
  116. assert( not isPosInf( -1.0) )
  117. def test_isNegInf():
  118. assert( not isNegInf(PosInf) )
  119. assert( isNegInf(NegInf) )
  120. assert( not isNegInf(NaN ) )
  121. assert( not isNegInf( 1.0) )
  122. assert( not isNegInf( -1.0) )
  123. # overall test
  124. def test():
  125. test_isNaN()
  126. test_isInf()
  127. test_isFinite()
  128. test_isPosInf()
  129. test_isNegInf()
  130. if __name__ == "__main__":
  131. test()