search tag # rextended ieee754toint IEEE754 DWORD FLOAT to number
Functions to convert DWORD FLOAT as IEEE754 to number
This feature is not complete.
RouterOS does not support numbers larger than 9223372036854775807 or smaller than -9223372036854775808, and it also does not support decimal division.
So we can only have an approximation, because the float type go from
(+/-)0,(put 45 zeros here)140129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125
to
(+/-)340282346638528859811704183484516925440
and
is impossible to represent correctly on RouterOS.
So this function, in extreme cases, could produce an incorrect value.
:global ieee754toint do={
:local input [:tonum "$1"]
:local hack 0x3B9ACA00 ; # Hack, RouterOS do not support decimal numbers...
:local isneg ($input >> 31)
:local exponent (($input >> 23) & 0xFF)
:local powerof2 1
:for x from=1 to=($exponent - 0x7F) step=1 do={:set powerof2 ($powerof2 * 2)}
:set exponent $powerof2
:local mantissa ($input & 0x7FFFFF)
:set powerof2 $hack
:local temp $hack
:for x from=22 to=5 step=-1 do={ ; # is 5 and not 0 because missing support for decimals on RouterOS
:set powerof2 ($powerof2 / 2)
:if ((($mantissa >> $x) & 1) = 1) do={
:set temp ($temp + $powerof2)
}
}
:set mantissa $temp
:local total (($exponent * $mantissa) / $hack)
:local decimal (($exponent * $mantissa) % $hack)
:if ($decimal > 444444444) do={:set total ($total +1)}
:if ($isneg = 1) do={:set total ($total * -1)}
:return $total
}
example code
{
:local output {18630;49152} ; # simulate the read
# a = 18630 = 0x48C6
# b = 49152 = 0xC000
# (a * 0x1000) + b = (0x48C6 * 0x10000) + 0xC000 = 0x48C60000 + 0xC0000 = 0x48C6C000 = 1220984832
:local fullvalue ((($output->0) * 0x10000) + ($output->1))
:set fullvalue [$ieee754toint $fullvalue]
:local intmw ($fullvalue / 1000)
:local decmw "00$($fullvalue % 1000)" ; :set decmw [:pick $decmw ([:len $decmw] - 3) [:len $decmw]]
:put "$intmw,$"decmw"mW"
}
result code
407,040mW
next convert 407040,0 to it's intended value:
407040,0 / 1000 = 407,040 mW