本文整理汇总了C++中cast_num函数的典型用法代码示例。如果您正苦于以下问题:C++ cast_num函数的具体用法?C++ cast_num怎么用?C++ cast_num使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了cast_num函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: readhexa
static lua_Number readhexa (const char **s, lua_Number r, int *count) {
for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */
r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s)));
(*count)++;
}
return r;
}
示例2: luaO_str2d
int luaO_str2d (const char *s, lua_Number *result) {
char *endptr;
*result = lua_str2number(s, &endptr);
if (endptr == s) return 0; /* conversion failed */
if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
#if defined(LUA_CROSS_COMPILER)
{
long lres = strtoul(s, &endptr, 16);
#if LONG_MAX != 2147483647L
if (lres & ~0xffffffffL)
*result = cast_num(-1);
else if (lres & 0x80000000L)
*result = cast_num(lres | ~0x7fffffffL);
else
#endif
*result = cast_num(lres);
}
#else
*result = cast_num(c_strtoul(s, &endptr, 16));
#endif
if (*endptr == '\0') return 1; /* most common case */
while (isspace(cast(unsigned char, *endptr))) endptr++;
if (*endptr != '\0') return 0; /* invalid trailing characters? */
return 1;
}
示例3: luaV_numtointeger
/*
** Check whether a float number is within the range of a lua_Integer.
** (The comparisons are tricky because of rounding, which can or
** not occur depending on the relative sizes of floats and integers.)
** This function is called only when 'n' has an integer value.
*/
int luaV_numtointeger (lua_Number n, lua_Integer *p) {
if (cast_num(MIN_INTEGER) <= n && n < (MAX_INTEGER + cast_num(1))) {
*p = cast_integer(n);
lua_assert(cast_num(*p) == n);
return 1;
}
return 0; /* number is outside integer limits */
}
示例4: LEintfloat
/*
** Check whether integer 'i' is less than or equal to float 'f'.
** See comments on previous function.
*/
static int LEintfloat (lua_Integer i, lua_Number f) {
#if defined(l_intfitsf)
if (!l_intfitsf(i)) {
if (f >= -cast_num(LUA_MININTEGER)) /* -minint == maxint + 1 */
return 1; /* f >= maxint + 1 > i */
else if (f >= cast_num(LUA_MININTEGER)) /* minint <= f <= maxint ? */
return (i <= cast(lua_Integer, f)); /* compare them as integers */
else /* f < minint <= i (or 'f' is NaN) --> not(i <= f) */
return 0;
}
#endif
return luai_numle(cast_num(i), f); /* compare them as floats */
}
示例5: l_hashfloat
static int l_hashfloat (lua_Number n) {
int i;
lua_Integer ni;
n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */
lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL));
return 0;
}
else { /* normal case */
unsigned int u = cast_uint(i) + cast_uint(ni);
return cast_int(u <= cast_uint(INT_MAX) ? u : ~u);
}
}
示例6: addk
static int addk (FuncState *fs, TValue *key, TValue *v) {
lua_State *L = fs->ls->L;
TValue *idx = luaH_set(L, fs->h, key);
Proto *f = fs->f;
int k, oldsize;
if (ttisnumber(idx)) {
lua_Number n = nvalue(idx);
lua_number2int(k, n);
if (luaV_rawequalobj(&f->k[k], v))
return k;
/* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0");
go through and create a new entry for this value */
}
/* constant not found; create a new entry */
oldsize = f->sizek;
k = fs->nk;
/* numerical value does not need GC barrier;
table has no metatable, so it does not need to invalidate cache */
setnvalue(idx, cast_num(k));
luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
setobj(L, &f->k[k], v);
fs->nk++;
luaC_barrier(L, f, v);
return k;
}
示例7: luaB_tonumber
static int luaB_tonumber (lua_State *L) {
int base = luaL_optint(L, 2, 10);
if (base == 10) { /* standard conversion */
luaL_checkany(L, 1);
if (lua_isnumber(L, 1)) { /* numeric string, or a number */
#ifdef LUA_TINT
lua_pushvalue_as_number(L,1); /* API extension (not to lose accuracy here) */
#else
lua_pushnumber(L, lua_tonumber(L, 1));
#endif
return 1;
}
}
else {
const char *s1 = luaL_checkstring(L, 1);
char *s2;
unsigned LUA_INTEGER n;
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
n = lua_str2ul(s1, &s2, base);
if (s1 != s2) { /* at least one valid digit? */
while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
if (*s2 == '\0') { /* no invalid trailing characters? */
/* Push as number, there needs to be separate 'luaB_tointeger' for
* when the caller wants to preserve the bits (matters if unsigned
* values are used).
*/
lua_pushnumber(L, cast_num(n));
return 1;
}
}
}
lua_pushnil(L); /* else not a number */
return 1;
}
示例8: luaO_str2d
int luaO_str2d(const char* s, lua_Number* result)
{
char* endptr;
*result = lua_str2number(s, &endptr);
if (endptr == s) return 0; /* conversion failed */
if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
*result = cast_num(strtoul(s, &endptr, 16));
if ((*endptr == 'o' || *endptr == 'O') && (*(endptr + 1) != '\0'))
*result = cast_num(strtoul(endptr + 1, &endptr, 8));
if ((*endptr == 'b' || *endptr == 'B') && (*(endptr + 1) != '\0'))
*result = cast_num(strtoul(endptr + 1, &endptr, 2));
if (*endptr == '\0') return 1; /* most common case */
while (isspace(cast(unsigned char, *endptr))) endptr++;
if (*endptr != '\0') return 0; /* invalid trailing characters? */
return 1;
}
示例9: addk
static int addk (FuncState *fs, TValue *k, TValue *v) {
lua_State *L = fs->L;
TValue *idx = luaH_set(L, fs->h, k);
Proto *f = fs->f;
int oldsize = f->sizek;
if (ttisnumber(idx)) {
lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v));
return cast_int(nvalue(idx));
}
else { /* constant not found; create a new entry */
setnvalue(idx, cast_num(fs->nk));
#if LUA_MEMORY_STATS
luaM_setname(L, "lua.parser.constants");
#endif /* LUA_MEMORY_STATS */
luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,
MAXARG_Bx, "constant table overflow");
#if LUA_MEMORY_STATS
luaM_setname(L, 0);
#endif /* LUA_MEMORY_STATS */
#if LUA_REFCOUNT
while (oldsize < f->sizek) setnilvalue2n(L, &f->k[oldsize++]);
#else
while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
#endif /* LUA_REFCOUNT */
setobj(L, &f->k[fs->nk], v);
luaC_barrier(L, f, v);
return fs->nk++;
}
}
示例10: pushstr
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
int n = 1;
pushstr(L, "");
for (;;) {
const char *e = strchr(fmt, '%');
if (e == NULL) break;
setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
incr_top(L);
switch (*(e+1)) {
case 's': {
const char *s = va_arg(argp, char *);
if (s == NULL) s = "(null)";
pushstr(L, s);
break;
}
case 'c': {
char buff[2];
buff[0] = cast(char, va_arg(argp, int));
buff[1] = '\0';
pushstr(L, buff);
break;
}
case 'd': {
setnvalue(L->top, cast_num(va_arg(argp, int)));
incr_top(L);
break;
}
case 'f': {
setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
incr_top(L);
break;
}
case 'p': {
char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
sprintf(buff, "%p", va_arg(argp, void *));
pushstr(L, buff);
break;
}
case '%': {
pushstr(L, "%");
break;
}
default: {
char buff[3];
buff[0] = '%';
buff[1] = *(e+1);
buff[2] = '\0';
pushstr(L, buff);
break;
}
}
n += 2;
fmt = e+2;
}
pushstr(L, fmt);
luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
L->top -= n;
return svalue(L->top - 1);
}
示例11: luaV_lessthan
int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
int res;
int tl= ttype(l);
if (tl == ttype(r)) {
switch(tl) {
#ifdef LUA_TINT
case LUA_TINT:
return ivalue(l) < ivalue(r);
#endif
case LUA_TNUMBER:
#ifdef LNUM_COMPLEX
if ( (nvalue_img_fast(l)!=0) || (nvalue_img_fast(r)!=0) )
error_complex( L, l, r );
#endif
return luai_numlt(nvalue_fast(l), nvalue_fast(r));
case LUA_TSTRING:
return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
}
if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
return res;
/* fall through to 'luaG_ordererror()' */
}
#ifdef LUA_TINT
else if (ttype_ext(l) == ttype_ext(r)) {
lua_Integer tmp;
/* Avoid accuracy losing casts: if 'r' is integer by value, do comparisons
* in integer realm. Only otherwise cast 'l' to FP (which might change its
* value).
*/
# ifdef LNUM_COMPLEX
if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
error_complex( L, l, r );
# endif
if (tl==LUA_TINT) { /* l:int, r:num */
return tt_integer_valued(r,&tmp) ? (ivalue(l) < tmp)
: luai_numlt( cast_num(ivalue(l)), nvalue_fast(r) );
} else { /* l:num, r:int */
return tt_integer_valued(l,&tmp) ? (tmp < ivalue(r))
: luai_numlt( nvalue_fast(l), cast_num(ivalue(r)) );
}
}
#endif
return luaG_ordererror(L, l, r);
}
示例12: luaV_tonumber_
int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
lua_assert(!ttisfloat(obj));
if (ttisinteger(obj)) {
*n = cast_num(ivalue(obj));
return 1;
}
else
return (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, n));
}
示例13: lessequal
static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
int res;
int tl= ttype(l);
if (tl == ttype(r)) {
switch(tl) {
#ifdef LUA_TINT
case LUA_TINT:
return ivalue(l) <= ivalue(r);
#endif
case LUA_TNUMBER:
#ifdef LNUM_COMPLEX
if ( (nvalue_img_fast(l)!=0) || (nvalue_img_fast(r)!=0) )
error_complex( L, l, r );
#endif
return luai_numle(nvalue_fast(l), nvalue_fast(r));
case LUA_TSTRING:
return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
}
if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
return res;
else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
return !res;
/* fall through to 'luaG_ordererror()' */
}
#ifdef LUA_TINT
else if (ttype_ext(l) == ttype_ext(r)) {
lua_Integer tmp;
# ifdef LNUM_COMPLEX
if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
error_complex( L, l, r );
# endif
if (tl==LUA_TINT) { /* l:int, r:num */
return tt_integer_valued(r,&tmp) ? (ivalue(l) <= tmp)
: luai_numle( cast_num(ivalue(l)), nvalue_fast(r) );
} else { /* l:num, r:int */
return tt_integer_valued(l,&tmp) ? (tmp <= ivalue(r))
: luai_numle( nvalue_fast(l), cast_num(ivalue(r)) );
}
}
#endif
return luaG_ordererror(L, l, r);
}
示例14: arrayindex
/*
** returns the index for `key' if `key' is an appropriate key to live in
** the array part of the table, -1 otherwise.
*/
static int arrayindex (const TValue *key) {
if (ttisnumber(key)) {
lv_Number n = nvalue(key);
int k;
lv_number2int(k, n);
if (lvi_numeq(cast_num(k), n))
return k;
}
return -1; /* `key' did not match some condition */
}
示例15: tofloat
/*
** Similar to 'tonumber', but does not attempt to convert strings and
** ensure correct precision (no extra bits). Used in comparisons.
*/
static int tofloat (const TValue *obj, lua_Number *n) {
if (ttisfloat(obj)) *n = fltvalue(obj);
else if (ttisinteger(obj)) {
volatile lua_Number x = cast_num(ivalue(obj)); /* avoid extra precision */
*n = x;
}
else {
*n = 0; /* to avoid warnings */
return 0;
}
return 1;
}