HISE Logo Forum
    • Categories
    • Register
    • Login

    Rounding numbers

    Scheduled Pinned Locked Moved Scripting
    5 Posts 2 Posters 1.0k 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.
    • d.healeyD
      d.healey
      last edited by

      I'd expect this code to produce 2.33 but it gives me 2.33999999999 - is this a JavaScript thing or a HISE thing? Thanks.

      Console.print(Math.round(2.337898 * 100) / 100);
      

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

      1 Reply Last reply Reply Quote 0
      • Christoph HartC
        Christoph Hart
        last edited by

        Nope, that's standard double floating point behaviour (you don't have exact numbers but more like a guarantee how many digits are precise). But unless you are planning to simulate a scientific environment, it should not affect your functionality :)

        If you want 2.33, use Math.floor() instead (it still gives you the rounding error tough).

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

          Ah I see, there might be another solution then - I'm using custom knob and slider graphics on my interface and I want to display the current value of those knobs in separate labels so I'm using knob.getValue(). But for things like the ADSR sustain which is in dB I don't get the nice neat -100 - 0 dB readout like the default knob provides (when set to Decibels mode). Is there a command to retrieve a knob's display value rather than its actual value?

          Edit: Actually math.floor works fine for the sustain parameter. For a pitch constant modulator though I need more precision than an integer so I used```
          Console.print(Math.floor(2.337898 * 100) / 100);

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

          1 Reply Last reply Reply Quote 0
          • Christoph HartC
            Christoph Hart
            last edited by Christoph Hart

            Engine.doubleToString(number, digits)?

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

              Fantastic!

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

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

              55

              Online

              1.7k

              Users

              11.7k

              Topics

              101.8k

              Posts