广州大彩串口屏论坛_大彩开发者交流论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 4048|回复: 5

negative value, divide, etc

[复制链接]

19

主题

52

帖子

261

积分

中级会员

Rank: 3Rank: 3

积分
261
发表于 2022-10-7 14:52:31 | 显示全部楼层 |阅读模式
本帖最后由 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
回复

使用道具 举报

2

主题

1253

帖子

1万

积分

论坛元老

Rank: 8Rank: 8

积分
12553
发表于 2022-10-8 08:58:14 | 显示全部楼层
"//" in lua means to fetch remainder downward, which is consistent with the usage of math. floor ()
Easy doesn't enter into Grown-up life
成年人的生活里面没有“容易”二字
回复

使用道具 举报

2

主题

1253

帖子

1万

积分

论坛元老

Rank: 8Rank: 8

积分
12553
发表于 2022-10-8 09:01:30 | 显示全部楼层
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
Easy doesn't enter into Grown-up life
成年人的生活里面没有“容易”二字
回复

使用道具 举报

19

主题

52

帖子

261

积分

中级会员

Rank: 3Rank: 3

积分
261
 楼主| 发表于 2022-10-8 23:22:40 | 显示全部楼层
本帖最后由 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

  1.         a= 0xFF16        -- -234
  2.         
  3.         --16bit register is negative?
  4.         if (a & 0x8000 ~=0) then
  5.                 a=  (~a +1) & 0xFFFF
  6.                 a = -a
  7.         end

  8.         string_buf = string.format ( "%1d,%d" ,math.modf(a/10), math.abs(a)%10)
  9.         set_text(Screen,27,string_buf)
复制代码


After conversion, Instead magic with string buf, to see variable wit the same result I can simple write
  1. set_text(Screen,27,a/10)
复制代码
but I need this magic to add some char to variable text.
回复

使用道具 举报

2

主题

1253

帖子

1万

积分

论坛元老

Rank: 8Rank: 8

积分
12553
发表于 2022-10-9 08:59:04 | 显示全部楼层
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
Easy doesn't enter into Grown-up life
成年人的生活里面没有“容易”二字
回复

使用道具 举报

2

主题

1253

帖子

1万

积分

论坛元老

Rank: 8Rank: 8

积分
12553
发表于 2022-10-9 09:00:27 | 显示全部楼层
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.
Easy doesn't enter into Grown-up life
成年人的生活里面没有“容易”二字
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|广州大彩串口屏论坛_大彩开发者交流论坛

GMT+8, 2024-5-15 22:41 , Processed in 0.045524 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表