dan mackinlay avatar dan mackinlay committed ad94101

test mantissa wrangling

Comments (0)

Files changed (1)


 import random
 import math
+MANTISSA_BITS = 53 #max python 64 bit mantissa size
 def G1DListCrossoverMean(genome, **args):
     """Ultra-stupid crossover taking the mean of both parents. Not very useful -
     AFAICT it results in pressure to converge to the population mean rather than
 def G1DListCrossoverBitwise(genome, **args):
     """bitwise uniform crossover for floats"""
     sister = None
     brother = None
     gMom = args["mom"]
     for i in xrange(len(gMom)):
-        numMask = random.randbits(53) #maximal python mantissa length
-        denomMask = random.randbits(53)
-        numDad, denomDad = gDad[i].as_integer_ratio()
-        numMom, denomMom = gMom[i].as_integer_ratio()
+        numMask = random.randbits(MANTISSA_BITS) 
+        denomMask = random.randbits(MANTISSA_BITS)
+        mantissaDad, exponentDad = math.frexpr(gDad[i])
+        mantissaMom, exponentMom = math.frexpr(gMom[i])
+        mantissaMom = mantissaMom*MANTISSA_DENOM
         sister[i] = bitcross(numDad, numMum, numMask) / \
           bitcross(denomDad, denomMum, denomMask)
     return (sister, brother)
+def test_mantissa_mathematics():
+    """does all my mantisssa maths work? well, if this is stable it does"""
+    def rndtrip(f): return int((2<<53)*f)/float(2<<53)
+    f=1.0/3.0
+    for i in range(100):
+       print f.hex()
+       f=rndtrip(f)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.