HISE Logo Forum
    • Categories
    • Register
    • Login

    Odd result from Math.round

    Scheduled Pinned Locked Moved Solved General Questions
    8 Posts 4 Posters 403 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.
    • M_HollowayM
      M_Holloway
      last edited by

      Hi

      I have a slider to control gain on a Simple Gain module set with a scale of 0 to -100dB and a 0.1 step size and am using LAF to display the value in the slider handle. The actual numbers it spits out run to many decimal places so to round it to one dp, I'm using

      labelText = Math.round(obj.value * 10) / 10 + "dB";
      

      For most values, this works perfectly but between -8dB and -10dB it throws up odd results:

      Interface: -10.0dB
      Interface: -9.9dB
      Interface: -9.800000000000001dB
      Interface: -9.699999999999999dB
      Interface: -9.300000000000001dB
      Interface: -9.0dB
      Interface: -8.800000000000001dB
      Interface: -8.6dB
      Interface: -8.300000000000001dB
      Interface: -8.1dB
      Interface: -8.0dB
      

      When I remove the division by 10, the numbers are all integers so dividing by 10 should just move the decimal place and yet for these values it doesn't.

      Any thoughts, as ever, gratefully received.

      Dan KorneffD A 2 Replies Last reply Reply Quote 0
      • Dan KorneffD
        Dan Korneff @M_Holloway
        last edited by

        @M_Holloway I think this is normal. Once you round your numbers, use Engine.doubleToString to shorten the result to your desired decimals.

        Dan Korneff - Producer / Mixer / Audio Nerd

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

          If I remember correctly the laf object should contain the value as a text string which you can output directly.

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

          1 Reply Last reply Reply Quote 0
          • M_HollowayM
            M_Holloway @Dan Korneff
            last edited by M_Holloway

            @Dan-Korneff Great, thanks for that; replacing the rounding operation with Engine.doubleToString has done the job 👍

            1 Reply Last reply Reply Quote 0
            • M_HollowayM M_Holloway has marked this topic as solved on
            • d.healeyD
              d.healey
              last edited by

              obj.valueAsText is the thing I was thinking of.

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

              1 Reply Last reply Reply Quote 0
              • A
                aaronventure @M_Holloway
                last edited by

                @M_Holloway Kyle has a fresh one for https://www.youtube.com/watch?v=qTXwRSksJPg

                M_HollowayM 1 Reply Last reply Reply Quote 1
                • M_HollowayM
                  M_Holloway @aaronventure
                  last edited by

                  @aaronventure Fantastic,, now I get why this happens...

                  var n;
                  for(i = 0; i < 101; i++)
                  {
                  	n = i/10;
                  	Console.print("i = " + i);
                  	Console.print("n = " + n);
                  }
                  
                  
                  Interface: n = 8.0
                  Interface: i = 81
                  Interface: n = 8.1
                  Interface: i = 82
                  Interface: n = 8.199999999999999
                  Interface: i = 83
                  Interface: n = 8.300000000000001
                  Interface: i = 84
                  Interface: n = 8.4
                  Interface: i = 85
                  Interface: n = 8.5
                  Interface: i = 86
                  Interface: n = 8.6
                  Interface: i = 87
                  Interface: n = 8.699999999999999
                  Interface: i = 88
                  Interface: n = 8.800000000000001
                  Interface: i = 89
                  Interface: n = 8.9
                  Interface: i = 90
                  Interface: n = 9.0
                  Interface: i = 91
                  Interface: n = 9.1
                  Interface: i = 92
                  Interface: n = 9.199999999999999
                  Interface: i = 93
                  Interface: n = 9.300000000000001
                  Interface: i = 94
                  Interface: n = 9.4
                  Interface: i = 95
                  Interface: n = 9.5
                  Interface: i = 96
                  Interface: n = 9.6
                  Interface: i = 97
                  Interface: n = 9.699999999999999
                  Interface: i = 98
                  Interface: n = 9.800000000000001
                  Interface: i = 99
                  Interface: n = 9.9
                  Interface: i = 100
                  Interface: n = 10.0
                  
                  A 1 Reply Last reply Reply Quote 0
                  • A
                    aaronventure @M_Holloway
                    last edited by

                    @M_Holloway you can always do a custom round float function for your interface needs.

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

                    13

                    Online

                    1.7k

                    Users

                    11.8k

                    Topics

                    102.8k

                    Posts