Module float
[hide private]
[frames] | no frames]

Source Code for Module float

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2006  Gary S Thompson (see https://gna.org/users for contact  # 
  4  #                                      details)                               # 
  5  #                                                                             # 
  6  # Copyright (C) 2008-2012 Edward d'Auvergne                                   # 
  7  #                                                                             # 
  8  # This file is part of the program relax.                                     # 
  9  #                                                                             # 
 10  # relax is free software; you can redistribute it and/or modify               # 
 11  # it under the terms of the GNU General Public License as published by        # 
 12  # the Free Software Foundation; either version 2 of the License, or           # 
 13  # (at your option) any later version.                                         # 
 14  #                                                                             # 
 15  # relax is distributed in the hope that it will be useful,                    # 
 16  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 17  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 18  # GNU General Public License for more details.                                # 
 19  #                                                                             # 
 20  # You should have received a copy of the GNU General Public License           # 
 21  # along with relax; if not, write to the Free Software                        # 
 22  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 23  #                                                                             # 
 24  ############################################################################### 
 25   
 26  # Module docstring. 
 27  """ieeefloat a set of functions for dealing with IEEE-754 float objects. 
 28   
 29  On most platforms Python uses IEEE-754 double objects of length 64 bits to represent floats (some 
 30  architectures such as older Crays and Vaxes don't).  Thus an IEEE-754 double is the implementation 
 31  of a python float object on most platforms. 
 32   
 33  IEEE-74 uses special bit patterns to represent the following states or classes of IEEE floating 
 34  point numbers (IEEE-class): 
 35      - +/- NaN:    Not a number (e.g. 0.0/0.0). 
 36      - inf:        Positive or negative infinity (1.0/0.0). 
 37      - +/- zero:   Zero maybe positive or negative under IEEE-754. 
 38   
 39  This module provides functions for working with python floats and their special values, if they 
 40  contain IEEE-754 formatted values.  Specifically: 
 41      - Pack and unpack a list of bytes representing an IEEE-754 double to a python float (takes care 
 42        of little endian/big endian issues). 
 43      - Get the sign bit of a python float. 
 44      - Check the ordering of python floats allowing for NaNs (NaNs cannot normally be compared). 
 45      - Check if a value is finite (as opposed to NaN or inf). 
 46      - Copy the sign of one float to another irrespective of if it's IEEE-class. 
 47      - Check if a float is denormalised (and might be about to underflow). 
 48      - Check the IEEE-class of a python float (NaN, pos-inf, neg-inf, pos-zero, neg-zero, ...). 
 49      - Check that the current python float implementations uses IEEE-754 doubles. 
 50   
 51  It also provides constants containg specific bit patterns for NaN and +-inf as these values cannot 
 52  be generated from strings via the constructor float(x) with some compiler implementations (typically 
 53  older Microsoft Windows compilers). 
 54   
 55  As a convenience the names of functions and constants conform to those defined in the draft python 
 56  PEP 754 'IEEE 754 Floating Point Special Values'. 
 57   
 58  Notes: 
 59      1.  Binary data is documented as binary strings e.g. 0xF0 = 0b11110000. 
 60      2.  The module doesn't support all the functions recommended by IEEE-754, the following features 
 61          are missing: 
 62              - Control of exception and rounding modes. 
 63              - scalb(y, N). 
 64              - logb(x). 
 65              - nextafter(x,y). 
 66              - Next towards. 
 67      3.  Division by zero currently (python 2.5) raises exception and the resulting inf/NaN cannot be 
 68          propogated. 
 69      4.  A second module ieeefloatcapabilities (currently incomplete) provides tests of the 
 70          capabilities of a floating point implementation on a specific python platform. 
 71      5.  Development and conventions on byte order come from a little endian (Intel) platform. 
 72      6.  To reduce overheads all functions that take python float arguments do _no type_ conversion 
 73          thus if other numeric types are passed the functions will raise exceptions, (I am not sure 
 74          this is the best behaviour however, as python functions should be polymorphic...). 
 75      7.  In most cases conversion to C code for performance reasons would be trivial. 
 76   
 77  IEEE-754 double format: 
 78      - 63 sign bit. 
 79      - 62-52 exponent (offset by 1023 value - field-1023). 
 80      - 51-0 mantissa each bit n counts as 1/2^n, running from 1/2 which is the most significant bit 
 81        to 1/2^51, The 1/0 bit is defined by the exponent field if it has any bits set if it has bits 
 82        set then precede the mantissa with a 1 (normalised otherwise precede it by a 0 (denormalised). 
 83   
 84   
 85  Todo: 
 86      - Unit test suite. 
 87      - Test under Windows. 
 88      - Test under a Solaris Sparc box (big endian). 
 89      - Add example IEEE double. 
 90      - Check byte/nibble attributions. 
 91  """ 
 92  from struct import pack, unpack 
 93  import sys 
 94   
 95   
 96  SIGNBIT = 0x80 
 97  """Bit pattern for the sign bit in byte 8 0b00000001 of a IEEE-754 double.""" 
 98   
 99   
100  EXPONENT_ALL_ONES_BYTE_1 = 0x7F 
101  """Value of the first byte (byte 8) in the mantissa of a IEEE-754 double that is all ones 
102  (0b11111110).""" 
103   
104  EXPONENT_ALL_ONES_BYTE_0 = 0xF << 4 
105  """Value of the second byte (byte 7) in the mantissa of a IEEE-754 double that is all ones 
106  (0b00001111).""" 
107   
108   
109  MANTISSA_NIBBLE_MASK=0x0F 
110  """Mask to select the bits from the first nibble of  byte 7 of an IEEE-754 which is part of the 
111  mantissa (0b00001111).""" 
112   
113  EXPONENT_NIBBLE_MASK=0xF0 
114  """Mask to select the bits from the second nibble of  byte 7 of an IEEE-754 which is part of the 
115  exponent (0b11110000).""" 
116   
117   
118  EXPONENT_SIGN_MASK= 0x7F 
119  """Mask to select only bits from byte 8 of an IEEE-754 double that are not part of the sign bit 
120  (0b11111110).""" 
121   
122  """Classes of floating point numbers.""" 
123  CLASS_POS_INF = 1 
124  CLASS_NEG_INF = 2 
125  CLASS_POS_NORMAL = 4 
126  CLASS_NEG_NORMAL = 8 
127  CLASS_POS_DENORMAL = 16 
128  CLASS_NEG_DENORMAL = 32 
129  CLASS_QUIET_NAN =  64 
130  CLASS_SIGNAL_NAN = 128 
131  CLASS_POS_ZERO =  256 
132  CLASS_NEG_ZERO = 512 
133   
134   
135 -def isZero(float):
136 return isMantissaAllZeros(float) and isExpAllZeros(float)
137 138
139 -def getFloatClass(float):
140 """Get the IEEE-class (NaN, inf etc) of a python float. 141 142 @param float: Python float object. 143 @type float: float 144 @return: An IEEE class value. 145 @rtype: int 146 @raise TypeError: If float is not a python float object. 147 """ 148 149 result = None 150 151 # check finite 152 if isFinite(float): 153 # check and store is positive 154 positive = isPositive(float) 155 if isZero(float): 156 if positive: 157 result = CLASS_POS_ZERO 158 else: 159 result = CLASS_NEG_ZERO 160 elif isDenormalised(float): 161 if positive: 162 result = CLASS_POS_DENORMAL 163 else: 164 result = CLASS_NEG_DENORMAL 165 else: 166 if positive: 167 result = CLASS_POS_NORMAL 168 else: 169 result = CLASS_NEG_NORMAL 170 else: 171 if isNaN(float): 172 # we don't currently test the type of NaN signalling vs quiet 173 # so we always assume a quiet NaN 174 result = CLASS_QUIET_NAN 175 elif isPosInf(float): 176 result = CLASS_POS_INF 177 elif isNegInf(float): 178 result = CLASS_NEG_INF 179 return result
180 181
182 -def packBytesAsPyFloat(bytes):
183 """Pack 8 bytes into a python float. 184 185 The function is endian aware and the data should be input in little endian format. Thus byte 8 186 contains the most significant bit of the exponent and the sign bit. 187 188 @param bytes: 8 bytes to pack into a python (IEEE 754 double) float. 189 @type bytes: float 190 @return: A python float 191 @rtype: float 192 @raise TypeError: If bytes contains < 8 bytes type of exception not determined. 193 """ 194 195 # pack bytes into binary string 196 doubleString=pack('8B',*bytes) 197 198 #change byte order to little endian by reversing string 199 if sys.byteorder == 'big': 200 doubleString = doubleString[::-1] 201 202 # unpack binary string to a python float 203 return unpack('d', doubleString)[0]
204 205 206 NAN_BYTES = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x7F] 207 """Bytes for an arbitary IEEE-754 not a number (NaN) in little endian format 208 0b00000000 00000000 00000000 00000000 00000000 00000000 00011111 11111110.""" 209 210 211 INF_BYTES = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x7F] 212 """Bytes for IEEE-754 positive infinity (inf) in little endian format 213 0b00000000 00000000 00000000 00000000 00000000 00000000 00001111 11111110.""" 214 215 216 nan = packBytesAsPyFloat(NAN_BYTES) 217 """One of a number of possible bit patterns used to represent an IEEE-754 double as a python float. 218 Do not use this value for comparisons of the form x==NaN as it will fail on some platforms use 219 function isNaN instead.""" 220 221 222 pos_inf = packBytesAsPyFloat(INF_BYTES) 223 """The value of a positive IEEE-754 double infinity as a python float.""" 224 225 226 neg_inf = -1 * pos_inf 227 """The value of a negative IEEE-754 double infinity as a python float.""" 228 229
230 -def floatToBinaryString(obj):
231 """Pack a python float into a binary string. 232 233 This function assumes that the python type float it represents a 64bit double of 8 bytes. This 234 function reverses the resulting string if the current architecture is big endian. 235 236 @param obj: A python float to pack. 237 @type obj: float 238 @return: A string of 8 bytes. 239 @rtype: str 240 @raise TypeError: If the input object isn't a python float. 241 """ 242 243 if not isinstance(obj, float): 244 raise TypeError('the object recieved wasn\'t a float, type was: %s' % type(obj)) 245 246 # pack float into binary string 247 packed =pack('d', obj) 248 249 #change byte order to little endian by reversing string 250 if sys.byteorder == 'big': 251 packed = packed[::-1] 252 253 return packed
254 255
256 -def floatAsByteArray(obj):
257 """Unpack a python float as a list of 8 bytes. 258 259 This function assumes that the python type float it represents a 64bit double of 8 bytes. 260 261 @param obj: A python float to unpack. 262 @type obj: float 263 @return: A list of 7 bytes. 264 @rtype: list of str 265 @raise TypeError: If obj is not composed of 8 bytes. 266 """ 267 268 # unpack bytes to a binary string (takes care of byte order) 269 binaryString = floatToBinaryString(obj) 270 271 # convert the binary string to an array of 8 bytes 272 bytes = unpack('8B', binaryString) 273 274 #convert bytes to a list for ease of editing 275 return list(bytes)
276 277
278 -def getSignBit(obj):
279 """Get the sign bit from a python float. 280 281 @param obj: A python float object. 282 @type obj: float 283 @return: The float's sign bit, this has the value 1 if the float is negative 284 otherwise 0 (positive). 285 @rtype: bit 286 @raise TypeError: If the input object isn't a python float. 287 """ 288 289 # unpack float to bytes 290 unpacked = floatAsByteArray(obj) 291 292 # grab last byte and check if sign bit is set 293 return unpacked[7] & SIGNBIT
294 295
296 -def isPositive(obj):
297 """Test if a python float is positive. 298 299 @param obj: A python float object. 300 @type obj: float 301 @return: True if the float is positive otherwise False. 302 @rtype: bool 303 @raise TypeError: If the input object isn't a python float. 304 """ 305 306 if getSignBit(obj): 307 return False 308 else: 309 return True
310 311
312 -def isNegative(obj):
313 """Test if a python float 64 bit IEEE-74 double is negative. 314 315 @param obj: A python float object. 316 @type obj: float 317 @return: True if the float is negative. 318 @rtype: bool 319 @raise TypeError: If the input object isn't a python float. 320 """ 321 322 return not isPositive(obj)
323 324
325 -def areUnordered(obj1, obj2):
326 """Test to see if two python float are unordered. 327 328 Float comparison is unordered if either or both of the objects is 'not a number' (NaN). 329 330 @param obj1: A python float object 331 @type obj1: float 332 @param obj2: A python float object 333 @type obj2: float 334 @return: True if one of the args is a NaN. 335 @rtype: bool 336 @raise TypeError: If the input objects aren't python floats. 337 """ 338 339 # check to see if objects are NaNs 340 nanTest1 = isNaN(obj1) 341 nanTest2 = isNaN(obj2) 342 343 # if either object is a NaN we are unordered 344 if nanTest1 or nanTest2: 345 return True 346 else: 347 return False
348 349
350 -def isFinite(obj):
351 """Test to see if a python float is finite. 352 353 To be finite a number mustn't be a NaN or +/- inf. A result of True guarantees that the number 354 is bounded by +/- inf, -inf < x < inf. 355 356 @param obj: A python float object. 357 @type obj: float 358 @return: True if the float is finite. 359 @rtype: bool 360 @raise TypeError: If the input object isn't a python float. 361 """ 362 363 result = True 364 if isNaN(obj): 365 result = False 366 if isInf(obj): 367 result = False 368 369 370 return result
371 372
373 -def copySign(fromNumber, toDouble):
374 """Copy the sign bit from one python float to another. 375 376 This function is class agnostic the sign bit can be copied freely between ordinary floats, NaNs 377 and +/- inf. 378 379 @param fromNumber: The python float to copy the sign bit from. 380 @type fromNumber: float 381 @param toDouble: The python float to copy the sign bit to. 382 @type toDouble: float 383 @raise TypeError: If toDouble isn't a python float or if fromNumber can't be converted to a 384 float. 385 """ 386 387 #convert first number to a float so as to use facilities 388 fromNumber = float(fromNumber) 389 390 # check signs of numbers 391 fromIsPositive = isPositive(fromNumber) 392 toIsPositive = isPositive(toDouble) 393 394 # convert the float to an array of 8 bytes 395 toBytes = floatAsByteArray(toDouble) 396 397 if not toIsPositive and fromIsPositive: 398 # unset the sign bit of the number 399 toBytes[7] &= EXPONENT_SIGN_MASK 400 401 elif toIsPositive and not fromIsPositive: 402 # set the sign bit 403 toBytes[7] = toBytes[7] + 0x80 404 405 #repack bytes to float 406 return packBytesAsPyFloat(toBytes)
407 408
409 -def isDenormalised(obj):
410 """Check to see if a python float is denormalised. 411 412 Denormalisation indicates that the number has no exponent set and all the precision is in the 413 mantissa, this is an indication that the number is tending to towards underflow. 414 415 @param obj: Python float object to check. 416 @type obj: float 417 @return: True if the number is denormalised. 418 @rtype: bool 419 @raise TypeError: If toDouble isn't a python float or if obj isn't a float. 420 """ 421 422 result = True 423 # check to see if the exponent is all zeros (a denorm doesn't have a 424 # finite exponent) Note we ignore the sign of the float 425 if not isExpAllZeros(obj): 426 result = False 427 428 # check to see if this is zero (which is in some ways a special 429 # class of denorm... but isn't counted in this case) 430 # if it isn't zero it must be a 'general' denorm 431 if isZero(obj): 432 result = False 433 434 return result
435 436
437 -def getMantissaBytes(obj):
438 """Get the 7 bytes that makeup the mantissa of float. 439 440 The mantissa is returned as the 7 bytes in the mantissa in little endian order in the 7th byte 441 the 2nd nibble of the byte is masked off as it contains part of the exponent. The second nibble 442 of the 7th byte is therefore always has the value 0x0. 443 444 @param obj: Float object to extract the mantissa from. 445 @type obj: float 446 @return: A list of 7 bytes in little endian order. 447 @rtype: list of 7 bytes 448 @raise TypeError: If obj isn't a python float. 449 """ 450 451 # unpack float to bytes 452 bytes = floatAsByteArray(obj) 453 454 # mask out overlap from exponent 455 bytes[6] = bytes[6] & MANTISSA_NIBBLE_MASK 456 457 # remove the exponent bytes that can be removed 458 return bytes[:7]
459 460
461 -def getExponentBytes(obj):
462 """Get the 2 bytes that makeup the exponent of a float. 463 464 The exponent is returned as the 2 bytes in the exponent in little endian order in the 2nd byte 465 the last bit is masked off as this is the sign bit. Therefore all values have the last bit set 466 to zero. In the first byte the first nibble of the byte is also masked off as it contains part 467 of the mantissa and thus always has the value 0x0. 468 469 @param obj: Float object to extract the exponent from. 470 @type obj: float 471 @return: A list of 2 bytes in little endian order. 472 @rtype: list of 2 bytes 473 @raise TypeError: If obj isn't a python float. 474 """ 475 476 # unpack float to bytes 477 bytes = floatAsByteArray(obj) 478 479 # mask out the ovberlap with the mantissa 480 bytes[6] = bytes[6] & EXPONENT_NIBBLE_MASK 481 482 # mask out the sign bit 483 bytes[7] = bytes[7] & EXPONENT_SIGN_MASK 484 485 # remove the mantissa bytes that can be removed 486 return bytes[6:]
487 488
489 -def isExpAllZeros(obj):
490 """Check if the bits of the exponent of a float is zero. 491 492 @param obj: Float object to check exponent for zero value. 493 @type obj: float 494 @return: True if the exponent is zero. 495 @rtype: bool 496 @raise TypeError: If obj isn't a python float. 497 """ 498 499 result = True 500 501 # get the exponent as a byte array porperly masked 502 expBytes = getExponentBytes(obj) 503 504 # check to see if any of the bytes in the exponent are not zero 505 if expBytes[0] > 0 or expBytes[1] > 0: 506 result = False 507 508 return result
509 510
511 -def isMantissaAllZeros(obj):
512 """Check if the bits of the mantissa of a float is zero. 513 514 @param obj: Float object to check mantissa for zero value. 515 @type obj: float 516 @return: True if the mantissa is zero. 517 @rtype: bool 518 @raise TypeError: If obj isn't a python float. 519 """ 520 521 result = True 522 523 # get the mantissa as a byte array properly masked 524 mantissaBytes = getMantissaBytes(obj) 525 526 # check if any of the mantissa bytes are greater than zero 527 for byte in mantissaBytes: 528 if byte != 0: 529 result = False 530 break 531 532 return result
533 534
535 -def isExpAllOnes(obj):
536 """Check if the bits of the exponent of a float is all 1 bits. 537 538 @param obj: Float object to check exponent for 1 bits. 539 @type obj: float 540 @return: True if all the bits in the exponent are one. 541 @rtype: bool 542 @raise TypeError: If obj isn't a python float. 543 """ 544 545 result = False 546 547 # get the exponent as a byte array properly masked 548 expBytes = getExponentBytes(obj) 549 550 # check against masks to see if all the correct bits are set 551 if expBytes[0] == EXPONENT_ALL_ONES_BYTE_0 and expBytes[1] == EXPONENT_ALL_ONES_BYTE_1: 552 result = True 553 554 return result
555 556
557 -def isNaN(obj):
558 """Check to see if a python float is an IEEE-754 double not a number (NaN). 559 560 @param obj: Float object to check for not a number. 561 @type obj: float 562 @return: True if object is not a number. 563 @rtype: bool 564 @raise TypeError: If obj isn't a python float. 565 """ 566 567 # bad result for code checking 568 result = None 569 570 # check to see if exponent is all ones (excluding sign bit) 571 # if exponent is not all ones this can't be a NaN 572 if not isExpAllOnes(obj): 573 result = False 574 else: 575 # get the mantissa as a byte array properly masked 576 manBytes = getMantissaBytes(obj) 577 578 # check if any of the unmasked mantissa bytes are not zero 579 # to be a NaN the mantissa must be non zero 580 for byte in manBytes: 581 if byte > 0: 582 result = True 583 break 584 # todo NOT NEEDED, UNITTEST!!!! 585 # check to see if the mantissa nibble that overlaps with the 586 #if (manBytes[6] & MANTISSA_NIBBLE_MASK) > 0: 587 # result = True 588 return result
589 590
591 -def isInf(obj):
592 """Check to see if a python float is an infinity. 593 594 The check returns true for either positive or negative infinities. 595 596 @param obj: Float object to check for infinity. 597 @type obj: float 598 @return: True if object is an infinity. 599 @rtype: bool 600 @raise TypeError: If obj isn't a python float. 601 """ 602 603 # bad result for code checking 604 result = None 605 606 # check to see if exponent is all ones (excluding sign bit) 607 # if exponent is not all ones this can't be a Inf 608 if not isExpAllOnes(obj): 609 result = False 610 else: 611 # get the mantissa as a byte array properly masked 612 manBytes = getMantissaBytes(obj) 613 614 for byte in manBytes: 615 #check if any of the unmasked mantissa bytes are zero 616 # to be a NaN the mantissa must be zero 617 if byte > 0: 618 result = False 619 break 620 result = True 621 622 return result
623 624
625 -def isPosInf(obj):
626 """Check to see if a python float is positive infinity. 627 628 @param obj: Float object to check for positive infinity. 629 @type obj: float 630 @return: True if object is a positive infinity. 631 @rtype: bool 632 @raise TypeError: If obj isn't a python float. 633 """ 634 635 return isInf(obj) and isPositive(obj)
636 637
638 -def isNegInf(obj):
639 """Check to see if a python float is negative infinity. 640 641 @param obj: Float object to check for negative infinity. 642 @type obj: float 643 @return: True if object is a negative infinity. 644 @rtype: bool 645 @raise TypeError: If obj isn't a python float. 646 """ 647 648 return isInf(obj) and not isPositive(obj)
649 650
651 -def bitpatternToFloat(string, endian='big'):
652 """Convert a 64 bit IEEE-754 ascii bit pattern into a 64 bit Python float. 653 654 @param string: The ascii bit pattern repesenting the IEEE-754 float. 655 @type string: str 656 @param endian: The endianness of the bit pattern (can be 'big' or 'little'). 657 @type endian: str 658 @return: The 64 bit float corresponding to the IEEE-754 bit pattern. 659 @returntype: float 660 @raise TypeError: If 'string' is not a string, the length of the 'string' is not 64, or if 661 'string' does not consist solely of the characters '0' and '1'. 662 """ 663 664 # Test that the bit pattern is a string. 665 if not isinstance(string, str): 666 raise TypeError("The string argument '%s' is not a string." % string) 667 668 # Test the length of the bit pattern. 669 if len(string) != 64: 670 raise TypeError("The string '%s' is not 64 characters long." % string) 671 672 # Test that the string consists solely of zeros and ones. 673 for char in string: 674 if char not in ['0', '1']: 675 raise TypeError("The string '%s' should be composed solely of the characters '0' and '1'." % string) 676 677 # Reverse the byte order as neccessary. 678 if endian == 'big' and sys.byteorder == 'little': 679 string = string[::-1] 680 elif endian == 'little' and sys.byteorder == 'big': 681 string = string[::-1] 682 683 # Convert the bit pattern into a byte array (of integers). 684 bytes = [] 685 for i in xrange(8): 686 bytes.append(bitpatternToInt(string[i*8:i*8+8], endian=sys.byteorder)) 687 688 # Pack the byte array into a float and return it. 689 return packBytesAsPyFloat(bytes)
690 691
692 -def bitpatternToInt(string, endian='big'):
693 """Convert a bit pattern into its integer representation. 694 695 @param string: The ascii string repesenting binary data. 696 @type string: str 697 @param endian: The endianness of the bit pattern (can be 'big' or 'little'). 698 @type endian: str 699 @return: The integer value. 700 @returntype: int 701 """ 702 703 # Test that the bit pattern is a string. 704 if not isinstance(string, str): 705 raise TypeError("The string argument '%s' is not a string." % string) 706 707 # Test that the string consists solely of zeros and ones. 708 for char in string: 709 if char not in ['0', '1']: 710 raise TypeError("The string '%s' should be composed solely of the characters '0' and '1'." % string) 711 712 # Reverse the byte order as neccessary. 713 if endian == 'big' and sys.byteorder == 'little': 714 string = string[::-1] 715 elif endian == 'little' and sys.byteorder == 'big': 716 string = string[::-1] 717 718 # Calculate the integer corresponding to the string. 719 int_val = 0 720 for i in xrange(len(string)): 721 if int(string[i]): 722 int_val = int_val + 2**i 723 724 # Return the integer value. 725 return int_val
726 727 728 # IEEE-754 Constants. 729 ##################### 730 731 # The following bit patterns are to be read from right to left (big-endian). 732 # Hence bit positions 0 and 63 are to the far right and far left respectively. 733 PosZero = bitpatternToFloat('0000000000000000000000000000000000000000000000000000000000000000', endian='big') 734 NegZero = bitpatternToFloat('1000000000000000000000000000000000000000000000000000000000000000', endian='big') 735 PosEpsilonDenorm = bitpatternToFloat('0000000000000000000000000000000000000000000000000000000000000001', endian='big') 736 NegEpsilonDenorm = bitpatternToFloat('1000000000000000000000000000000000000000000000000000000000000001', endian='big') 737 PosEpsilonNorm = bitpatternToFloat('0000000000010000000000000000000000000000000000000000000000000001', endian='big') 738 NegEpsilonNorm = bitpatternToFloat('1000000000010000000000000000000000000000000000000000000000000001', endian='big') 739 PosMax = bitpatternToFloat('0111111111101111111111111111111111111111111111111111111111111111', endian='big') 740 NegMin = bitpatternToFloat('1111111111101111111111111111111111111111111111111111111111111111', endian='big') 741 PosInf = bitpatternToFloat('0111111111110000000000000000000000000000000000000000000000000000', endian='big') 742 NegInf = bitpatternToFloat('1111111111110000000000000000000000000000000000000000000000000000', endian='big') 743 PosNaN_A = bitpatternToFloat('0111111111110000000000000000000000000000001000000000000000000000', endian='big') 744 NegNaN_A = bitpatternToFloat('1111111111110000000000000000000000000000001000000000000000000000', endian='big') 745 PosNaN_B = bitpatternToFloat('0111111111110000000000000000011111111111111111111110000000000000', endian='big') 746 NegNaN_B = bitpatternToFloat('1111111111110000000000000000011111111111111111111110000000000000', endian='big') 747 PosNaN_C = bitpatternToFloat('0111111111110101010101010101010101010101010101010101010101010101', endian='big') 748 NegNaN_C = bitpatternToFloat('1111111111110101010101010101010101010101010101010101010101010101', endian='big') 749 PosNaN = PosNaN_C 750 NegNaN = NegNaN_C 751 752 #print("%-30s%-20.40g" % ("Pos zero: ", PosZero)) 753 #print("%-30s%-20.40g" % ("Neg zero: ", NegZero)) 754 #print("%-30s%-20.40g" % ("Pos epsilon denorm: ", PosEpsilonDenorm)) 755 #print("%-30s%-20.40g" % ("Neg epsilon denorm: ", NegEpsilonDenorm)) 756 #print("%-30s%-20.40g" % ("Pos epsilon norm: ", PosEpsilonNorm)) 757 #print("%-30s%-20.40g" % ("Neg epsilon norm: ", NegEpsilonNorm)) 758 #print("%-30s%-20.40g" % ("Max: ", PosMax)) 759 #print("%-30s%-20.40g" % ("Min: ", NegMin) 760 #print("%-30s%-20.40g" % ("Pos inf: ", PosInf))) 761 #print("%-30s%-20.40g" % ("Neg inf: ", NegInf)) 762 #print("%-30s%-20.40g" % ("Pos NaN (A): ", PosNaN_A)) 763 #print("%-30s%-20.40g" % ("Neg NaN (A): ", NegNaN_A)) 764 #print("%-30s%-20.40g" % ("Pos NaN (B): ", PosNaN_B)) 765 #print("%-30s%-20.40g" % ("Neg NaN (B): ", NegNaN_B)) 766 #print("%-30s%-20.40g" % ("Pos NaN (C): ", PosNaN_C)) 767 #print("%-30s%-20.40g" % ("Neg NaN (C): ", NegNaN_C)) 768