puzzles: fix floating-point formatting

This is pretty ad-hoc, but the only other ways are to rewrite
sprintf (which would use too much memory on the c200v2), or
implement support for floats in rockbox's formatter, neither of
which are acceptable.

Change-Id: I70d59fd3e90a16e2db9ae0a84cd8c14807f50b46
This commit is contained in:
Franklin Wei 2017-08-16 11:35:32 -04:00
parent bf25f3e6e7
commit c78ff7f615
6 changed files with 23 additions and 7 deletions

View file

@ -15,6 +15,7 @@ double cos_wrapper(double rads);
int vsprintf_wrapper(char *s, const char *fmt, va_list ap);
float fabs_wrapper(float n);
float floor_wrapper(float n);
int ftoa(char *buf, int len, float f);
float atan_wrapper(float x);
float atan2_wrapper(float y, float x);

View file

@ -23,6 +23,12 @@ int puts_wrapper(const char *s)
return 0;
}
int ftoa(char *buf, int len, float f)
{
/* biggest hack ever */
return rb->snprintf(buf, len, "%d.%06d", (int)f, (int)((f - (int)f)*1e6));
}
/* fixed-point wrappers */
static long lastphase = 0, lastsin = 0, lastcos = 0x7fffffff;

View file

@ -1701,7 +1701,7 @@ void midend_serialise(midend *me,
*/
if (me->ourgame->is_timed) {
char buf[80];
sprintf(buf, "%g", me->elapsed);
ftoa(buf, 80, me->elapsed);
wr("TIME", buf);
}

View file

@ -257,7 +257,10 @@ static char *encode_params(const game_params *params, int full)
if (params->wrapping)
ret[len++] = 'w';
if (full && params->barrier_probability)
len += sprintf(ret+len, "b%g", params->barrier_probability);
{
len += sprintf(ret+len, "b");
len += ftoa(ret + len, 400, params->barrier_probability);
}
if (full && !params->unique)
ret[len++] = 'a';
assert(len < lenof(ret));
@ -292,7 +295,7 @@ static config_item *game_configure(const game_params *params)
ret[3].name = "Barrier probability";
ret[3].type = C_STRING;
sprintf(buf, "%g", params->barrier_probability);
ftoa(buf, 80, params->barrier_probability);
ret[3].sval = dupstr(buf);
ret[3].ival = 0;

View file

@ -241,7 +241,10 @@ static char *encode_params(const game_params *params, int full)
if (params->wrapping)
ret[len++] = 'w';
if (full && params->barrier_probability)
len += sprintf(ret+len, "b%g", params->barrier_probability);
{
len += sprintf(ret+len, "b");
len += ftoa(ret + len, 400, params->barrier_probability);
}
/* Shuffle limit is part of the limited parameters, because we have to
* provide the target move count. */
if (params->movetarget)
@ -278,7 +281,7 @@ static config_item *game_configure(const game_params *params)
ret[3].name = "Barrier probability";
ret[3].type = C_STRING;
sprintf(buf, "%g", params->barrier_probability);
ftoa(buf, 80, params->barrier_probability);
ret[3].sval = dupstr(buf);
ret[3].ival = 0;

View file

@ -163,7 +163,10 @@ static char *encode_params(const game_params *params, int full)
sprintf(data, "%dx%d", params->w, params->h);
if (full && params->expandfactor)
sprintf(data + strlen(data), "e%g", params->expandfactor);
{
sprintf(data + strlen(data), "e");
ftoa(data + strlen(data), 256, params->expandfactor);
}
if (full && !params->unique)
strcat(data, "a");
@ -191,7 +194,7 @@ static config_item *game_configure(const game_params *params)
ret[2].name = "Expansion factor";
ret[2].type = C_STRING;
sprintf(buf, "%g", params->expandfactor);
ftoa(buf, 80, params->expandfactor);
ret[2].sval = dupstr(buf);
ret[2].ival = 0;