Skip to content

Commit 4978a4a

Browse files
committed
fix bug & optimize
1 parent 961a0c7 commit 4978a4a

3 files changed

Lines changed: 71 additions & 86 deletions

File tree

.doc/int.cnew.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
*It is recommended to use string type as "number" input, also you can input number type.*
1010

1111
```lua
12-
function int.cnew(number, size) -- (number:string|number, size:string|number) For setting a size per chunk. **CHUNK SIZE SHOULD BE SAME WHEN CALCULATE**
12+
---For create int object and setting a size per chunk.
13+
---@param number string|number
14+
---@param size string|number
15+
---@return table
16+
function int.cnew(number, size)
1317
```
1418

1519
| Parameter | Type | Description |

.doc/int.new.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
The [**int object**](../README.md#int-object) is represented as a table in Lua, designed for calculation purposes within the int module. This table includes numerical data and calculation-related information, and it supports a metatable to facilitate ease of use.
99

1010
```lua
11-
function int.new(...) -- (string|number) For only create. alway use default size! **CHUNK SIZE SHOULD BE SAME WHEN CALCULATE**
11+
---For create multiple int object in one times.
12+
---@param ... string|number
13+
---@return table
14+
function int.new(...)
1215
```
1316

1417
**Parameter:**

int.lua

Lines changed: 62 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- ULTIMATE INT --
33
----------------------------------------------------
44
-- MODULE VERSION: 186
5-
-- BUILD VERSION: 186.7.1 (04/05/2026) dd:mm:yyyy
5+
-- BUILD VERSION: 186.7.2 (06/05/2026) dd:mm:yyyy
66
-- USER FEATURE: 26/11/2025
77
-- DEV FEATURE: 27/12/2025
88
-- AUTHOR: SupTan85
@@ -27,7 +27,7 @@ local master = {
2727

2828
DEFAULT = intcur[1], -- recommend
2929
},
30-
30+
--#region
3131
OPTION = {
3232
--[[ MASTER DIVISION | BYPASS GENERATE FLOATING POINT >>
3333
How dose it work :
@@ -48,13 +48,12 @@ local master = {
4848
By SupTan85
4949
<< BUILD-IN >>]]
5050
MASTER_CALCULATE_DIV_AUTO_CONFIG_ITERATIONS = true,
51-
MASTER_CALCULATE_DIV_AUTO_CONFIG_ITERATIONS_BUFF_MODE = false, -- use buff-accurate for more accuracy. note: very slow but high accuracy!
5251
},
5352

5453
ACCURACY_LIMIT = {
5554
-- MASTER FUNCTION CONFIG --
5655
MASTER_CALCULATE_DIV_MAXITERATIONS = 15, -- 15
57-
MASTER_CALCULATE_DIV_BUFF_ACCURATE = 2, -- 2
56+
MASTER_CALCULATE_DIV_BUFF_ACCURATE = 4, -- 4
5857
MASTER_DEFAULT_FRACT_LIMIT_DIV = 14, -- 14
5958

6059
-- MEDIA FUNCTION CONFIG --
@@ -70,14 +69,14 @@ local master = {
7069
MEDIA_DEFAULT_SQRTROOT_MAXITERATIONS = 15, -- 15
7170
MEDIA_DEFAULT_SQRTROOT_FRACT_LIMIT = 14, -- 14
7271
},
73-
72+
--#endregion
7473
-- SYSTEM CONFIG ! Internal use only, Do not modify. ! --
7574
MAXIMUM_SIZE_PERCHUNK = intcur[1], -- stable size is 9
7675
MAXIMUM_LUA_INTEGER = intcur[2] -- math.maxinteger
7776
},
7877

7978
_VERSION = "186",
80-
_BUILD = "186.7.1"
79+
_BUILD = "186.7.2"
8180
}
8281

8382
local ISDEBUG = false -- mute everything that can be mute.
@@ -86,7 +85,7 @@ local OPTION = master._config.OPTION
8685
local ACCURACY_LIMIT = master._config.ACCURACY_LIMIT
8786
local OBJECT_CODENAME = "int object"
8887
local OBJECT_PROFILE = ({(OBJECT_CODENAME):gsub("%s+", "-")})[1] -- auto create profile
89-
local FEATURE_METATABLE_LEN = #setmetatable({}, {__len = function() return 32 end}) == 32 -- Lua >= 5.2 *optimize feature for newer lua version
88+
9089
local CONSTANT_LN2_DLEN = math.min(math.max(intcur[1] * 2, 14), 18)
9190
local CONSTANT_LN2 = "0."..("693147180559945309"):sub(1, CONSTANT_LN2_DLEN)
9291

@@ -103,13 +102,7 @@ local function istableobj(...) -- All value are table/int-object, else return fa
103102
local itype = type(v)
104103
if itype ~= OBJECT_CODENAME then
105104
if itype ~= "table" then
106-
if itype == "userdata" then
107-
if getmetatable(v).__name ~= OBJECT_CODENAME then
108-
return false
109-
end
110-
else
111-
return false
112-
end
105+
return false
113106
elseif not v._size then
114107
return false
115108
end
@@ -245,26 +238,22 @@ master.custom = {
245238
return x, x._dlen, tonumber(lastcut_num)
246239
end,
247240

248-
_refresh = function(x, lu, endp) -- refresh all chunk that should to be, by fast `ADD` process.
241+
_addcarry = function(x, sel, carry) -- add number in select chunk and refresh it all.
249242
-- BUILD 186.7
250-
lu = lu or 0
251-
local s, endp = x._size or 1, endp or x._dlen or 1
252-
local re
253-
local ca, sl = tostring(x[endp]):match("(%d-)0*$"), floor(10 ^ s)
254-
re = tonumber(endp < 1 and ca..("0"):rep(s - #ca) or ca) + (lu * floor(10 ^ (s - #ca)))
255-
x[endp], lu = re % sl, floor(re / sl)
256-
if x[endp] == 0 then
257-
x._dlen, x[endp] = x._dlen + 1, nil
243+
local s = 10 ^ (x._size)
244+
while carry > 0 do
245+
local data = (x[sel] or 0) + carry
246+
carry = floor(data / s)
247+
x[sel], sel = data % s, sel + 1
258248
end
259-
while lu ~= 0 do
260-
endp = endp + 1
261-
re = (x[endp] or 0) + lu
262-
x[endp], lu = re % sl, floor(re / sl)
263-
if x[endp] == 0 then
264-
x._dlen, x[endp] = x._dlen + 1, nil
249+
for i = x._dlen or 1, 0 do
250+
if (x[i] or 0) ~= 0 then
251+
x._dlen = i
252+
break
253+
else
254+
x[i] = nil
265255
end
266256
end
267-
x._dlen = max(endp, x._dlen)
268257
return x
269258
end,
270259

@@ -290,7 +279,7 @@ master.custom = {
290279
local s = x._size
291280
local x, endp, lastcut_num = self._cfloor(x, length)
292281
if endp < 1 and lastcut_num then
293-
x = lastcut_num > (center and center or 5) and self._refresh(x, 10 ^ (length % s), endp) or x
282+
x = lastcut_num > (center and center or 5) and self._addcarry(x, endp, 10 ^ ((s - length) % s)) or x
294283
end
295284
return x
296285
end
@@ -612,21 +601,6 @@ master.calculate = {
612601
return result
613602
end,
614603

615-
_var_mirror = FEATURE_METATABLE_LEN and function(d)
616-
return setmetatable({}, {__index = d, __len = function() return #d end})
617-
---@diagnostic disable-next-line: deprecated
618-
end or (newproxy and function(d)
619-
---@diagnostic disable-next-line: deprecated
620-
local proxy = newproxy(true)
621-
local meta = getmetatable(proxy)
622-
local data = {}
623-
624-
meta.__len = function() return #d end
625-
meta.__index = function(_, i) return data[i] or d[i] end
626-
meta.__newindex = data
627-
meta.__name = OBJECT_CODENAME
628-
return proxy
629-
end or master.copy),
630604
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**
631605
-- BUILD 186.7
632606
self._verify(a, b, master._config.MAXIMUM_SIZE_PERCHUNK, "DIV")
@@ -645,9 +619,6 @@ master.calculate = {
645619
L, lastpoint = #L >= 4 and L:sub(1, -2) or L, #L >= 4 and L:sub(-2, -2) or (#L > 1 and L:sub(-1, -1) or L)
646620
-- print(p, L, R, lastpoint)
647621
local S = #L:match("^(%d+)%.?")
648-
if R > master._config.MAXIMUM_LUA_INTEGER then
649-
error("[DIV] OVERFLOW | division function can't handle this number, or something went wrong. (%s > %s)")
650-
end
651622
return ("0."..("0"):rep(tonumber(R) - S)..(#L > 1 and L:gsub("%.", "") or L)):sub(1, -2)
652623
elseif p ~= "0.0" and p ~= "0" then
653624
if #p > 13 then
@@ -663,7 +634,7 @@ master.calculate = {
663634
d = FLOAT > 0 and "0."..("0"):rep(FLOAT)
664635
end
665636

666-
local BUFF_ACCURATE_ENABLE = false
637+
local BUFF_ACCURATE = false
667638
if auto_acc then
668639
local function HF(x)
669640
return (s - #tostring(x[#x])) + (x._dlen < 1 and s - #tostring(x[x._dlen] or "") or 0)
@@ -675,19 +646,15 @@ master.calculate = {
675646
else
676647
error("[DIV] OVERFLOW | division function can't handle this number, or something went wrong.")
677648
end
678-
if OPTION.MASTER_CALCULATE_DIV_AUTO_CONFIG_ITERATIONS_BUFF_MODE then
679-
BUFF_ACCURATE_ENABLE = true
680-
end
681649
else
682650
accuracy = (l or ACCURACY_LIMIT.MASTER_CALCULATE_DIV_MAXITERATIONS) + 2
683-
BUFF_ACCURATE_ENABLE = true
684651
end
685652

686-
local var_mirror = self._var_mirror
687653
local F_LIMIT = f or accuracy - 2
654+
local B_LIMIT = ceil(accuracy / s)
688655
local function check(n)
689656
-- print("d =", d and masterD(d) or "nil")
690-
local dc = d and var_mirror(d) or masterC(n, s)
657+
local dc = d and setmetatable({d[1]}, {__index = d}) or masterC(n, s)
691658
-- print(masterD(dc), masterD(d and concat:right(dc, n, false, uc, true) or dc))
692659

693660
local nc = self:mul(b, d and concat:right(dc, n, false, uc) or dc, s, true)
@@ -733,28 +700,20 @@ master.calculate = {
733700
-- print(high, low, code)
734701
return false, low
735702
end
736-
if d then
737-
if istableobj(d) then
738-
local fp, bp = d[2], d[1]
739-
accuracy = accuracy - masterD(bp)
740-
d = {0, _size = s, _dlen = 1}
741-
local SIZE = masterC(s, s)
742-
while equation:less(bp, one) do
743-
bp = self:sub(bp, SIZE)
744-
if equation:less(bp, one) then
745-
for v in fp:gmatch(("."):rep(s)) do
746-
d._dlen = d._dlen - 1
747-
d[d._dlen] = tonumber(v)
748-
end
749-
break
750-
end
751-
d._dlen = d._dlen - 1
752-
d[d._dlen] = 0
703+
do
704+
---@diagnostic disable-next-line: need-check-nil
705+
local forcus = d:sub(1, 2) == "0." and d:sub(3, -1)
706+
if forcus and #forcus > 0 then
707+
uc = #forcus:match("^0*")
708+
accuracy = accuracy - #forcus
709+
if uc < #forcus then
710+
uc = 0
711+
d, lastpoint = masterC(d, s), lastpoint or tonumber(d:sub(-1, -1))
712+
else
713+
d = nil
753714
end
754715
else
755-
d = d:sub(1, accuracy)
756-
accuracy = accuracy - #(d:match("%.(.+)") or "")
757-
d, lastpoint = masterC(d, s), lastpoint or d:match("(%d)0*$")
716+
d, lastpoint = masterC(d, s), lastpoint or tonumber(d:sub(-1, -1))
758717
end
759718
end
760719
while accuracy > 0 do
@@ -791,14 +750,25 @@ master.calculate = {
791750
end
792751
-- Newton-Raphson --
793752
-- x = x * (2 - a * x)
794-
if BUFF_ACCURATE_ENABLE then
795-
local TWO = masterC(2, s)
796-
for _ = 1, ACCURACY_LIMIT.MASTER_CALCULATE_DIV_BUFF_ACCURATE do
797-
local rap = self:sub(TWO, self:mul(b, d))
798-
if rap._dlen >= 1 then
753+
local TWO = masterC(2, s)
754+
for _ = 1, BUFF_ACCURATE or ACCURACY_LIMIT.MASTER_CALCULATE_DIV_BUFF_ACCURATE do
755+
local rap = self:sub(TWO, self:mul(b, d))
756+
d = self:mul(d, rap)
757+
758+
local breaktime = B_LIMIT
759+
for i = 0, rap._dlen, -1 do
760+
local v = rap[i]
761+
if v ~= 0 then
799762
break
763+
else
764+
breaktime = breaktime - 1
765+
if breaktime <= 0 then
766+
break
767+
end
800768
end
801-
d = self:mul(d, rap)
769+
end
770+
if breaktime <= 0 then
771+
break
802772
end
803773
end
804774
--------------------
@@ -1381,18 +1351,26 @@ local int = setmetatable({
13811351
__index = mediaobj
13821352
})
13831353

1384-
int.new = function(...) -- (string|number) For only create. alway use default size! **CHUNK SIZE SHOULD BE SAME WHEN CALCULATE**
1354+
---For create multiple int object in one times.
1355+
---@param ... string|number
1356+
---@return table
1357+
int.new = function(...)
13851358
local stack, em = {}, false
13861359
for i, s in ipairs({...}) do
13871360
stack[i], em = media.convert(s, int._defaultsize), true
13881361
end
13891362
if not em then
13901363
return media.convert(0, int._defaultsize)
13911364
end
1365+
---@diagnostic disable-next-line: redundant-return-value
13921366
return table.unpack(stack)
13931367
end
13941368

1395-
int.cnew = function(number, size) -- (number:string|number, size:string|number) For setting a size per chunk. **CHUNK SIZE SHOULD BE SAME WHEN CALCULATE**
1369+
---For create int object and setting a size per chunk.
1370+
---@param number string|number
1371+
---@param size string|number
1372+
---@return table
1373+
int.cnew = function(number, size)
13961374
return media.convert(number or 0, size and (tonumber(size) or master._config.SETINTEGER_PERCHUNK[size:upper()]) or int._defaultsize)
13971375
end
13981376

0 commit comments

Comments
 (0)