HISE Logo Forum
    • Categories
    • Register
    • Login

    parseInt cannot parse '08' and '09' but all other strings.

    Scheduled Pinned Locked Moved Bug Reports
    bugbug trackerscript
    5 Posts 3 Posters 405 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.
    • oskarshO
      oskarsh
      last edited by

      Now this is a interesting bug!

      This code:

      Console.print(parseInt('01'));
      Console.print(parseInt('02'));
      Console.print(parseInt('03'));
      Console.print(parseInt('04'));
      Console.print(parseInt('05'));
      Console.print(parseInt('06'));
      Console.print(parseInt('07'));
      Console.print(parseInt('08'));
      Console.print(parseInt('09'));
      Console.print(parseInt('10'));
      Console.print(parseInt('11'));
      Console.print(parseInt('12'));
      

      produces this output:

      Interface: 1
      Interface: 2
      Interface: 3
      Interface: 4
      Interface: 5
      Interface: 6
      Interface: 7
      Interface: 0
      Interface: 0
      Interface: 10
      Interface: 11
      Interface: 12
      

      @Christoph-Hart any idea why this might be the case? Why the 08 and 09 is there some deeper meaning to this?

      breathing HISE

      ustkU 1 Reply Last reply Reply Quote 0
      • ustkU
        ustk @oskarsh
        last edited by ustk

        @oskarsh Yeah this is a known thing, parseInt() decides if the string contains a int or a hex and is fooled by the 08...
        I prefer to use Math.round() instead.
        I just discovered String.getIntValue() but I haven't yet tried and don't know if there's a perf difference, trying....

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

        oskarshO 1 Reply Last reply Reply Quote 2
        • oskarshO
          oskarsh @ustk
          last edited by

          @ustk Interesting! Thanks for the insights. String.getIntValue() works as expected.

          breathing HISE

          ustkU 1 Reply Last reply Reply Quote 0
          • ustkU
            ustk @oskarsh
            last edited by ustk

            @oskarsh Made some test, better to stick with the the good ol' math function...

            const var test = ["321", "1564.21", "text", "08"];
            reg tmp;
            
            Console.startBenchmark();
            
            for (t in test)
            {
            	for (i=0;i<100000;i++)
            		//tmp = t.getIntValue();	// 174ms
            		tmp = Math.round(t);		// 100ms
            }
            
            Console.stopBenchmark();
            

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

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

              @ustk yeah it has to resolve two things (the object t and the function getIntValue) vs. one (only the object, the math function is resolved at compile time.

              But this performance difference is neglible - in UI contexts there are much slower things and if you are using anything string related in an audio processing context you have to sit back and evaluate life decisions that lead you into this situation of misery anyway.

              Also Math.round still returns a float number IIRC while getIntValue makes sure that the var instance uses the integer data slot internally.

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

              47

              Online

              1.7k

              Users

              11.7k

              Topics

              101.8k

              Posts