useful_programs
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
useful_programs [2020/06/03 12:01] – [Other handy functions] emozolyak | useful_programs [2021/02/12 10:15] – [Curve handler] emozolyak | ||
---|---|---|---|
Line 877: | Line 877: | ||
</ | </ | ||
+ | ===== Curve handler ===== | ||
+ | Library | ||
+ | <code lua - curves.lib> | ||
+ | function GetCurveValue ( curve_register, | ||
+ | --[[ | ||
+ | --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, | ||
+ | --]] | ||
+ | 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, | ||
+ | in_range_status, | ||
+ | return in_range_status, | ||
+ | end | ||
+ | end | ||
+ | if x_to_find_y < table[1].range[1] then -- behaivior for outside left-sided | ||
+ | -- in_range_status, | ||
+ | in_range_status, | ||
+ | return in_range_status, | ||
+ | elseif x_to_find_y > table[# | ||
+ | -- in_range_status, | ||
+ | in_range_status, | ||
+ | return in_range_status, | ||
+ | end | ||
+ | |||
+ | return in_range_status, | ||
+ | end | ||
+ | </ | ||
+ | Example of usage: | ||
+ | <code lua - curves getter> | ||
+ | include " | ||
+ | |||
+ | function main (userId) | ||
+ | |||
+ | local t = os.date(" | ||
+ | current_hour = tonumber(t.hour) | ||
+ | curve_status, | ||
+ | WriteReg(" | ||
+ | |||
+ | end | ||
+ | </ | ||
+ | |||
+ | ===== Processing double float numbers ===== | ||
+ | |||
+ | <code lua> | ||
+ | function d2float(n) | ||
+ | |||
+ | local function getBits(input_num, | ||
+ | -- works with number and len | ||
+ | local tab = {} | ||
+ | local max_i = length - 1 | ||
+ | local remainder = input_num | ||
+ | | ||
+ | for i = max_i, 0, -1 do | ||
+ | local bit_ = (remainder - 2^i >= 0) and ' | ||
+ | table.insert(tab, | ||
+ | end | ||
+ | return tab | ||
+ | end | ||
+ | |||
+ | local function getNumberFromTab(tab, | ||
+ | local result_str = "" | ||
+ | |||
+ | for i = start, (start + length - 1) do | ||
+ | result_str = result_str .. tostring(tab[i]) | ||
+ | end | ||
+ | return tonumber(result_str, | ||
+ | end | ||
+ | |||
+ | local NaN = tonumber(" | ||
+ | local result_tab = {} | ||
+ | |||
+ | -- get a table of " | ||
+ | if (type(n) == " | ||
+ | result_tab = getBits(n, 64) | ||
+ | elseif (type(n) == " | ||
+ | local tmpS = '' | ||
+ | for i = 1, #n do | ||
+ | tmpS = tmpS .. table.concat(getBits(n[i], | ||
+ | end | ||
+ | |||
+ | for j=1, #tmpS do | ||
+ | table.insert(result_tab, | ||
+ | end | ||
+ | else | ||
+ | ERROR(" | ||
+ | end | ||
+ | |||
+ | local sign, exp, mantissa = 0, 0, 0 | ||
+ | local fraction_table = {} -- fraction part table | ||
+ | |||
+ | sign = ((result_tab[1] == " | ||
+ | exp = getNumberFromTab(result_tab, | ||
+ | |||
+ | for i = 13, 64 do | ||
+ | table.insert(fraction_table, | ||
+ | end | ||
+ | |||
+ | for j = 1, 52 do | ||
+ | if (fraction_table[j]== " | ||
+ | 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 | ||
+ | | ||
+ | end | ||
+ | |||
+ | if exp == 0x7ff then -- nan | ||
+ | | ||
+ | end | ||
+ | |||
+ | | ||
+ | end | ||
+ | </ | ||
useful_programs.txt · Last modified: 2024/03/19 09:39 by emozolyak