Attempted precision fix for ftos

git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@2288 fc73d0e0-1445-4013-8a0c-d673dee63da5
This commit is contained in:
TimeServ 2006-05-19 03:45:44 +00:00
parent dccc3efb10
commit bb98dc4939
3 changed files with 50 additions and 1 deletions

View File

@ -328,6 +328,54 @@ int Q_strcasecmp (char *s1, char *s2)
#endif
// Q_ftoa: convert IEEE 754 float to a base-10 string with "infinite" decimal places
void Q_ftoa(char *str, float in)
{
unsigned int i = *((float *)&in);
int signbit = (i & 0x80000000) >> 31;
int exp = (signed int)((i & 0x7F800000) >> 23) - 127;
int mantissa = (i & 0x007FFFFF);
if (exp == 128) // 255(NaN/Infinity bits) - 127(bias)
{
if (signbit)
{
*str = '-';
str++;
}
if (mantissa == 0) // infinity
strcpy(str, "1.#INF");
else // NaN or indeterminate
strcpy(str, "1.#NAN");
return;
}
exp = -exp;
exp = (int)(exp * 0.30102999957f); // convert base 2 to base 10
exp += 11; //
if (exp <= 0)
sprintf(str, "%f", in);
else
{
char tstr[8];
char *lsig = str - 1; // last significant character
sprintf(tstr, "%%.%if", exp);
sprintf(str, tstr, in);
// find last significant digit and trim
while (*str)
{
if (*str >= '1' && *str <= '9')
lsig = str;
else if (*str == '.')
lsig = str - 1;
str++;
}
lsig[1] = '\0';
}
}
char *Q_strlwr(char *s)
{
char *ret=s;

View File

@ -169,6 +169,7 @@ void MSG_ReadData (void *data, int len);
char *Q_strcpyline(char *out, char *in, int maxlen); //stops at '\n' (and '\r')
void Q_ftoa(char *str, float in);
char *Q_strlwr(char *str);
int wildcmp(char *wild, char *string); //1 if match

View File

@ -3525,7 +3525,7 @@ void PF_ftos (progfuncs_t *prinst, struct globalvars_s *pr_globals)
else if (pr_brokenfloatconvert.value)
sprintf (pr_string_temp, "%5.1f",v);
else
sprintf (pr_string_temp, "%f",v);
Q_ftoa (pr_string_temp, v);
RETURN_TSTRING(pr_string_temp);
}