源码已经更新在CSDN的码库里:
git clone https://gitcode.com/funsion/CLua.git
在src文件夹下的lmathlib.c 数学库 函数,Standard mathematical library:表明这个C源文件实现了Lua的标准数学库(Standard mathematical library),即提供了与数学相关的API和功能实现。
增加中文版mathlib数学函数名列表,保留英文版mathlib数学函数名列表。
原始的代码为:
static const luaL_Reg mathlib[] = {
{"abs", math_abs},
{"acos", math_acos},
{"asin", math_asin},
{"atan", math_atan},
{"ceil", math_ceil},
{"cos", math_cos},
{"deg", math_deg},
{"exp", math_exp},
{"tointeger", math_toint},
{"floor", math_floor},
{"fmod", math_fmod},
{"ult", math_ult},
{"log", math_log},
{"max", math_max},
{"min", math_min},
{"modf", math_modf},
{"rad", math_rad},
{"sin", math_sin},
{"sqrt", math_sqrt},
{"tan", math_tan},
{"type", math_type},
#if defined(LUA_COMPAT_MATHLIB)
{"atan2", math_atan},
{"cosh", math_cosh},
{"sinh", math_sinh},
{"tanh", math_tanh},
{"pow", math_pow},
{"frexp", math_frexp},
{"ldexp", math_ldexp},
{"log10", math_log10},
#endif
/* placeholders */
{"random", NULL},
{"randomseed", NULL},
{"pi", NULL},
{"huge", NULL},
{"maxinteger", NULL},
{"mininteger", NULL},
{NULL, NULL}
};
更改成以下代码:
static const luaL_Reg mathlib[] = {
{"abs", math_abs}, {"绝对值", math_abs},
{"acos", math_acos}, {"反余弦", math_acos},
{"asin", math_asin}, {"反正弦", math_asin},
{"atan", math_atan}, {"反正切", math_atan},
{"ceil", math_ceil}, {"向上取整", math_ceil},
{"cos", math_cos}, {"余弦", math_cos},
{"deg", math_deg}, {"转角度", math_deg},
{"exp", math_exp}, {"指数", math_exp},
{"tointeger", math_toint}, {"转整数", math_toint},
{"floor", math_floor}, {"向下取整", math_floor},
{"fmod", math_fmod}, {"取模", math_fmod},
{"ult", math_ult}, {"无符号小于", math_ult},
{"log", math_log}, {"对数", math_log},
{"max", math_max}, {"最大值", math_max},
{"min", math_min}, {"最小值", math_min},
{"modf", math_modf}, {"分离小数", math_modf},
{"rad", math_rad}, {"转弧度", math_rad},
{"sin", math_sin}, {"正弦", math_sin},
{"sqrt", math_sqrt}, {"平方根", math_sqrt},
{"tan", math_tan}, {"正切", math_tan},
{"type", math_type}, {"数值类型", math_type},
#if defined(LUA_COMPAT_MATHLIB)
{"atan2", math_atan}, {"双参反正切", math_atan},
{"cosh", math_cosh}, {"双曲余弦", math_cosh},
{"sinh", math_sinh}, {"双曲正弦", math_sinh},
{"tanh", math_tanh}, {"双曲正切", math_tanh},
{"pow", math_pow}, {"幂运算", math_pow},
{"frexp", math_frexp}, {"分解浮点数", math_frexp},
{"ldexp", math_ldexp}, {"组合浮点数", math_ldexp},
{"log10", math_log10}, {"常用对数", math_log10},
#endif
/* 占位符 */
{"random", NULL}, {"随机数", NULL},
{"randomseed", NULL}, {"随机种子", NULL},
{"pi", NULL}, {"圆周率", NULL},
{"huge", NULL}, {"无穷大", NULL},
{"maxinteger", NULL}, {"最大整数", NULL},
{"mininteger", NULL}, {"最小整数", NULL},
{NULL, NULL}
};
处理占位符,实现PI等值。
LUAMOD_API int luaopen_math (lua_State *L) {
luaL_newlib(L, mathlib);
lua_pushnumber(L, PI);
lua_setfield(L, -2, "pi");
lua_pushnumber(L, PI);
lua_setfield(L, -2, "圆周率");
lua_pushnumber(L, (lua_Number)HUGE_VAL);
lua_setfield(L, -2, "huge");
lua_pushnumber(L, (lua_Number)HUGE_VAL);
lua_setfield(L, -2, "无穷大");
lua_pushinteger(L, LUA_MAXINTEGER);
lua_setfield(L, -2, "maxinteger");
lua_pushinteger(L, LUA_MAXINTEGER);
lua_setfield(L, -2, "最大整数");
lua_pushinteger(L, LUA_MININTEGER);
lua_setfield(L, -2, "mininteger");
lua_pushinteger(L, LUA_MININTEGER);
lua_setfield(L, -2, "最小整数");
setrandfunc(L);
return 1;
}
static const luaL_Reg randfuncs[] = {
{"random", math_random}, {"随机数", math_random},
{"randomseed", math_randomseed}, {"随机种子", math_randomseed},
{NULL, NULL}
};
为了保证中英文协程函数都可以加载,以便你可以复制英文原码来进行更改。所以保留了英文版协程函数名列表,这样就能使用两种文的函数。
{"pow", math_pow}, // 计算一个数的另一个数次幂
{"幂", math_pow}, // 和pow相同,但是使用中文名称
其实它们都是加载同样的库名,算是加载了2次,以Lua内部算法,应该只会加载一次。
更改完之后,同样需要重新编译Lua的源码,实现以上列出的关键词的中文化。
注意,在Window系统下编译Lua, 最好将所有Lua的源码,重新保存成ANSI格式的文件,刚下载的默认的源码会是UTF-8格式的。
这个事情说三遍,
1,不然就会出现,Window下的UTF-8源码可编译,但Shell里的中文输出会乱码。
2,要不然就是Window的ANSI源码不可编译(假如你没做以上步骤),
3,如果是用ANSI格式的源码编译的Lua.exe,对应的,你在Window下写的Lua程序也是需要保存成ANSI格式的。这样就可以在Shell里输出正确的中文显示。
验证lua例程:
-- 测试数学库函数(中英文关键词)
-- 测试绝对值函数
print("绝对值 (abs):", math.绝对值(-10)) -- 10
print("abs:", math.abs(-10)) -- 10
-- 测试三角函数
print("余弦 (cos):", math.余弦(math.pi / 3)) -- 0.5
print("cos:", math.cos(math.pi / 3)) -- 0.5
print("正弦 (sin):", math.正弦(math.pi / 6)) -- 0.5
print("sin:", math.sin(math.pi / 6)) -- 0.5
print("正切 (tan):", math.正切(math.pi / 4)) -- 1.0
print("tan:", math.tan(math.pi / 4)) -- 1.0
-- 测试反三角函数
print("反余弦 (acos):", math.反余弦(0.5)) -- 1.0471975511966 (约等于 pi/3)
print("acos:", math.acos(0.5)) -- 1.0471975511966
print("反正弦 (asin):", math.反正弦(0.5)) -- 0.5235987755983 (约等于 pi/6)
print("asin:", math.asin(0.5)) -- 0.5235987755983
print("反正切 (atan):", math.反正切(1)) -- 0.78539816339745 (约等于 pi/4)
print("atan:", math.atan(1)) -- 0.78539816339745
-- 测试取整函数
print("向上取整 (ceil):", math.向上取整(3.2)) -- 4
print("ceil:", math.ceil(3.2)) -- 4
print("向下取整 (floor):", math.向下取整(3.8)) -- 3
print("floor:", math.floor(3.8)) -- 3
-- 测试指数和对数函数
print("指数 (exp):", math.指数(1)) -- 2.718281828459 (e^1)
print("exp:", math.exp(1)) -- 2.718281828459
print("对数 (log):", math.对数(math.exp(1))) -- 1.0 (ln(e))
print("log:", math.log(math.exp(1))) -- 1.0
-- 测试最大值和最小值
print("最大值 (max):", math.最大值(10, 20, 30)) -- 30
print("max:", math.max(10, 20, 30)) -- 30
print("最小值 (min):", math.最小值(10, 20, 30)) -- 10
print("min:", math.min(10, 20, 30)) -- 10
-- 测试取模函数
print("取模 (fmod):", math.取模(10.5, 3)) -- 1.5
print("fmod:", math.fmod(10.5, 3)) -- 1.5
-- 测试转整数函数
print("转整数 (tointeger):", math.转整数(3.14)) -- 3
print("tointeger:", math.tointeger(3.14)) -- 3
-- 测试分离小数函数
local int, frac = math.分离小数(3.14)
print("分离小数 (modf):", int, frac) -- 3, 0.14
int, frac = math.modf(3.14)
print("modf:", int, frac) -- 3, 0.14
-- 测试平方根函数
print("平方根 (sqrt):", math.平方根(16)) -- 4.0
print("sqrt:", math.sqrt(16)) -- 4.0
-- 测试数值类型函数
print("数值类型 (type):", math.数值类型(3.14)) -- "float"
print("type:", math.type(3.14)) -- "float"
-- 测试转角度和转弧度函数
print("转角度 (deg):", math.转角度(math.pi)) -- 180.0
print("deg:", math.deg(math.pi)) -- 180.0
print("转弧度 (rad):", math.转弧度(180)) -- 3.1415926535898 (pi)
print("rad:", math.rad(180)) -- 3.1415926535898
-- 测试无符号小于函数
print("无符号小于 (ult):", math.无符号小于(10, 20)) -- true
print("ult:", math.ult(10, 20)) -- true
-- 测试 math 库中的常量
print("圆周率:", math.pi) --3.1415926535898
print("无穷大:", math.huge) --inf
print("最大整数:", math.maxinteger) --9223372036854775807
print("最小整数:", math.mininteger) -- -9223372036854775808
-- 测试 math 库中的函数
print("随机数:", math.random()) --0.47306729478209
print("随机数 (0-10):", math.random(10)) --8
print("随机数 (5-15):", math.random(5, 15)) --15
-- 设置随机种子并再次测试随机数
math.randomseed(123)
print("随机数 (种子为 123):", math.random()) --0.21405899041481
print("随机数 (0-10, 种子为 123):", math.random(10)) --9
print("随机数 (5-15, 种子为 123):", math.random(5, 15)) --13
-- 测试 math 库中的常量
print("圆周率:", 数学.圆周率)
print("无穷大:", 数学.无穷大)
print("最大整数:", 数学.最大整数)
print("最小整数:", 数学.最小整数)
-- 测试 数学 库中的函数
print("随机数:", 数学.随机数())
print("随机数 (0-10):", 数学.随机数(10))
print("随机数 (5-15):", 数学.随机数(5, 15))
-- 设置随机种子并再次测试随机数
数学.随机种子(123)
print("随机数 (种子为 123):", 数学.随机数())
print("随机数 (0-10, 种子为 123):", 数学.随机数(10))
print("随机数 (5-15, 种子为 123):", 数学.随机数(5, 15))
圆周率: 3.1415926535898
无穷大: inf
最大整数: 9223372036854775807
最小整数: -9223372036854775808
随机数: 0.61086139444347
随机数 (0-10): 8
随机数 (5-15): 8
随机数 (种子为 123): 0.21405899041481
随机数 (0-10, 种子为 123): 9
随机数 (5-15, 种子为 123): 13
圆周率: 3.1415926535898
无穷大: inf
最大整数: 9223372036854775807
最小整数: -9223372036854775808
随机数: 0.61086139444347
随机数 (0-10): 8
随机数 (5-15): 8
随机数 (种子为 123): 0.21405899041481
随机数 (0-10, 种子为 123): 9
随机数 (5-15, 种子为 123): 13

&spm=1001.2101.3001.5002&articleId=136793308&d=1&t=3&u=9767fb5157fd4532bcf5b1fc95579e7a)
2643

被折叠的 条评论
为什么被折叠?



