Why does lua string string matching do this?

I have an external application that monitors the temperatures of the processor and GPU ...

I use Lua with an extension for aliens to capture these values ​​(via GetWindowText ) and perform pattern matching on these values, effectively extracting the temperature digits from the string, which by default appears as something like a CPU 67.875 Β°C ...
But maybe I have the wrong idea about how patterns work in LUA (since they don't look like regular expressions)?

I use the pattern [%d]+[.%d+]* , which should match any number from 0 to 100.0, right?
Oddly enough, I get an incredibly strange conclusion when the values ​​reach 56.5 degrees (see Link).

Why is this happening?
And how can I extract the correct floating point values ​​(as a string) in the range from 0 to 100 in the format XYY.ZZZ , where X is optional, Y is optional, as well . is optional if Z exists?

+4
source share
1 answer

You see the effect of cumulative rounding errors because 0.16 cannot be accurately represented in a floating point. Below is the code below:

 local n = 0 while n < 10000 do local s = tostring(n/100) local t = s:match("[%d]+[.%d+]*") print(t) n = n + 16 end 

Now, to your question, try a simpler picture below:

 s="CPU 67.875 Β°C" print(s:match("CPU +(.-) +")) 
+5
source

Source: https://habr.com/ru/post/1481163/


All Articles