HISE Logo Forum
    • Categories
    • Register
    • Login

    Rounding error?

    Scheduled Pinned Locked Moved Bug Reports
    10 Posts 4 Posters 193 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • VirtualVirginV
      VirtualVirgin
      last edited by

      HISE is rounding 4.5 to 4 here:
      Screenshot 2025-02-02 at 1.36.07 PM.png

      Any ideas on what is going on?
      The other .5 values are rounded up properly.

      d.healeyD 1 Reply Last reply Reply Quote 0
      • d.healeyD
        d.healey @VirtualVirgin
        last edited by

        @VirtualVirgin 2.5 also gets rounded down. Use Math.ceil() instead for consistency.

        Libre Wave - Freedom respecting instruments and effects
        My Patreon - HISE tutorials
        YouTube Channel - Public HISE tutorials

        VirtualVirginV 1 Reply Last reply Reply Quote 0
        • VirtualVirginV
          VirtualVirgin @d.healey
          last edited by VirtualVirgin

          @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);
          
          ustkU 1 Reply Last reply Reply Quote 2
          • ustkU
            ustk @VirtualVirgin
            last edited by

            @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

            Can't help pressing F5 in the forum...

            d.healeyD 1 Reply Last reply Reply Quote 2
            • d.healeyD
              d.healey @ustk
              last edited by

              @ustk I'm on an Intel Linux system

              Libre Wave - Freedom respecting instruments and effects
              My Patreon - HISE tutorials
              YouTube Channel - Public HISE tutorials

              1 Reply Last reply Reply Quote 0
              • d.healeyD
                d.healey
                last edited by d.healey

                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#gafe8eb86381d389f4ba9f9931a38e1a44

                Note 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.

                Libre Wave - Freedom respecting instruments and effects
                My Patreon - HISE tutorials
                YouTube Channel - Public HISE tutorials

                ustkU VirtualVirginV 2 Replies Last reply Reply Quote 1
                • ustkU
                  ustk @d.healey
                  last edited by

                  @d-healey So we got our answer ☺

                  Can't help pressing F5 in the forum...

                  1 Reply Last reply Reply Quote 0
                  • VirtualVirginV
                    VirtualVirgin @d.healey
                    last edited by

                    @d-healey Wow. That is absurd!

                    d.healeyD hujackusH 2 Replies Last reply Reply Quote 1
                    • d.healeyD
                      d.healey @VirtualVirgin
                      last edited by

                      @VirtualVirgin It is strange, it's also concerning because this function is used a lot throughout the HISE codebase.

                      Libre Wave - Freedom respecting instruments and effects
                      My Patreon - HISE tutorials
                      YouTube Channel - Public HISE tutorials

                      1 Reply Last reply Reply Quote 1
                      • hujackusH
                        hujackus @VirtualVirgin
                        last edited by

                        @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));
                        	}
                        }
                        

                        Check out the music of Conway's Game of Life - https://www.youtube.com/@hujackus
                        ConwayMatrix - http://hujackus.altervista.org/conwaymatrix/

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post

                        29

                        Online

                        1.7k

                        Users

                        11.8k

                        Topics

                        102.7k

                        Posts