encoder.js
function getNumType( vmin, vmax, is_float ) {
	if (typeof vmin !== "number") return NUMTYPE.NAN;
	if (typeof vmax !== "number") return NUMTYPE.NAN;
	if (isNaN(vmin) || isNaN(vmax)) return NUMTYPE.NAN;

	// If float, test only-floats
	if (is_float) {

		// Try to find smallest value for float32 minimum tests
		var smallest;
		if (vmin === 0) {
			// vmin is 0, which makes vmax positive, so 
			// test vmax for smallest
			smallest = vmax;
		} else if (vmax === 0) {
			// if vmax is 0, it makes vmin negative, which
			// means we should test it's positive version
			smallest = -vmax;
		} else {
			// if vmin is positive, both values are positive
			// so get the smallest for small test (vmin)
			if (vmin > 0) {
				smallest = vmin;

			// if vmax is negative, both values are negative
			// so get the biggest for small test (vmax)
			} else if (vmax < 0) {
				smallest = -vmax;

			// if vmin is negative and vmax positive, get the
			// smallest of their absolute values
			} else if ((vmin < 0) && (vmax > 0)) {
				smallest = -vmin;
				if (vmax < smallest) smallest = vmax;
			}
		}

		// Test if float number fits on 32 or 64 bits
		if ((vmin > FLOAT32_NEG) && (vmax < FLOAT32_POS) && (smallest > FLOAT32_SMALL)) {
			return NUMTYPE.FLOAT32;
		} else {
			return NUMTYPE.FLOAT64;
		}

	}

	// If we have a negative value, switch to signed tests
	if ((vmax < 0) || (vmin < 0)) {

		// Get absolute maximum of bound values
		var amax = -vmin;
		if (vmax < 0) {
			if (-vmax > amax) amax = -vmax;
		} else {
			if (vmax > amax) amax = vmax;
		}

		// Test for integer bounds
		if (amax < INT8_MAX) {
			return NUMTYPE.INT8;

		} else if (amax < INT16_MAX) {
			return NUMTYPE.INT16;

		} else if (amax < INT32_MAX) {
			return NUMTYPE.INT32;

		} else {
			return NUMTYPE.FLOAT64;

		}

	// Otherwise perform unsigned tests
	} else {

		// Check for unsigned cases
		if (vmax < UINT8_MAX) {
			return NUMTYPE.UINT8;

		} else if (vmax < UINT16_MAX) {
			return NUMTYPE.UINT16;

		} else if (vmax < UINT32_MAX) {
			return NUMTYPE.UINT32;

		} else {
			return NUMTYPE.FLOAT64;

		}

	}

}
function getNumType( vmin, vmax, is_float ) {
	if (typeof vmin !== "number") return NUMTYPE.NAN;
	if (typeof vmax !== "number") return NUMTYPE.NAN;
	if (isNaN(vmin) || isNaN(vmax)) return NUMTYPE.NAN;

	// If float, test only-floats
	if (is_float) {

		// Try to find smallest value for float32 minimum tests
		var smallest;
		if (vmin === 0) {
			// vmin is 0, which makes vmax positive, so 
			// test vmax for smallest
			smallest = vmax;
		} else if (vmax === 0) {
			// if vmax is 0, it makes vmin negative, which
			// means we should test it's positive version
			smallest = -vmax;
		} else {
			// if vmin is positive, both values are positive
			// so get the smallest for small test (vmin)
			if (vmin > 0) {
				smallest = vmin;

			// if vmax is negative, both values are negative
			// so get the biggest for small test (vmax)
			} else if (vmax < 0) {
				smallest = -vmax;

			// if vmin is negative and vmax positive, get the
			// smallest of their absolute values
			} else if ((vmin < 0) && (vmax > 0)) {
				smallest = -vmin;
				if (vmax < smallest) smallest = vmax;
			}
		}

		// Test if float number fits on 32 or 64 bits
		if ((vmin > FLOAT32_NEG) && (vmax < FLOAT32_POS) && (smallest > FLOAT32_SMALL)) {
			return NUMTYPE.FLOAT32;
		} else {
			return NUMTYPE.FLOAT64;
		}

	}

	// If we have a negative value, switch to signed tests
	if ((vmax < 0) || (vmin < 0)) {

		// Get absolute maximum of bound values
		var amax = -vmin;
		if (vmax < 0) {
			if (-vmax > amax) amax = -vmax;
		} else {
			if (vmax > amax) amax = vmax;
		}

		// Test for integer bounds
		if (amax < INT8_MAX) {
			return NUMTYPE.INT8;

		} else if (amax < INT16_MAX) {
			return NUMTYPE.INT16;

		} else if (amax < INT32_MAX) {
			return NUMTYPE.INT32;

		} else {
			return NUMTYPE.FLOAT64;

		}

	// Otherwise perform unsigned tests
	} else {

		// Check for unsigned cases
		if (vmax < UINT8_MAX) {
			return NUMTYPE.UINT8;

		} else if (vmax < UINT16_MAX) {
			return NUMTYPE.UINT16;

		} else if (vmax < UINT32_MAX) {
			return NUMTYPE.UINT32;

		} else {
			return NUMTYPE.FLOAT64;

		}

	}

}
function getNumType ( vmin , vmax , is_float ) { if ( typeof vmin !== "number" ) return NUMTYPE . NAN ; if ( typeof vmax !== "number" ) return NUMTYPE . NAN ; if ( isNaN ( vmin ) || isNaN ( vmax ) ) return NUMTYPE . NAN ; // If float, test only-floats if ( is_float ) { // Try to find smallest value for float32 minimum tests var smallest ; if ( vmin === 0 ) { // vmin is 0, which makes vmax positive, so  // test vmax for smallest smallest = vmax ; } else if ( vmax === 0 ) { // if vmax is 0, it makes vmin negative, which // means we should test it's positive version smallest = - vmax ; } else { // if vmin is positive, both values are positive // so get the smallest for small test (vmin) if ( vmin > 0 ) { smallest = vmin ; // if vmax is negative, both values are negative // so get the biggest for small test (vmax) } else if ( vmax < 0 ) { smallest = - vmax ; // if vmin is negative and vmax positive, get the // smallest of their absolute values } else if ( ( vmin < 0 ) && ( vmax > 0 ) ) { smallest = - vmin ; if ( vmax < smallest ) smallest = vmax ; } } // Test if float number fits on 32 or 64 bits if ( ( vmin > FLOAT32_NEG ) && ( vmax < FLOAT32_POS ) && ( smallest > FLOAT32_SMALL ) ) { return NUMTYPE . FLOAT32 ; } else { return NUMTYPE . FLOAT64 ; } } // If we have a negative value, switch to signed tests if ( ( vmax < 0 ) || ( vmin < 0 ) ) { // Get absolute maximum of bound values var amax = - vmin ; if ( vmax < 0 ) { if ( - vmax > amax ) amax = - vmax ; } else { if ( vmax > amax ) amax = vmax ; } // Test for integer bounds if ( amax < INT8_MAX ) { return NUMTYPE . INT8 ; } else if ( amax < INT16_MAX ) { return NUMTYPE . INT16 ; } else if ( amax < INT32_MAX ) { return NUMTYPE . INT32 ; } else { return NUMTYPE . FLOAT64 ; } // Otherwise perform unsigned tests } else { // Check for unsigned cases if ( vmax < UINT8_MAX ) { return NUMTYPE . UINT8 ; } else if ( vmax < UINT16_MAX ) { return NUMTYPE . UINT16 ; } else if ( vmax < UINT32_MAX ) { return NUMTYPE . UINT32 ; } else { return NUMTYPE . FLOAT64 ; } } }
Get the smallest possible numeric type fits this numberic bounds

@param {number} vmin - The minimum number to check
@param {number} vmax - The maximum number to check
@param {boolean} is_float - Set to 'true' to assume that the numbers are float
@return {NUMTYPE} - The numerical type to rerutn
Get the smallest possible numeric type fits this numberic bounds