** Serialize a floating-point number in such a way that it can be ** scanned back by Lua. Use hexadecimal format for "common" numbers ** (to preserve precision); inf, -inf, and NaN are handled separately. ** (NaN cannot be expressed as a numeral, so we write '(0/0)' for it.) */
| 1149 | ** (NaN cannot be expressed as a numeral, so we write '(0/0)' for it.) |
| 1150 | */ |
| 1151 | static int quotefloat (lua_State *L, char *buff, lua_Number n) { |
| 1152 | const char *s; /* for the fixed representations */ |
| 1153 | if (n == (lua_Number)HUGE_VAL) /* inf? */ |
| 1154 | s = "1e9999"; |
| 1155 | else if (n == -(lua_Number)HUGE_VAL) /* -inf? */ |
| 1156 | s = "-1e9999"; |
| 1157 | else if (n != n) /* NaN? */ |
| 1158 | s = "(0/0)"; |
| 1159 | else { /* format number as hexadecimal */ |
| 1160 | int nb = lua_number2strx(L, buff, MAX_ITEM, |
| 1161 | "%" LUA_NUMBER_FRMLEN "a", n); |
| 1162 | /* ensures that 'buff' string uses a dot as the radix character */ |
| 1163 | if (memchr(buff, '.', nb) == NULL) { /* no dot? */ |
| 1164 | char point = lua_getlocaledecpoint(); /* try locale point */ |
| 1165 | char *ppoint = (char *)memchr(buff, point, nb); |
| 1166 | if (ppoint) *ppoint = '.'; /* change it to a dot */ |
| 1167 | } |
| 1168 | return nb; |
| 1169 | } |
| 1170 | /* for the fixed representations */ |
| 1171 | return l_sprintf(buff, MAX_ITEM, "%s", s); |
| 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { |
no test coverage detected