KSP bug, ui_table precision lost

Reid115
Reid115 Member Posts: 47 Member
edited August 2022 in Scripting Workshop

Run this code:

on init
    declare ui_table %table[4] (1, 1, 7200)
    %table[2] := 1908
    message(%table[2])
end on

on ui_control (%table)
    message(%table[2])
end on

When the script loads, the third column will be set to 1908 and be printed out. Now set any column except the third column to something else with your mouse. The third column will randomly get set to, and print out, 1907. This is a precision bug. If you add as many zeros as you can without overflow to the values, you'll see 190800000 -> 190799984. I don't know if this is ui_table specific.

And while I'm on this topic, there's a seconday reason why this is important. If you right click drag over a ui_table to draw a linear slope, $NI_CONTROL_PAR_IDX doesn't update. So if the last changed index of a table is significant in your code, you're going to have to keep a copy of the table data and manually loop through to check what's changed in the table callback each time. So if something is set to a precision-losing value like above during a table change operation, and then you change another value afterward, you're going to get that wrong index showing up as changed even though it wasn't, which messes everything up.

Comments

  • EvilDragon
    EvilDragon Moderator Posts: 1,023 mod
    edited August 2022

    Yeah this is probably (but not 100% sure) a float to int then back to float precision issue. A pretty risky thing to fix since it can break existing libraries in unforeseen ways.

Back To Top