/* * @@ lua_numbertointeger converts a float number to an integer, or ** returns 0 if float is not within the range of a lua_Integer. ** (The range comparisons are tricky because of rounding. The tests ** here assume a two-complement representation, where MININTEGER always ** has an exact representation as a float; MAXINTEGER may not have one, ** and therefore its conversion to float may have an ill-defined value.) */ public static bool lua_numbertointeger(LUA_NUMBER n, out LUA_INTEGER p) { //#define lua_numbertointeger(n,p) \ // ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ // (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ // (*(p) = (LUA_INTEGER)(n), 1)) if ((n >= (LUA_NUMBER)(LUA_MININTEGER)) && (n < -(LUA_NUMBER)(LUA_MININTEGER))) { p = (LUA_INTEGER)n; return(true); } else { p = 0; return(false); } }
public static String lua_number2str(LUA_NUMBER n) { return(n.ToString(LUA_NUMBER_FMT_NET, CultureInfo.InvariantCulture)); }
//#else /* }{ */ //#error "numeric real type not defined" //#endif /* } */ public static LUA_NUMBER l_floor(LUA_NUMBER x) { return(Math.Floor(x)); }
public static int lua_number2str(out String s, LUA_NUMBER n) { s = n.ToString(LUA_NUMBER_FMT_NET, CultureInfo.InvariantCulture); return(s.Length); }
//#define l_mathop(op) op //#define lua_str2number(s,p) strtod((s), (p)) public static LUA_NUMBER lua_str2number(String s, out int p) { p = 0; if (String.IsNullOrWhiteSpace(s)) { return(0); } // TODO Make an optimised strtod implementation String st = s.TrimStart(); bool isNegative = false; p = s.Length - st.Length; if (st.StartsWith("+")) { p++; st = st.Substring(1); } else if (st.StartsWith("-")) { p++; st = st.Substring(1); isNegative = true; } if (st.StartsWith("NAN", StringComparison.OrdinalIgnoreCase)) { p += 3; return(LUA_NUMBER.NaN); } else if (st.StartsWith("INFINITY", StringComparison.OrdinalIgnoreCase)) { p += 8; return(isNegative ? LUA_NUMBER.NegativeInfinity : LUA_NUMBER.PositiveInfinity); } else if (st.StartsWith("INF", StringComparison.OrdinalIgnoreCase)) { p += 3; return(isNegative ? LUA_NUMBER.NegativeInfinity : LUA_NUMBER.PositiveInfinity); } else if (st.StartsWith("0X", StringComparison.OrdinalIgnoreCase)) { p += 2; st = st.Substring(1); LUA_NUMBER r = 0; for (int i = 0; i < st.Length; i++) { Int64 i64; if (Int64.TryParse(st.Substring(0, i), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out i64)) { r = i64; } else { p += i; return(r); } } p += st.Length; return(r); } else { LUA_NUMBER r = 0; for (int i = 0; i < st.Length; i++) { Double dd; if (Double.TryParse(st.Substring(0, i), NumberStyles.Any, CultureInfo.InvariantCulture, out dd)) { r = dd; } else { p += i; return(r); } } p += st.Length; return(r); } }