gskslprinter: Deal with non-normal floating points

Instead of NaN, print "(0.0 / 0.0)" and instead of INF, print "(1.0 /
0.0)".

Both of that is not spec-conformant, but the best we can do if we
encounter constant expressions that evaluate to these numbers.
This commit is contained in:
Benjamin Otte
2017-10-04 03:34:55 +02:00
parent 420fb00e1e
commit 6bdf46fbcf
3 changed files with 43 additions and 12 deletions

View File

@@ -127,16 +127,47 @@ gsk_sl_printer_append_uint (GskSlPrinter *printer,
}
void
gsk_sl_printer_append_double (GskSlPrinter *printer,
double d,
gboolean with_dot)
gsk_sl_printer_append_float (GskSlPrinter *printer,
float f)
{
char buf[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr (buf, G_ASCII_DTOSTR_BUF_SIZE, d);
g_string_append (printer->string, buf);
if (with_dot && strchr (buf, '.') == NULL)
g_string_append (printer->string, ".0");
if (isnanf (f))
g_string_append (printer->string, "(0.0 / 0.0)");
else if (isinff (f) > 0)
g_string_append (printer->string, "(1.0 / 0.0)");
else if (isinff (f) < 0)
g_string_append (printer->string, "(-1.0 / 0.0)");
else
{
g_ascii_dtostr (buf, G_ASCII_DTOSTR_BUF_SIZE, f);
g_string_append (printer->string, buf);
if (strchr (buf, '.') == NULL)
g_string_append (printer->string, ".0");
}
}
void
gsk_sl_printer_append_double (GskSlPrinter *printer,
double d)
{
char buf[G_ASCII_DTOSTR_BUF_SIZE];
if (isnan (d))
g_string_append (printer->string, "(0.0lf / 0.0lf)");
else if (isinf (d) > 0)
g_string_append (printer->string, "(1.0lf / 0.0lf)");
else if (isinf (d) < 0)
g_string_append (printer->string, "(-1.0lf / 0.0lf)");
else
{
g_ascii_dtostr (buf, G_ASCII_DTOSTR_BUF_SIZE, d);
g_string_append (printer->string, buf);
if (strchr (buf, '.') == NULL)
g_string_append (printer->string, ".0lf");
else
g_string_append (printer->string, "lf");
}
}
void

View File

@@ -43,9 +43,10 @@ void gsk_sl_printer_append_int (GskSlPrinter
int i);
void gsk_sl_printer_append_uint (GskSlPrinter *printer,
guint u);
void gsk_sl_printer_append_float (GskSlPrinter *printer,
float f);
void gsk_sl_printer_append_double (GskSlPrinter *printer,
double d,
gboolean with_dot);
double d);
void gsk_sl_printer_newline (GskSlPrinter *printer);
G_END_DECLS

View File

@@ -95,7 +95,7 @@ print_float (GskSlPrinter *printer,
{
const gfloat *f = value;
gsk_sl_printer_append_double (printer, *f, TRUE);
gsk_sl_printer_append_float (printer, *f);
}
static guint32
@@ -122,8 +122,7 @@ print_double (GskSlPrinter *printer,
{
const gdouble *d = value;
gsk_sl_printer_append_double (printer, *d, TRUE);
gsk_sl_printer_append (printer, "lf");
gsk_sl_printer_append_double (printer, *d);
}
static guint32