Skip to content

Commit 33187be

Browse files
authored
Merge pull request #7 from SupTan85/186-6-scm
Update module version 186-6
2 parents 80d8547 + c57937b commit 33187be

File tree

3 files changed

+81
-35
lines changed

3 files changed

+81
-35
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# vscode config
22
.vscode/*
3-
43
!.vscode/settings.json
54

5+
# workspace folder
6+
/.test
7+
68
# other
79
.lh
810
*.out

int.lua

Lines changed: 70 additions & 33 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.5 (16/11/2025) dd:mm:yyyy
6-
-- USER FEATURE: 08/11/2025
7-
-- DEV FEATURE: 08/11/2025
5+
-- BUILD VERSION: 186.6 (26/11/2025) dd:mm:yyyy
6+
-- USER FEATURE: 26/11/2025
7+
-- DEV FEATURE: 26/11/2025
88
-- AUTHOR: SupTan85
99
-- LICENSE: MIT (the same license as Lua itself)
1010
-- LINK: https://github.com/SupTan85/int.lua
@@ -43,14 +43,17 @@ local master = {
4343
note: this option causes a division speed slow, but very powerful for high accuracy.
4444
4545
// DISABLE : MASTER_CALCULATE_DIV_MAXITERATIONS
46+
// DISABLE : MASTER_DEFAULT_FRACT_LIMIT_DIV
4647
By SupTan85
4748
<< BUILD-IN >>]]
4849
MASTER_CALCULATE_DIV_AUTO_CONFIG_ITERATIONS = true,
50+
MASTER_CALCULATE_DIV_AUTO_CONFIG_ITERATIONS_BUFF_MODE = true, -- use buff-accurate for more accuracy. note: very slow but high accuracy!
4951
},
5052

5153
ACCURACY_LIMIT = {
5254
-- MASTER FUNCTION CONFIG --
5355
MASTER_CALCULATE_DIV_MAXITERATIONS = 15, -- 15
56+
MASTER_CALCULATE_DIV_BUFF_ACCURATE = 2, -- 2
5457
MASTER_DEFAULT_FRACT_LIMIT_DIV = 14, -- 14
5558

5659
-- MEDIA FUNCTION CONFIG --
@@ -70,7 +73,7 @@ local master = {
7073
},
7174

7275
_VERSION = "186",
73-
_BUILD = "186.5"
76+
_BUILD = "186.6"
7477
}
7578

7679
local OPTION = master._config.OPTION
@@ -98,12 +101,13 @@ end
98101

99102
master.convert = function(st, s)
100103
assert(type(st) == "string" or type(st) == "number", ("[CONVERT] INVALID_INPUT_TYPE | attempt to convert with a '%s'."):format(type(st)))
104+
assert(type(st) == "number" or st:find("^%d*%.?%d*$"), ("[CONVERT] MALFORMED_NUMBER | function not support number format or input wans't number format. (%s)"):format(st))
101105
st, s = tostring(st), s or 1
102106
assert(not (s <= 0), ("[CONVERT] SETTING_SIZE_ISSUE | size per chunk is less then one. (%s < 1)"):format(s))
103107
assert(not (s > master._config.MAXIMUM_SIZE_PERCHUNK), ("[CONVERT] INVALID_SIZE_PERCHUNK | size per chunk is more then maxiumum setting. (%s > %s)"):format(s, master._config.MAXIMUM_SIZE_PERCHUNK))
104108
local result, step = {_size = s}, 0
105-
local i, i2 = st:match("^0*(.-)%.(.-)0*$")
106-
i, i2 = (i or st):match("^0*(.-)$"):reverse(), (i2 or "")
109+
local i, i2 = st:match("^0*(%d*)%.?(%d-)0*$")
110+
i, i2 = (i or st):match("^0*(.-)$"):reverse(), i2 or ""
107111
local len_i, len_i2 = #i, #i2
108112
for index = 1, max(len_i, len_i2), s do
109113
step = step + 1
@@ -121,28 +125,42 @@ master.convert = function(st, s)
121125
end
122126

123127
master.deconvert = function(x)
128+
-- BUILD 186.6
124129
assert(istableobj(x or error("[DECONVERT] VOID_INPUT")), ("[DECONVERT] INVALID_INPUT_TYPE | attempt to deconvert with a '%s'."):format(type(x)))
125-
local em, sm, fm, s = false, {}, {}, x._size or 1
130+
local chunk_size = x._size or 1
131+
local pattern_format = ("%%0%dd"):format(chunk_size)
132+
local chunk_decimal = {}
133+
local process = false
126134
for i = x._dlen or 1, 0 do
127-
local v = tostring(x[i] or error(("[DECONVERT] DAMAGED_OBJECT | missing decimal part value. index[%s]"):format(i)))
128-
assert(type(x[i]) == "number", ("[DECONVERT] DAMAGED_OBJECT | detected invalid type in decimal part data. index[%s]: integer (not %s)"):format(i, type(v)))
129-
if not em and x[i] <= 0 then
130-
x[i] = nil
135+
local v = x[i] or error(("[DECONVERT] DAMAGED_OBJECT | missing decimal part value. index[%s]"):format(i))
136+
assert(type(v) == "number", ("[DECONVERT] DAMAGED_OBJECT | detected invalid type in decimal part data. index[%s]: integer (not %s)"):format(i, type(v)))
137+
local target = -i + 2
138+
if process then
139+
chunk_decimal[target] = pattern_format:format(v)
140+
elseif v > 0 then
141+
process = true
142+
chunk_decimal[1] = "."
143+
chunk_decimal[target] = pattern_format:format(v):match("^(%d-)0*$")
131144
else
132-
sm[-i], em = tonumber(v) % 1 ~= 0 and error(("[DECONVERT] DAMAGED_OBJECT | data issue at decimal part value. index[%s]"):format(i)) or ("0"):rep(s - #v)..v, true
145+
x[i] = nil
133146
end
134147
end
135-
em = false
148+
process = false
149+
local chunk_integer = {}
136150
for i = #x, 1, -1 do
137-
local v = tostring(x[i] or error(("[DECONVERT] DAMAGED_OBJECT | missing integer path value. index[%s]"):format(i)))
138-
assert(type(x[i]) == "number", ("[DECONVERT] DAMAGED_OBJECT | detected invalid type in integer part data. index[%s]: integer (not %s)"):format(i, type(v)))
139-
if not em and x[i] <= 0 then
140-
x[i] = nil
151+
local v = x[i] or error(("[DECONVERT] DAMAGED_OBJECT | missing integer path value. index[%s]"):format(i))
152+
assert(type(v) == "number", ("[DECONVERT] DAMAGED_OBJECT | detected invalid type in integer part data. index[%s]: integer (not %s)"):format(i, type(v)))
153+
local i = #x - i + 1
154+
if process then
155+
chunk_integer[i] = pattern_format:format(v)
156+
elseif v > 0 then
157+
process = true
158+
chunk_integer[i] = pattern_format:format(v):match("^0*(%d-)$")
141159
else
142-
fm[#fm+1], em = tonumber(v) % 1 ~= 0 and error(("[DECONVERT] DAMAGED_OBJECT | data issue at integer path value. index[%s]"):format(i)) or x[i+1] and ("0"):rep(s - #v)..v or v, true
160+
x[i] = nil
143161
end
144162
end
145-
return (fm[1] and table.concat(fm) or "0")..(sm[0] and "."..table.concat(sm, "", 0):match("(%d-)0*$") or "")
163+
return (#chunk_integer > 0 and table.concat(chunk_integer) or "0")..(chunk_decimal[2] and table.concat(chunk_decimal) or "")
146164
end
147165

148166
master.copy = function(x)
@@ -286,7 +304,7 @@ master.concat = {
286304
end,
287305

288306
_deep = function(var, reverse, dlen) -- Returns number of chunk, that are start first.
289-
-- BUILD 3
307+
-- BUILD 186.3
290308
local dlen = dlen or var._dlen or 1
291309
while var[dlen - 1] do
292310
dlen = dlen - 1
@@ -309,7 +327,7 @@ master.concat = {
309327
end,
310328

311329
_seek = function(var, reqsize, offset, reverse, ignore) -- set and gets number position.
312-
-- BUILD 3
330+
-- BUILD 186.3
313331
assert(var and reqsize, ("[SEEK] VOID_INPUT |%s%s"):format(not var and " var: nil (input-required)" or "", not reqsize and " reqsize: nil (input-required)" or ""))
314332
assert(tonumber(reqsize), ("[SEEK] INVALID_INPUT | reqsize: integer (not %s)"):format(type(reqsize)))
315333
reqsize = tonumber(reqsize)
@@ -365,7 +383,7 @@ master.concat = {
365383
end,
366384

367385
left = function(self, x, y, ignore, shift, copy, force)
368-
-- BUILD 3
386+
-- BUILD 186.3
369387
assert(type(self) == "table" and self._creq and self:_creq(x, y, force), "[CONCAT] BAD_FUNCTIONCALL | can't include required function")
370388
x = copy and master.copy(x) or x
371389
shift = max(tonumber(shift) or 0, 0)
@@ -403,7 +421,7 @@ master.concat = {
403421
end,
404422

405423
right = function(self, x, y, ignore, shift, copy, force)
406-
-- BUILD 3
424+
-- BUILD 186.3
407425
assert(type(self) == "table" and self._creq and self:_creq(x, y, force), "[CONCAT] BAD_FUNCTIONCALL | can't include required function")
408426
x = copy and master.copy(x) or x
409427
shift = max(tonumber(shift) or 0, 0)
@@ -514,11 +532,12 @@ master.calculate = {
514532
-- print(offset, ("%09d"):format(BA), ("%09d"):format(b[i2] or 0), "=", calcul, "+", result[offset] or 0, "=", chunk_data)
515533
local next = floor(chunk_data / s)
516534
chunk_data = chunk_data % s
517-
if not cd then
518-
cd = chunk_data ~= 0
519-
end
535+
cd = cd or chunk_data ~= 0
520536
result[offset] = (offset > 0 or cd) and chunk_data or nil
521-
result[offset + 1], op = (next ~= 0 and (next + (result[offset + 1] or 0))) or ((offset > 0 or cd) and result[offset + 1] or 0) or result[offset + 1], result[offset] and min(op, offset) or op
537+
result[offset + 1] = (next ~= 0 and (next + (result[offset + 1] or 0))) or ((offset > 0 or cd) and result[offset + 1] or 0) or result[offset + 1]
538+
if offset < 1 and op == 1 then
539+
op = (result[offset] and offset) or (result[offset + 1] and offset + 1)
540+
end
522541
end
523542
if e and #result >= 1 then -- optimize zone for div function
524543
if (#result == 1 and result[1] ~= 0) then
@@ -561,14 +580,14 @@ master.calculate = {
561580
div = function(self, a, b, s, f, l) -- _size maxiumum *1 (`f` The maxiumum number of decimal part, `l` The maximum number of iterations to perform.) **chunk size should be same**
562581
self._verify(a, b, master._config.MAXIMUM_SIZE_PERCHUNK, "DIV")
563582
assert(not master.equation.equal(b, masterC(0, b._size or 1)), "[DIV] INVALID_INPUT | divisor cannot be zero.")
564-
local s, b_dlen, f = a._size or s or 1, b._dlen or 1, f or ACCURACY_LIMIT.MASTER_DEFAULT_FRACT_LIMIT_DIV
583+
local s, b_dlen, f = a._size or s or 1, b._dlen or 1, f or (not OPTION.MASTER_CALCULATE_DIV_AUTO_CONFIG_ITERATIONS and ACCURACY_LIMIT.MASTER_DEFAULT_FRACT_LIMIT_DIV or 0)
565584
local auto_acc, more, less, concat = not l and OPTION.MASTER_CALCULATE_DIV_AUTO_CONFIG_ITERATIONS, master.equation.more, master.equation.less, master.concat
566585
local one = masterC(1, s)
567586
local accuracy, uc = 0, 0
568587
local lastpoint, fin, mark
569588
b = self:mul(b, masterC("1"..("0"):rep(math.abs(b_dlen - 1)), b._size))
570589
local d = OPTION.MASTER_CALCULATE_DIV_BYPASS_GEN_FLOATING_POINT and (function(b)
571-
local p = b == "1" and "1.0" or tostring("1" / b)
590+
local p = b == "1" and "1" or tostring("1" / b)
572591
if p:find("e") then
573592
local L, R = p:match("^[-+]?(%d-%.?%d+)e"), p:match("e[-+]?(%d+)$")
574593
L, lastpoint = L:sub(1, -2), L:sub(-2, -2)
@@ -590,6 +609,7 @@ master.calculate = {
590609
local FLOAT = ((#b - 1) * s) + #tostring(b[#b]) - 2
591610
d = FLOAT > 0 and "0."..("0"):rep(FLOAT)
592611
end
612+
local BUFF_ACCURATE_ENABLE = false
593613
if auto_acc then
594614
local function HF(x)
595615
return (s - #tostring(x[#x])) + (x._dlen < 1 and s - #tostring(x[x._dlen] or "") or 0)
@@ -603,8 +623,12 @@ master.calculate = {
603623
local MORE = more(AS, BS)
604624
accuracy = self.add(self.add(self.sub(self:mul((MORE and AS or BS), masterC(s, s)), masterC(HF(MORE and a or b), s)), masterC(f, s)), masterC(s, s))
605625
end
626+
if OPTION.MASTER_CALCULATE_DIV_AUTO_CONFIG_ITERATIONS_BUFF_MODE then
627+
BUFF_ACCURATE_ENABLE = true
628+
end
606629
else
607630
accuracy = (l or ACCURACY_LIMIT.MASTER_CALCULATE_DIV_MAXITERATIONS) + 1
631+
BUFF_ACCURATE_ENABLE = true
608632
end
609633
local function check(n)
610634
local dc = d and setmetatable({}, {__index = d, __len = function() return #d end}) or masterC(n, s)
@@ -666,7 +690,7 @@ master.calculate = {
666690
end
667691
else
668692
d = d:sub(1, auto_acc and masterD(accuracy) or accuracy)
669-
accuracy = auto_acc and self.sub(accuracy, masterC(#d)) or accuracy - #d:match("%.(.+)$")
693+
accuracy = auto_acc and self.sub(accuracy, masterC(#d)) or accuracy - #(d:match("%.(.+)") or "")
670694
d, lastpoint = masterC(d, s), lastpoint or d:match("(%d)0*$")
671695
end
672696
end
@@ -695,17 +719,30 @@ master.calculate = {
695719
if less(accuracy, one) then
696720
break
697721
end
698-
accuracy = self.sub(accuracy, one) or accuracy
722+
accuracy = self.sub(accuracy, one)
699723
else
700-
accuracy = (accuracy - 1 or accuracy)
724+
accuracy = accuracy - 1
725+
end
726+
end
727+
end
728+
-- Newton-Raphson --
729+
-- x = x * (2 - a * x)
730+
if BUFF_ACCURATE_ENABLE then
731+
local TWO = masterC(2, s)
732+
for _ = 1, ACCURACY_LIMIT.MASTER_CALCULATE_DIV_BUFF_ACCURATE do
733+
local rap = self:sub(TWO, self:mul(b, d))
734+
if rap._dlen >= 1 then
735+
break
701736
end
737+
d = self:mul(d, rap)
702738
end
703739
end
740+
--------------------
704741
if b_dlen < 1 then
705742
d = self:mul(d, masterC("1"..("0"):rep(math.abs(b_dlen - 1)), s))
706743
end
707744
local raw = self:mul(a, d)
708-
if lastpoint and -raw._dlen >= floor(f / s) then
745+
if f > 0 and lastpoint and -raw._dlen >= floor(f / s) then
709746
local shf = 0
710747
for i = 0, raw._dlen or 1, -1 do
711748
local sel = raw[i]

testsuite.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,17 @@ local function benchmark(head, function_call, function_arg)
8282
end
8383
end
8484

85+
local function format_result(value)
86+
local i, d = value:tostring():match("(%d+).?(%d*)")
87+
return ("0"):rep(INT_LEN - #i)..i..(d ~= "" and "."..d..("0"):rep(DEC_LEN - #d) or "")
88+
end
89+
8590
local function farg(t)
8691
local result_raw = 0
8792
print("index", "x", "y", "loss")
8893
for i, v in ipairs(t) do
8994
result_raw = result_raw + math.abs(v[3])
90-
print(i, v[1], v[2], math.abs(v[3]))
95+
print(i, format_result(v[1]), format_result(v[2]), math.abs(v[3]))
9196
end
9297
local result = result_raw / #t
9398
if tonumber(arg[6]) and result > tonumber(arg[6]) then
@@ -114,7 +119,9 @@ if not suite_select or suite_select == 0 then
114119
benchmark("mod", function(x, y)
115120
return int.fmod(x, y)
116121
end)
122+
print("[POW] simple value: limit mode")
117123
benchmark("pow", function(x, y)
124+
x, y = int.new(tostring(x):sub(1, 4), tostring(y):sub(1, 4))
118125
return x:pow(x.sign == "+" and y or y:floor())
119126
end)
120127
benchmark("sqrt", function(x, _)

0 commit comments

Comments
 (0)