User Tools

Site Tools


useful_programs

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
useful_programs [2020/09/03 09:14] atolstovuseful_programs [2021/02/12 10:15] – [Curve handler] emozolyak
Line 925: Line 925:
  
 end end
 +</code>
 +
 +===== Processing double float numbers =====
 +
 +<code lua>
 +function d2float(n)  ----------------------- CONVERT DOUBLE FLOAT TO FLOAT -------------------------------------------
 +
 + local function getBits(input_num, length)  -- get number as table of bits 
 + -- works with number and len 
 + local tab = {}         
 + local max_i = length - 1                 
 + local remainder = input_num            -- rem of bitwise weighing 
 +   
 + for i = max_i, 0, -1 do
 + local bit_ = (remainder - 2^i >= 0) and '1' or '0'
 + table.insert(tab, bit_)           
 + end 
 + return tab
 + end 
 +
 + local function getNumberFromTab(tab, start, length)  -- get a number from table 
 + local result_str = ""
 +
 + for i = start, (start + length - 1) do 
 + result_str = result_str .. tostring(tab[i])
 + end 
 + return tonumber(result_str,2)
 + end 
 +
 + local NaN = tonumber("11111111111111111111111111111111", 2)
 + local result_tab = {} 
 +
 + -- get a table of "0" & "1"
 +   if (type(n) == "number") then 
 + result_tab = getBits(n, 64) 
 +   elseif (type(n) == "table") then 
 + local tmpS = ''
 + for i = 1, #n do 
 + tmpS = tmpS .. table.concat(getBits(n[i],8))
 + end 
 +  
 + for j=1, #tmpS do 
 + table.insert(result_tab, string.sub(tmpS, j, j))
 + end 
 + else 
 + ERROR("Unknown type for the d2float!")
 + end 
 +
 + local sign, exp, mantissa = 0, 0, 0
 + local fraction_table = {} -- fraction part table 
 +
 + sign = ((result_tab[1] == "1") and -1) or 1     -- get sign 
 + exp = getNumberFromTab(result_tab, 2, 11)       -- get exp 
 +
 + for i = 13, 64 do 
 + table.insert(fraction_table, result_tab[i]) -- mantissa
 + end 
 +
 + for j = 1, 52 do 
 + if (fraction_table[j]== "1") then 
 + mantissa = mantissa +(2 ^(-1 * j))    -- calc. mantissa by summing individual bits 
 + end 
 + end 
 + mantissa = mantissa + 1
 + local result_num = sign * (2 ^ (exp - 1023)) * mantissa
 +
 + ----------------------------------------- exceptions ----------------------------------
 + if exp == 0 then -- subnormals
 +    result_num = sign*(2^(-1022))*(mantissa-1)
 + end 
 +
 + if exp == 0x7ff then -- nan 
 +    result_num = NaN;
 + end 
 +   
 +   return result_num
 +end 
 </code> </code>
  
useful_programs.txt · Last modified: 2024/05/27 13:23 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki