Skip to content

Commit 95727ca

Browse files
Use symmetric functions when rounding. (#176)
1 parent 7f12d6b commit 95727ca

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

src/Modules/Common.lua

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,60 @@ function floor(val, dec)
649649
end
650650
end
651651

652+
-- Symmetric round with precision: Rounds towards zero to <dec> decimal places.
653+
function roundSymmetric(val, dec)
654+
if dec then
655+
local factor = 10 ^ dec
656+
if val >= 0 then
657+
return m_floor(val * factor + 0.5) / factor
658+
else
659+
return m_ceil(val * factor - 0.5) / factor
660+
end
661+
else
662+
if val >= 0 then
663+
return m_floor(val + 0.5)
664+
else
665+
return m_ceil(val - 0.5)
666+
end
667+
end
668+
end
669+
670+
-- Symmetric floor with precision: Rounds down towards zero to <dec> decimal places.
671+
function floorSymmetric(val, dec)
672+
if dec then
673+
local factor = 10 ^ dec
674+
if val >= 0 then
675+
return m_floor(val * factor) / factor
676+
else
677+
return m_ceil(val * factor) / factor
678+
end
679+
else
680+
if val >= 0 then
681+
return m_floor(val)
682+
else
683+
return m_ceil(val)
684+
end
685+
end
686+
end
687+
688+
-- Symmetric ceil with precision: Rounds up towards zero to <dec> decimal places.
689+
function ceilSymmetric(val, dec)
690+
if dec then
691+
local factor = 10 ^ dec
692+
if val >= 0 then
693+
return m_ceil(val * factor) / factor
694+
else
695+
return m_floor(val * factor) / factor
696+
end
697+
else
698+
if val >= 0 then
699+
return m_ceil(val)
700+
else
701+
return m_floor(val)
702+
end
703+
end
704+
end
705+
652706
---@param n number
653707
---@return number
654708
function triangular(n)

src/Modules/ItemTools.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ end
4343
-- precision is express a multiplier/divide and displayPrecision is expresed as decimal precision on rounding.
4444
-- ifRequired determines whether trailing zeros are displayed or not.
4545
function itemLib.formatValue(value, baseValueScalar, valueScalar, precision, displayPrecision, ifRequired)
46-
value = round(value * precision) -- resolve range to internal value
47-
if baseValueScalar and baseValueScalar ~= 1 then value = round(value * baseValueScalar) end -- apply corrupted mult
48-
if valueScalar and valueScalar ~= 1 then value = m_floor(value * valueScalar) end -- apply modifier magnitude
46+
value = roundSymmetric(value * precision) -- resolve range to internal value
47+
if baseValueScalar and baseValueScalar ~= 1 then value = roundSymmetric(value * baseValueScalar) end -- apply corrupted mult
48+
if valueScalar and valueScalar ~= 1 then value = floorSymmetric(value * valueScalar) end -- apply modifier magnitude
4949
value = value / precision -- convert back to display space
50-
if displayPrecision then value = round(value, displayPrecision) end -- presentation
50+
if displayPrecision then value = roundSymmetric(value, displayPrecision) end -- presentation
5151
if displayPrecision and not ifRequired then -- whitespace is needed
5252
return string.format("%"..displayPrecision.."f", value)
5353
elseif displayPrecision then
54-
return tostring(round(value, displayPrecision))
54+
return tostring(roundSymmetric(value, displayPrecision))
5555
else
56-
return tostring(round(value, precision and m_min(2, m_floor(math.log(precision, 10))) or 2)) -- max decimals ingame is 2
56+
return tostring(roundSymmetric(value, precision and m_min(2, m_floor(math.log(precision, 10))) or 2)) -- max decimals ingame is 2
5757
end
5858
end
5959

0 commit comments

Comments
 (0)