moonledger

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/moonledger
Log | Files | Refs

commit 5d3441876b76c2c7d86a7e757403a737d51affa4
parent 9b6ef07fc1867b47fbeb55356486e55bb12c98ab
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Wed, 17 Aug 2022 00:09:05 -0500

decmath: export tostring

Diffstat:
Mdecmath.lua | 27++++++++++++++++++++-------
1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/decmath.lua b/decmath.lua @@ -2,11 +2,17 @@ local imath = require "imath" -local meta = { - __tostring = function(val) - local str = tostring(val.sig) +function asstring(val) + local str = tostring(val.sig) + if val.exp < 0 then return string.sub(str, 1, #str + val.exp) .. "." .. string.sub(str, #str + val.exp + 1, #str) - end, + end + + return str +end + +local meta = { + __tostring = asstring, __add = function(a, b) if a.exp < b.exp then a, b = b, a @@ -26,10 +32,17 @@ return { -- todo normalize local asint, count = string.gsub(val, "%.", "") local pos = string.find(val, "%.") - assert(count == 1) - local dec = {sig=imath.new(asint), exp=pos-#val} + local dec = {} setmetatable(dec, meta) + if count == 1 then + dec.sig = imath.new(asint) + dec.exp = pos - #val + elseif count == 0 then + dec.sig = imath.new(asint) + dec.exp = 0 + end return dec end - end + end, + tostring = asstring }