Lua math(三) tool

前言#

今天这个系列的函数我将其命名为工具,之所以这么命名是因为这个系列函数没有复杂的公式,仅仅是取数字的一部分,或者是把数字变个形式等等,但有这些函数是很重要的,其中有很多函数是我们经常用到的,比如求绝对值、向下取整、求余运算等等。

内容#


math.huge##

  • 原型:math.huge
  • 解释:返回HUGE_VAL的值,是一个大于任何数字的值。

math.abs()##

  • 原型:math.abs(x)
  • 解释:返回一个数x的绝对值。

math.ceil()##

  • 原型:math.ceil(x)
  • 解释:返回一个大于等于x的最小的一个数,即向上取整。

math.floor()##

  • 原型:math.floor(x)
  • 解释:返回一个小于等于x的最大的一个数,即向下取整。

math.fmod()##

  • 原型:math.fmod(x, y)
  • 解释:返回x%y的值,即想除以y的余数。

math.modf()##

  • 原型:math.modf(x)
  • 解释:返回数x的整数部分和小数部分。

math.max()##

  • 原型:math.max(x, ...)
  • 解释:返回所有参数中最大的一个值。

math.min()##

  • 原型:math.min(x, ...)
  • 解释:返回所有参数中最小的一个值。

math.randomseed()##

  • 原型:math.randomseed(x)
  • 解释:将x 设置为伪随机数生成器的种子,相同的随机数生成器生成相同的数列。

math.random()##

  • 原型:math.random([m [, n]])
  • 解释:这个函数就是提供了一个使用ANSI C函数rand的一个接口,可以生成一个伪随机数。
  • 当不传任何参数时函数返回一个[0,1)范围内的真实数字。
  • 当只传入数字m时,函数返回一个范围[1,m]内的伪随机整数。
  • 当传入两个数字mn时,函数返回范围[m,n]内的一个伪随机整数。

Uasge##

  • 首先新建一个文件然后命名为toolfunctest.lua编写如下代码:
-- 数字最大值
print("\nmath.huge = "..math.huge)
if 99999999 < math.huge then
    print("math.huge test")
end

-- 绝对值
local x = 3
print("\nmath.abs("..x..") = "..math.abs(x))
x = -3
print("math.abs("..x..") = "..math.abs(x))

-- 向上取整
x = 3.1
print("\nmath.ceil("..x..") = "..math.ceil(x))

-- 向下取整
x = 3.9
print("\nmath.floor("..x..") = "..math.floor(x))

-- 求余数
x = 3
local y = 5
print("\nmath.fmod("..x..", "..y..") = "..math.fmod(x, y))
y = -5
print("math.fmod("..x..", "..y..") = "..math.fmod(x, y))
x = -3
y = 5
print("math.fmod("..x..", "..y..") = "..math.fmod(x, y))

-- 取整数和小数
x = 6.7
local zs, xs = math.modf(x)
print("\nmath.modf("..x..") = "..zs..", "..xs)

-- 最大值
x = 3; y = 10; z = 99
print("\nmath.max("..x..", "..y..", "..z..") = "..math.max(x, y, z))

-- 最小值
x = 3; y =-3; z= 32
print("\nmath.min("..x..", "..y..", "..z..") = "..math.min(x, y, z))

-- 随机数
local m = 8;
local n = 100;
print("\nmath.random() = "..math.random())
print("\nmath.random("..m..") = "..math.random(m))
print("\nmath.random("..m..", "..n..") = "..math.random(m, n))

m = 9999;
math.randomseed(100)
print("\nmath.randomseed(100)")
print("math.random("..m..") = "..math.random(m))
print("math.random("..m..") = "..math.random(m))
print("math.random("..m..") = "..math.random(m))

math.randomseed(100)
print("\nmath.randomseed(100)")
print("math.random("..m..") = "..math.random(m))
print("math.random("..m..") = "..math.random(m))
print("math.random("..m..") = "..math.random(m))

math.randomseed(1000)
print("\nmath.randomseed(1000)")
print("math.random("..m..") = "..math.random(m))
print("math.random("..m..") = "..math.random(m))
print("math.random("..m..") = "..math.random(m))
  • 运行结果

math_tool.png

总结#

  • 注意math.huge的使用,这是一个值而不是一个函数,所以使用的时候千万不要加括号。
  • 我们可以重点分析一下产生随机数的这个函数,从函数的定义中就可以看出,这个“随机数”并不是真正的随机,而是一个和时间种子相关的伪随机序列。
  • 当我们的随机种子相同时就可以产生相同的,这一点从运行结果中也可以看到,而当改变这个随机种子时,对应的序列也会改变,所以我们在日常的应用中常使用时间戳(从1970年1月1日至今的秒数)作为随机种子来尽可能的保证函数产生的序列不相同。



作者:AlbertS
链接:https://www.jianshu.com/p/00881b2712f7
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值