Floating point calculations

Home Forums General Programming Floating point calculations

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #69790
    pascalc
    Participant
      • Topics: 7
      • Replies: 27
      • Total: 34

      Hi,

      I am working on the Akai S2000 panel and it turned out that the tune parameter is coded in a signed floating point value of four nibbles…

      For example:

      -50.00 => 00 00 0E 0C

      -10.00 => 00 00 06 0F

      -0.10 => 07 0E 0F 0F

      -0.01 => 0E 0F 0F 0F

      0 => 00 00 00 00

      0.01 => 02 00 00 00

      0.10 => 09 01 00 00

      10.00 => 00 00 0A 00

      50.00 => 00 00 02 03

      I have been searching quite a lot on the Internet for an algorithm in Lua that would translate this for me but could not find anything except to implement it by myself!

      Now finally I found this topic on SO http://stackoverflow.com/questions/29592951/lua-hex-to-float that suggests using Lua 5.3.

      My question is if there are any plans to upgrade Ctrlr to Lua 5.3 soon or if there is any other support for floating point calculations available that could save me the work of implementing the translation algorithm from scratch.

      Thanks

      /Pascal

      #69791
      goodweather
      Participant
        • Topics: 45
        • Replies: 550
        • Total: 595
        • ★★★

        Hi, why not just copying the functions mentioned in http://stackoverflow.com/questions/18886447/convert-signed-ieee-754-float-to-hexadecimal-representation?

        In my panels, I have implemented a series of functions (like trim and co…) in a method I called Miscellaneous.
        You can call any function in Miscellaneous from any other method and the only thing to do is copy/paste…

        I suspect that going to Lua 5.3 will mean a lot of work and tests for atom…

        Anyway, very good topic (I mean the conversion hex-float)!

        #69800
        atom
        Keymaster
          • Topics: 159
          • Replies: 2945
          • Total: 3104
          • ★★★★★

          i can have a look at 5.3 but i remember there were issues with luabind, the framework i’m using for c++/lua integration, but i’ll have a look. i wanted to do that and supoort LuaJIT also to get better performance. it’s on my todolist.

          #69803
          atom
          Keymaster
            • Topics: 159
            • Replies: 2945
            • Total: 3104
            • ★★★★★

            could you also post that on github so i don’t forget ?

            #69805
            pascalc
            Participant
              • Topics: 7
              • Replies: 27
              • Total: 34

              Atom, Thanks! I have submitted an issue on Github now.

              Goodweather, I had tried your suggested SO code but obviously not thoroughly enough. I’ll take a new look at it now. BTW, which panels have you made? I would like to have a look at them as reference.

              BR
              /Pascal

              #69832
              pascalc
              Participant
                • Topics: 7
                • Replies: 27
                • Total: 34

                Hi again,

                After some more help from Stackoverflow I now have the two following algorithms:

                
                function float2nibbles(value)
                	local nibbles = MemoryBlock(4, true)
                	local n = math.floor(math.abs(value) * 256 + 0.13)
                	n = value < 0 and 0x10000 - n or n
                	for pos = 0, 3 do
                		nibbles:setByte(pos, n % 16)
                		n = math.floor(n / 16)
                	end
                	return nibbles
                end
                
                function nibbles2float(memBlock, offset)
                	local bi = BigInteger(0)
                	bi:setBitRangeAsInt(0, 4, memBlock:getByte(offset))
                	bi:setBitRangeAsInt(4, 4, memBlock:getByte(offset + 1))
                	bi:setBitRangeAsInt(8, 4, memBlock:getByte(offset + 2))
                	bi:setBitRangeAsInt(12, 4, memBlock:getByte(offset + 3))
                	local n = 0
                	for i = 0, 15 do
                		local factor = math.pow(2, i - 8)
                		n = n + bi:getBitRangeAsInt(i, 1) * factor
                	end
                	return memBlock:getByte(offset + 3) >= 0x8 and n - 256 or n
                end
                

                For more information you can read this thread on SO: http://stackoverflow.com/questions/38912827/convert-floating-point-values-to-hexadecimal

                • This reply was modified 7 years, 8 months ago by pascalc.
                • This reply was modified 7 years, 8 months ago by pascalc.
              Viewing 6 posts - 1 through 6 (of 6 total)
              • The forum ‘Programming’ is closed to new topics and replies.
              There is currently 0 users and 76 guests online
              No users are currently active
              Forum Statistics
              Threads: 2,495, Posts: 17,374, Members: 77,605
              Most users ever online was 12 on January 22, 2019 3:47 pm
              Ctrlr