Skip to content

Commit 368b2c3

Browse files
committed
patch 27/12/2025
+ rewrite `master.custom._cfloor` + fix issue on `master.mul` + fix issue on `media.sqrt`
1 parent 51e386f commit 368b2c3

File tree

2 files changed

+55
-43
lines changed

2 files changed

+55
-43
lines changed

int.lua

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
-- ULTIMATE INT --
33
----------------------------------------------------
44
-- MODULE VERSION: 186
5-
-- BUILD VERSION: 186.6 (15/12/2025) dd:mm:yyyy
5+
-- BUILD VERSION: 186.6 (27/12/2025) dd:mm:yyyy
66
-- USER FEATURE: 26/11/2025
7-
-- DEV FEATURE: 26/11/2025
7+
-- DEV FEATURE: 27/12/2025
88
-- AUTHOR: SupTan85
99
-- LICENSE: MIT (the same license as Lua itself)
1010
-- LINK: https://github.com/SupTan85/int.lua
@@ -178,38 +178,48 @@ end
178178

179179
local masterC, masterD = master.convert, master.deconvert
180180
master.custom = {
181-
_cfloor = function(x, length, resultonly)
182-
local rev, dlen, prlen, s = length < 0, x._dlen or 1, nil, x._size or 1
183-
length = rev and math.abs(((dlen - 1) * s) + (s - #(tostring(x[dlen]):match("^(%d-)0*$") or ""))) + length or length
184-
local endp
185-
if rev or ceil(-length / s) > dlen - 1 then
186-
endp = ceil(-length / s)
187-
for i = dlen, min(endp, 0) do
188-
if i == endp then
189-
local shift = tostring(x[i]):sub(1, length % s)
190-
local hofu = tonumber(shift..("0"):rep(s - #shift))
191-
prlen = prlen or #(tostring(x[i]):match("^(%d-)0*$") or "") - (length % s)
192-
x[i] = hofu ~= 0 and hofu
193-
if not x[i] then
194-
endp = endp + 1
195-
for i = endp, 0 do
196-
if x[i] == 0 then
197-
endp, x[i] = endp + 1, nil
198-
else
199-
break
200-
end
201-
end
202-
end
203-
else
181+
_cfloor = function(x, length, resultonly, copy)
182+
assert(type(length) == "number", ("[CFLOOR] INVALID_INPUT_TYPE | length: number (not %s)"):format(type(length)))
183+
if length < 0 or length % 1 ~= 0 then
184+
warn(("[CFLOOR] INVALID_INPUT | length: integer <positive> (current %s to %s)"):format(length, ceil(math.abs(length))))
185+
length = ceil(math.abs(length))
186+
end
187+
local s, dlen, prlen = x._size or 1, x._dlen or 1, nil
188+
local result = copy and {_size = s} or x
189+
local new_dlen = floor(-length / s) + 1
190+
if new_dlen >= dlen then
191+
if copy then
192+
for i = new_dlen, #x do
193+
result[i] = x[i]
194+
end
195+
else
196+
for i = dlen, new_dlen - 1 do
204197
x[i] = nil
205198
end
206199
end
207-
x._dlen = endp
200+
local rem_space = length % s
201+
local shift = s - rem_space
202+
if not resultonly then
203+
local f, b = tostring(result[new_dlen]):find("0*$")
204+
prlen = shift - (b - f + 1)
205+
end
206+
if rem_space > 0 then
207+
local new_value = floor(result[new_dlen] * (10 ^ -shift)) * floor(10 ^ shift)
208+
if new_value == 0 then
209+
result[new_dlen] = nil
210+
new_dlen = new_dlen + 1
211+
else
212+
result[new_dlen] = new_value
213+
end
214+
end
215+
else
216+
new_dlen = dlen
208217
end
218+
result._dlen = new_dlen
209219
if resultonly then
210-
return x
220+
return result
211221
end
212-
return x, endp or dlen, prlen
222+
return result, new_dlen or dlen, prlen
213223
end,
214224

215225
_refresh = function(x, lu, endp) -- refresh all chunk that should to be, by fast `ADD` process.
@@ -564,7 +574,7 @@ master.calculate = {
564574
end
565575
end
566576
for i = bottom, 0 do
567-
if result[i] ~= 0 then
577+
if (result[i] or 0) ~= 0 then
568578
result._dlen = i
569579
break
570580
else
@@ -582,7 +592,8 @@ master.calculate = {
582592
local one = masterC(1, s)
583593
local accuracy, uc = 0, 0
584594
local lastpoint, fin, mark
585-
b = self:mul(b, masterC("1"..("0"):rep(math.abs(b_dlen - 1)), b._size))
595+
local shift_mul = b_dlen < 1 and masterC("1"..("0"):rep((math.abs(min(b_dlen, 0)) * s) + #tostring(b[b_dlen])), s)
596+
b = shift_mul and self:mul(b, shift_mul) or b
586597
local d = OPTION.MASTER_CALCULATE_DIV_BYPASS_GEN_FLOATING_POINT and (function(b)
587598
local p = b == "1" and "1" or tostring("1" / b)
588599
if p:find("e") then
@@ -699,7 +710,7 @@ master.calculate = {
699710
local dv, lp = calcu(lastpoint)
700711
-- issue checker >>
701712
if not lp and master._config.OPTION.MASTER_CALCULATE_DIV_BYPASS_GEN_FLOATING_POINT then
702-
io.write(("\n[DIV] VALIDATION_FAILED | issues detected in division function, main process is unable to find the correct result.\n\tFUNCTION LOG >>\nprocess: (%s / %s)\nraw_data: %s\n"):format((a and masterD(a)) or "ERROR", (b and masterD(b)) or "ERROR", (d and masterD(d)) or "ERROR"))
713+
io.write(("\n[DIV] VALIDATION_FAILED | issues detected in division function, main process is unable to find the correct result.\n\tFUNCTION LOG >>\nprocess: (%s / %s)\nraw_data: %s\n\n"):format((a and masterD(a)) or "ERROR", (b and masterD(b)) or "ERROR", (d and masterD(d)) or "ERROR"))
703714
io.write("\n[DIV] VALIDATION_FAILED | issues detected in division function, main process is unable to find the correct result while using the option <MASTER_CALCULATE_DIV_BYPASS_GEN_FLOATING_POINT>.\nmodule will automatically disable this option permanent and recalculate the result again. some versions of Lua cannot using this option!\nset: master._config.OPTION.MASTER_CALCULATE_DIV_BYPASS_GEN_FLOATING_POINT = false\n")
704715
master._config.OPTION.MASTER_CALCULATE_DIV_BYPASS_GEN_FLOATING_POINT = false
705716
return master.calculate:div(a, b, s, f, l)
@@ -739,8 +750,8 @@ master.calculate = {
739750
end
740751
end
741752
--------------------
742-
if b_dlen < 1 then
743-
d = self:mul(d, masterC("1"..("0"):rep(math.abs(b_dlen - 1)), s))
753+
if shift_mul then
754+
d = self:mul(d, shift_mul)
744755
end
745756
local raw = self:mul(a, d)
746757
if f > 0 and lastpoint and -raw._dlen >= floor(f / s) then
@@ -1027,7 +1038,7 @@ function assets.vpow(self, x, y, l) -- pow function assets. `y >= 0`
10271038
return custom:cfloor(x, l)
10281039
end
10291040
local result = media.convert(1, x._size)
1030-
local exp = y
1041+
local x, exp = x, y
10311042
while master.equation.more(exp, masterC(0, x._size)) do
10321043
if (exp[1] or 0) % 2 == 1 then
10331044
result = result * x
@@ -1059,15 +1070,18 @@ function media.sqrt(x, f, l) -- Returns the Square root of `x`. (`f` The maxiumu
10591070
-- Newton's Method --
10601071
assert(tostring(x) >= "0", "[SQRT] INVALID_INPUT | Cannot compute the square root of a negative number.")
10611072
assert(not f or type(f) == "number", ("[SQRT] INVALID_INPUT_TYPE | Type of maxiumum number of decimal part should be integer (not %s)"):format(type(f)))
1062-
local res = (x + (x / x)) * 0.5
1073+
if #x >= 1 and (x[#x] or 0) == 0 and (x._dlen or 1) == 1 then
1074+
return x
1075+
end
1076+
local res = x
10631077
local TOLERANCE = f or ACCURACY_LIMIT.MEDIA_DEFAULT_SQRTROOT_TOLERANCE
1064-
for _ = 1, l or ACCURACY_LIMIT.MEDIA_DEFAULT_SQRTROOT_MAXITERATIONS do
1065-
local nes = (res + (x / res)) * 0.5
1066-
local dl, tl = media.vtype(media.decimallen(nes - res), TOLERANCE)
1067-
if dl >= tl then
1068-
return custom:cround(nes, TOLERANCE)
1078+
for _ = 1, max(l or ACCURACY_LIMIT.MEDIA_DEFAULT_SQRTROOT_MAXITERATIONS, 1) do
1079+
local next_res = (res + custom:cround(x / res, max(0, TOLERANCE - 1))) * 0.5
1080+
local ave = next_res - res
1081+
if #ave <= 1 and (ave[1] or 0) == 0 and media.decimallen(ave) >= TOLERANCE then
1082+
return custom:cround(next_res, TOLERANCE)
10691083
end
1070-
res = nes
1084+
res = next_res
10711085
end
10721086
return custom:cround(res, TOLERANCE)
10731087
end

testsuite.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,8 @@ if not suite_select or suite_select == 0 then
128128
local x, y = int.new(x, y)
129129
return int.fmod(x, y)
130130
end)
131-
print("[POW] simple value: limit mode")
132131
benchmark("pow", function(x, y)
133132
local x, y = int.new(x, y)
134-
x, y = int.new(tostring(x):sub(1, 4), tostring(y):sub(1, 4))
135133
return x:pow(x.sign == "+" and y or y:floor())
136134
end)
137135
benchmark("sqrt", function(x, _)

0 commit comments

Comments
 (0)