MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / quotefloat

Function quotefloat

lib/lua/src/lstrlib.c:1151–1172  ·  view source on GitHub ↗

** 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.) */

Source from the content-addressed store, hash-verified

1149** (NaN cannot be expressed as a numeral, so we write '(0/0)' for it.)
1150*/
1151static 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
1175static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {

Callers 1

addliteralFunction · 0.85

Calls 1

lua_number2strxFunction · 0.85

Tested by

no test coverage detected