Double values get truncated when sent by signal

Report crashes, strange behaviour, or apparent bugs
Post Reply
RalfR
Posts: 1
Joined: 28 May 2019, 09:18

Double values get truncated when sent by signal

Post by RalfR » 28 May 2019, 09:52

When trying to store a double value (with ~14 relevant digits) with a Lua signal, the corresponding function seems to be sim.setFloatSignal. However, the values seem to be truncated to 32-bit float values when using this.
A test code looks like this:

local testvar=simROS.getTime()
-- or something like: local testvar=123456789.123456
print(testvar)
sim.setFloatSignal("TestVar",testvar)
testvar=sim.getFloatSignal("TestVar")
print(testvar)

and returns:
1559031075,2268
1559031040

There does not seem to be a setDoubleSignal function. So there is currently no possibility to use signals with double-precision values !
Possible solutions: Implement a setDoubleSignal function (and corresponding functions), or let setFloatSignal work with double-precision values by default.

I suspect that the same problem occurs for sim.getFloatParamter (but have not tested it).

Meanwhile, does anybody know a workaround for this ?

coppelia
Site Admin
Posts: 7396
Joined: 14 Dec 2012, 00:25

Re: Double values get truncated when sent by signal

Post by coppelia » 28 May 2019, 10:44

Hello,

you can use following:

Code: Select all

    -- Set double signal:
    local doubleValue=123.4567890123
    sim.setStringSignal('myDoubleValue',sim.packDoubleTable({doubleValue}))

    -- Retrieve double signal:
    local str=sim.getStringSignal('myDoubleValue')
    if str then
        local retrievedDoubleValue=sim.unpackDoubleTable(str)[1]
    end
But yes, it makes sense to have sim.setFloatSignal / sim.getFloatSignal use doubles internally, or to add sim.setDoubleSignal / sim.getDoubleSignal.

Cheers

Post Reply