negative value, divide, etc
本帖最后由 terefere 于 2022-10-7 16:09 编辑453 // 10 give 45. here is ok
-453 // 10 give -46. Why not -45?!
Generally, how to keep negative value in lua. If I give -12 in set_value function, then screen show it also as -12. But if get from modbus value 0xFFFF, then screen show it as 65535 instead -1. If I see good then Dacai use external function who translate it to negative before display. If yes, then I see here many else problem, like "if" statment who will see 0xFFFF as higher than 0x0 "//" in lua means to fetch remainder downward, which is consistent with the usage of math. floor () In addition, if your register data is a signed integer, you need to judge whether the highest bit of the register data is 1 in the script to distinguish whether the data is an integer or a negative number 本帖最后由 terefere 于 2022-10-8 23:24 编辑
If后要接end 发表于 2022-10-8 08:58
"//" in lua means to fetch remainder downward, which is consistent with the usage of math. floor ()
Yes, but with negative value made "upward".I use math.modf
a= 0xFF16 -- -234
--16bit register is negative?
if (a & 0x8000 ~=0) then
a=(~a +1) & 0xFFFF
a = -a
end
string_buf = string.format ( "%1d,%d" ,math.modf(a/10), math.abs(a)%10)
set_text(Screen,27,string_buf)
After conversion, Instead magic with string buf, to see variable wit the same result I can simple write
set_text(Screen,27,a/10) but I need this magic to add some char to variable text. terefere 发表于 2022-10-8 23:22
Yes, but with negative value made "upward".I use math.modf
math.modfis to split the data into integer parts and decimal parts. For example, a, b=math. modf (20.34) yields a=20 and b=0.34, respectively terefere 发表于 2022-10-8 23:22
Yes, but with negative value made "upward".I use math.modf
If you want to determine whether a number is negative, you can determine whether the highest bit is 1, and then subtract 65536 from this data to get the negative number.
页:
[1]