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 revision
Previous revision
Next revisionBoth sides next revision
useful_programs [2020/06/03 12:01] – [Other handy functions] emozolyakuseful_programs [2021/02/12 10:15] – [Curve handler] emozolyak
Line 877: Line 877:
 </code> </code>
  
 +===== Curve handler =====
 +Library
 +<code lua - curves.lib>
 +function GetCurveValue ( curve_register, x_to_find_y  )
 +    --[[   
 +    --Curve handler 
 +    INPUT:
 +        Put curve register as first argument and X coordinate as second.
 +    OUTPUT:
 +        Get as result status if it is inside curve range of outside(false) and the Y value as second output argument
 +    
 +    EXAMPLE:
 +        curve_status, value = GetCurveValue( "curve_for_current_hour", current_hour )
 +    --]]
 +    table = cjson.decode( R ( curve_register ) )
 +    -- inRangeCurve( table, x_to_find_y )
 +    for _, value in ipairs( table ) do --piecewise handler inside range of the curve
 +        if ( value.range[1] <= x_to_find_y and x_to_find_y < value.range[2] ) then 
 +            -- in_range_status, index_curve_piece, y = true, index, curveLinearCalc( x_to_find_y, value.k, value.b )
 +            in_range_status, y = true, ( x_to_find_y * value.k + value.b )
 +            return in_range_status, y
 +        end
 +    end
 +    if  x_to_find_y < table[1].range[1] then -- behaivior for outside left-sided
 +        -- in_range_status, index_curve_piece, y = false, 1, curveLinearCalc( table[1].range[1], table[1].k*0, table[1].b ) --table[1].b
 +        in_range_status, index_curve_piece, y = false, 1, ( table[1].range[1] * table[1].k + table[1].b ) --table[1].b
 +        return in_range_status, y
 +    elseif x_to_find_y > table[#table].range[2] then -- behaivior for outside right-sided
 +        -- in_range_status, index_curve_piece, y = false,#table,curveLinearCalc( table[#table].range[2], table[#table].k, table[#table].b ) --table[#table].b
 +        in_range_status,  y = false,#table,( table[#table].range[2] * table[#table].k + table[#table].b)
 +        return in_range_status, y
 +    end
 +
 +    return in_range_status,
 +end
 +</code>
 +Example of usage:
 +<code lua - curves getter>
 +include "curves.lib"
 +
 +function main (userId)
 +
 +    local t = os.date("*t", os.time()) 
 +    current_hour = tonumber(t.hour)
 +    curve_status, y = GetCurveValue( "curve_for_current_hour", current_hour )
 +    WriteReg("value_for_current_hour", y) 
 +
 +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>
  
useful_programs.txt · Last modified: 2024/03/19 09:39 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki