HISE Logo Forum
    • Categories
    • Register
    • Login

    How to test for a NaN

    Scheduled Pinned Locked Moved General Questions
    9 Posts 3 Posters 344 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.
    • ustkU
      ustk
      last edited by ustk

      I am trying to test if a number is NaN after some derivative computation...

      I can throw an assertion in the console, but I'd like to be able to detect the NaN in code so I can force it, let's say to 0.0 instead.

      When I simply print the number, I get "nan" in the console.
      But I can't seem to detect it in script:

      Console.print(myNumber); // nan
      
      if (myNumber == "nan") // fails to detect
          myNumber = 0.0;
      
      if (myNumber == NaN) // also fails to detect
          myNumber = 0.0;
      

      Though if I do:

      // force NaN
      myNumber = NaN;
      
      if (myNumber == NaN) // This "forced" NaN is detected
          myNumber = 0.0;
      

      So why this difference between two NaNs? Is one with cheese while the other is raw? 😛

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

      Matt_SFM 1 Reply Last reply Reply Quote 0
      • Matt_SFM
        Matt_SF @ustk
        last edited by

        @ustk I'm not in front of my computer and I might be talking nonsense but, since you're looking for 'NotANumber', what if you test it like this? :

        if(yourNumber! = typeof(number)) 
        

        Develop branch
        Win10 & VS17 / Ventura & Xcode 14. 3

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

          @Matt_SF

          Don't even need the parentheses

          if (typeof valueToTest != "number")
          

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

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

            @d-healey ah, good to know!

            Develop branch
            Win10 & VS17 / Ventura & Xcode 14. 3

            1 Reply Last reply Reply Quote 0
            • ustkU
              ustk @d.healey
              last edited by

              @d-healey @Matt_SF thanks both, but I should have mentioned that when the value is computed and throws a nan in the console, then the variable typeof is still evaluated as number which is probably a bug.

              The nan from computed value is different than a NaN manually forced. I'll put a snippet together tomorrow to demonstrate the issue...

              @Christoph-Hart, anything about this?

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

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

                So here's a little snippet

                HiseSnippet 987.3ocsV01aaaCDlxIpXR6ErBre.2xWlMZpi8VW2vJRWS7KCdqwwntqneqflhphyxjFjT1yXI+G1O08OX6njhsbcPZqAlLfgt6dtiO2QdG0HshwMFkl3E7xUy4DuOye7JoMoSBUHIC5R79B+KnFKWCEpNe0bpwviHddG7KNEdAGRxe9me9bZJUx3aTQHuRIX7mKlIrazN5Y+lHMsOMh+RwrJnezyFvTxNpTUFxmC7aQlSYSoukOj5fUym3cudQBqRO1RsbCh4bUzpwIpkxB7uRXDSR4Ng1jwXfJTS5jHRiFcStZHDuCGsIyOnHy+J+KDQh052TA9xbCvFOpVC7pcWTp8GAk7pPoCKnz88Gyzh41MVb74S8GHwMjXJVpqRkBrjZ+8A9cTHBos4L5TdeMJr1i5OtUqiA7uFOIDQYTo7lrTNUWGUDFdxIfMQqVZ.JHoRPDCyoZ5L3TnUnPlJjbHNSxrBkDh3ZwBpUrfWm1H7uBCzbalVBsgGBWPsIMYJSBZBNoPzHj4hO.ZeB8IgWiqWHKgyl1WoGRGVuR7Z0HmNu6JVE8BZZ9hhTz8Nb5o.pFt5pff..yCShJKMBlvwLhCKoqBCBrXYUECaC2AlQkei0gMCKl.RTVgaNHNm.gAjYylv0GCB6F3n8lMatSjyjQ7Xj4QMbA+OxLVvpJX+wvjLKXviz.EqxSTK3GudovUACR9BVrZ.UtJm5XhFby90bsPZqezSgtbKunvL5rwi608HrnEbcX.O0v2E+WWAe+yF77R7Ag6FXGC9I3HbmpRd8.TwUt2xJs41Av.T7CyyesLOwJ+RLknVWoZoRO0.KS3RHVoYB4awiVXtFFjpXXTmsxk34aF4wxscVpKWYicSjf9W9hN85BO7oUx6qCWeJQIGpr7Kk0yOefVf20Tb7sZy01nUoob8sZ1MtReWNV+liH4Eo0.w94sGRbuOrgDrht3J.UxARg8x47R49pzHWyu68cGoPJGCfu86C5RsT2TlRcHt4bsU3niWW9BbLcwLm.+tbyTqZNNndmAR3nPUTVJ0t87Q2EAkFv5wVCkbCdjFgcU0KJ9HFZ15NGZ9gRw66ORXYI2NGqcKbDqT+evwxqZ9b+dwwXm3FBdne+Wuu2q7dV9WnxrXWGNAVK9Shm+vrYiwaXYbb0kRbTApyql6zRgbKmrqBLlKixE9W7ozXamrWow12XjLixzp2vJ5BbWl8I4ZPNIyu6N.+HBTFZSx6LpVmmg2s9FFa6Psiie69532suN9n80wueec7w6qi+v953O99cz8oOmkYUyJZaHjKF0KeThmWOIEOAleZk7e.Tdk9W
                

                Also, as strange as it sounds, a NaN is of type number so checking for this always returns true. There should be a way to just check for val == NaN instead

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

                Matt_SFM 1 Reply Last reply Reply Quote 0
                • Matt_SFM
                  Matt_SF @ustk
                  last edited by

                  @ustk

                  HiseSnippet 999.3ocsVstiaTCE1SxNHlgKhJwCvg7GRT2lMAJEDUao6lKn.cyF0Tp5+pb73zwjI1Q1dRHhceQ3oj2.33YlMYRSJfhnSjhlyU+cN93OOizJF2XTZhWvKVufS79X+wqk13NwTgjLnKw6S8uhZrbMjq5x0KnFCOh34U8GcJ7BNgj87m+vkzDpjw2phPdoRv3OSLWX2pczS+YQRReZD+Eh4k79gOc.SI6nRToHdp52hrfxlQeCeH04VEeh2GzKRXU5wVpkaHdmboJZ83X0JYt+uTXDSR3Ng1jwXhxU2WkD4PrSKoSrHIZzc0sgfYYz1tP07tvm6ekHRrQ+1twmkY.1FQ49gWkcgW0cfW6xvqUI3c.H4UBRmjCo64OloEKras3vyG4OPhaNSoXauLTx8kT4Op52QgdHsMmSmw6qQgMQT+QsZcJf+03wgnWFUBuIKgS00QEggmcFXi0pUFfBRpDDSgETMcNbNzJTHSDRNLMUxrBkDh3ZwRpUrjWm1H72CCzbapVBsgG.WQswMYJSLZBNKWzHjYh2GZeF8wg2hqWHKlyl0WoGRGVuT9Z0HCNu8JV16kzjrEEgn6c37ygZHjqA2bSPP.fUhIVklDAS3XMwgUz0gAAVrwplBEAf4w4tyYFU9kVmuoX6DPnxxCy4hKHPX.Y57Ib8ofvt0czdylM2KyoxH9TD6QMbI+WSMVvpxw+ovjTKXvAbfh84Ipk7S2rT3pfIIaAyWMfJWmAcrTCtaGagVHs0q8DnK2xyaMitX73dcqgssfaCC3IF999+Ek7u+ECdVg+Ag6mXGB9dnFtWUpttOp3F2aoE1vYCCGmsx1Lbox8Cq2epndwcfUXoQstV1JkdlAVEykvTklIjuAGxvZNLHQwvrOesqAjsojkI2FagtLkM1ufB5e8y6zqK7fmTp9uMby7hRNTY4WKqmMofVf21zzoGzl6.jVkjv0GzriDS+OEX86FUxZVabDOYuKcg+6ltnLaFK+7bIGUxARg85Eb46hiiTPBfu8KC5RsTGGSgNzuEbsU3ffWW9RjvNmwIvuK2LypVfT16QGgDgpnzDpcW1Q2UBEFvdvNTRNZGoQXWW9Ji+2nL+uBw64ORXYwGFiUN.FwN06CLVbQym32a5T7T3V.dhe+W894VExyUoV7jFx+pE+FNuMLc9X7tVFGWcoDoIPcdUbSK4xsbxtNvXtLJS3uvmBiscxdEFaemQxbJSqdMKex2cU1GloAwjL6V7.7yIPYnMI6zP497b7l0WyX6lp8B7qN1.+5iMvGdrA9MGafO5XC7aO1.+t+8.ce3yEoV077iMDxUi5kQk340SRwIvroUxeS8x1Nb
                  
                  if (val == "nan")
                  

                  Seems to work :)

                  Develop branch
                  Win10 & VS17 / Ventura & Xcode 14. 3

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

                    Why not avoid the nan at the source?

                    inline function derivative(a)
                    {
                    	return a == 0 ? -1 : 1 - Math.cosh(a) / Math.sinh(a) + 1/a;
                    }
                    

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

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

                      @Matt_SF Oh nice! I was certain I tested this too but apparently not!

                      @d-healey Yeah the apparence I could just treat it this way but unfortunately the number I need to return in the case of a NaN is depending on the context, so it's not that simple i the end

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

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

                      24

                      Online

                      1.7k

                      Users

                      11.8k

                      Topics

                      103.2k

                      Posts