Rounding error?
-
HISE is rounding 4.5 to 4 here:
Any ideas on what is going on?
The other .5 values are rounded up properly. -
@VirtualVirgin 2.5 also gets rounded down. Use
Math.ceil()
instead for consistency. -
@d-healey It needs both ceil and floor to work, so I wrote an inline function to do the job:
inline function roundFix(value) { local remainder = Math.fmod(value, 1.0); local roundedValue; remainder >= 0.5 ? roundedValue = Math.ceil(value) : roundedValue = Math.floor(value); return roundedValue; }; const testRound = roundFix(4.49); Console.print("test round: " + testRound); const testRound1 = roundFix(4.5); Console.print("test round: " + testRound1);
-
@VirtualVirgin this shouldn't be the case and the rounding of a
.5
should always be up. Sounds like a bug to me and it might be related to this:
https://forum.hise.audio/topic/11808/default-value-bug-affecting-arm-macs?_=1738423446969 -
@ustk I'm on an Intel Linux system
-
I just took a look at the source and it's just a wrapper on JUCE's
roundToInt
function. Looking at the documentation this inaccuracy is a known trade-off for increased performance. - https://docs.juce.com/master/group__juce__core-maths.html#gafe8eb86381d389f4ba9f9931a38e1a44Note that this routine gets its speed at the expense of some accuracy, and when rounding values whose floating point component is exactly 0.5, odd numbers and even numbers will be rounded up or down differently.
-
@d-healey So we got our answer
-
@d-healey Wow. That is absurd!
-
@VirtualVirgin It is strange, it's also concerning because this function is used a lot throughout the HISE codebase.
-
@VirtualVirgin Another workaround could be this inline function.
// a more accuate rounding function for -2.1474836485e9<=value<=2.1474836465e9 inline function roundBetter(value){ local r = Math.round(value); return (r+.5==value)?r+1:r; } const testArray = [0.,128.,-128., 32767., -32767. , 65535., -65535., 16777215., -16777215., 2147483646., -2147483646.]; for(v in testArray){ for(i=-11;i<11;i++){ Console.print("round("+(v+i+.5) + ")=" +Math.round(v+i+.5)+" ?=" + roundBetter(v+i+.5)); } }