** Add a float to list of constants and return its index. Floats ** with integral values need a different key, to avoid collision ** with actual integers. To that, we add to the number its smaller ** power-of-two fraction that is still significant in its scale. ** For doubles, that would be 1/2^52. ** (This method is not bulletproof: there may be another float ** with that value, and for floats la
| 600 | ** a duplicate.) |
| 601 | */ |
| 602 | static int luaK_numberK (FuncState *fs, lua_Number r) { |
| 603 | TValue o; |
| 604 | lua_Integer ik; |
| 605 | setfltvalue(&o, r); |
| 606 | if (!luaV_flttointeger(r, &ik, F2Ieq)) /* not an integral value? */ |
| 607 | return addk(fs, &o, &o); /* use number itself as key */ |
| 608 | else { /* must build an alternative key */ |
| 609 | const int nbm = l_floatatt(MANT_DIG); |
| 610 | const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1); |
| 611 | const lua_Number k = (ik == 0) ? q : r + r*q; /* new key */ |
| 612 | TValue kv; |
| 613 | setfltvalue(&kv, k); |
| 614 | /* result is not an integral value, unless value is too large */ |
| 615 | lua_assert(!luaV_flttointeger(k, &ik, F2Ieq) || |
| 616 | l_mathop(fabs)(r) >= l_mathop(1e6)); |
| 617 | return addk(fs, &kv, &o); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | |
| 622 | /* |
no test coverage detected