Ejemplo n.º 1
0
float LCG::FloatNeg1_1()
{
	u32 i = (u32)Int();
	u32 one = ((i & 0x00800000) << 8) /* random sign bit */ | 0x3F800000 /* fixed exponent */;
	i = one | (i & 0x007FFFFF) /* random mantissa */;
	float f = ReinterpretAsFloat(i); // f is now in range ]-2, -1[ union [1, 2].
	float fone = ReinterpretAsFloat(one); // +/- 1, of same sign as f.
	return f - fone;
}
Ejemplo n.º 2
0
float LCG::Float()
{
	u32 i = ((u32)Int() & 0x007FFFFF /* random mantissa */) | 0x3F800000 /* fixed exponent */;
	float f = ReinterpretAsFloat(i); // f is now in range [1, 2[
	f -= 1.f; // Map to range [0, 1[
	return f;
}
Ejemplo n.º 3
0
float LCG::Float01Incl()
{
	for(int i = 0; i < 100; ++i)
	{
		u32 val = (u32)Int() & 0x00FFFFFF;
		if (val > 0x800000)
			continue;
		else if (val == 0x800000)
			return 1.0f;
		else
		{
			val |= 0x3F800000;
			return ReinterpretAsFloat(val) - 1.f;
		}
	}
	return Float();
}
Ejemplo n.º 4
0
float DeserializeFloat(const char *str, const char **outEndStr)
{
	if (!str)
		return FLOAT_NAN;
	while(*str > 0 && *str <= ' ')
		++str;
	if (*str == 0)
		return FLOAT_NAN;
	if (MATH_NEXT_WORD_IS(str, "NaN("))
	{
		str += strlen("NaN("); //MATH_SKIP_WORD(str, "NaN(");
		u32 x;
		int n = sscanf(str, "%X", (unsigned int *)&x);
		if (n != 1)
			return FLOAT_NAN;
		while(*str != 0)
		{
			++str;
			if (*str == ')')
			{
				++str;
				break;
			}
		}
		if (outEndStr)
			*outEndStr = str;
		return ReinterpretAsFloat(x);
	}
	float f;

	if (!strncmp(str, "-inf", 4)) { f = -FLOAT_INF; str += 4; }
	else if (!strncmp(str, "inf", 3)) { f = FLOAT_INF; str += 3; }
	else f = (float)strtod(str, const_cast<char**>(&str));

	while(*str > 0 && *str <= ' ')
		++str;
	if (*str == ',' || *str == ';')
		++str;
	while(*str > 0 && *str <= ' ')
		++str;
	if (outEndStr)
		*outEndStr = str;
	return f;
}