Lua中如何为无符号整数完美实现取余运算符?
- 内容介绍
- 文章标签
- 相关推荐
本文共计214个文字,预计阅读时间需要1分钟。
如何使用Lua的运算符或函数实现无符号整数?我想使用类型转换来动态移动,但精度是一个问题。
luafunction ModU(a, b) if b==0 then return a end if a==0 then return 0 end if a <0 then a=a + b end return a % bend
如何为lua的无符号整数实现%运算符或函数?我想过使用类型转换来浮动,但精度是一个问题.
function ModU(a, b) if b == 0 then return a end if a == 0 then return 0 end if b < 0 then if a < 0 then if a < b then return b-a else return a end else return a end else if a > 0 then return math.tointeger(a % b) else i = ModU(x & 0x7fffffff + 1, b) j = ModU(2^31 - 1, b) return ModU(i + j, b) end end end
function unsigned_mod(x, y) return y<=x and x<0 and x-y or y<0 and x or ((x>>1)%y*2+(x&1)-y)%y end
本文共计214个文字,预计阅读时间需要1分钟。
如何使用Lua的运算符或函数实现无符号整数?我想使用类型转换来动态移动,但精度是一个问题。
luafunction ModU(a, b) if b==0 then return a end if a==0 then return 0 end if a <0 then a=a + b end return a % bend
如何为lua的无符号整数实现%运算符或函数?我想过使用类型转换来浮动,但精度是一个问题.
function ModU(a, b) if b == 0 then return a end if a == 0 then return 0 end if b < 0 then if a < 0 then if a < b then return b-a else return a end else return a end else if a > 0 then return math.tointeger(a % b) else i = ModU(x & 0x7fffffff + 1, b) j = ModU(2^31 - 1, b) return ModU(i + j, b) end end end
function unsigned_mod(x, y) return y<=x and x<0 and x-y or y<0 and x or ((x>>1)%y*2+(x&1)-y)%y end

