#include "bitutils.h"

// General purpose linear mapping functions that given a bitstring
// of "size" bits, will generate a real number in a given range.
//
// Variations of this routine can add a constant to the resulting real to
// shift the region anywhere on the number line.  Any mapping function
// can be used if you think it will focus the attention of the algorithm
// in the right region.  Note these routines are inlined.
//
inline double bit2Real(UINT x, double offset, double scale)
{
    return (x-offset)*scale;
}


int main()
{
    const double maxx=3.0;
    const double maxy=3.0;  // y domain [-maxy, +maxy]
    const double maxz=3.0;  // z domain [-maxy, +maxy]
    const int size=10;      // assume 10 bit code to map to real number


    unsigned int gene, newgene;
    double offsetx, scalex;
    double offsety, scaley;
    unsigned int zeroPoint;  // what bitstring to associate with zero
    double offsetz, scalez;


    // // // // // // // // // // // // // // // // // // 
    //
    // x domain 10 bits -> [0, maxx]
    // 
    offsetx = 0.0;
    scalex = maxx/((1<<size)-1);

    // make sample calls at each end of range of bits
    printf("x:%f x:%f\n", 
	   bit2Real(0, offsetx, scalex), 
	   bit2Real((1<<size)-1, offsetx, scalex));



    // // // // // // // // // // // // // // // // // // 
    //
    // y domain 10 bits -> [-maxy, maxy]
    // 
    offsety = ((1<<size)-1)/2.0;
    scaley = maxy/offsety;

    // make sample calls at each end of range of bits    
    printf("y:%f y:%f\n", 
	   bit2Real(0, offsety, scaley), 
	   bit2Real((1<<size)-1, offsety, scaley));

    // note that in the middle zero is not an allowed value for y
    printf("y:%f y:%f\n", 
	   bit2Real((1<<size-1)-1, offsety, scaley), // negative
	   bit2Real(1<<size-1, offsety, scaley));    // positive



    // // // // // // // // // // // // // // // // // // 
    //
    // z domain 10 bits such that bitpattern in zeroPoint -> 0.0
    //                                       and all ones -> maxy
    // 
    zeroPoint = (1<<size-1)-1;
    offsetz = zeroPoint;
    scalez = maxy/((1<<size)-1-zeroPoint);
	
    // make sample calls at each end of range of bits    
    printf("z:%f z:%f\n", 
	   bit2Real(0, offsetz, scalez), 
	   bit2Real((1<<size)-1, offsetz, scalez));

    // note that zero is where it should be
    printf("z:%f z:%f z:%f\n", 
	   bit2Real(zeroPoint-1, offsetz, scalez), 
	   bit2Real(zeroPoint, offsetz, scalez),
	   bit2Real(zeroPoint+1, offsetz, scalez));
}
