Be quiet, NaN
|
10-14-2020, 04:15 PM
(This post was last modified: 10-14-2020 10:16 PM by Albert Chan.)
Post: #1
|
|||
|
|||
Be quiet, NaN
I was adding a feature to my old luajit 1.1.8, having randomseed() return its state.
(So that I can save it and restore later ...) I had patched luajit using xoroshiro128+. With 128 bits state, my first thought were to return 2 64-bits IEEE double. (luajit (still) does not support 64-bits integer) But, if the returned double happens to be signaling NaN, it change the bits, and turn it quiet. gcc:1> union {double d; uint64_t u;} h gcc:2> h.u = 0x7ff0000000000001ULL /* signaling NaN */ gcc:3> printf("%I64x", h.u) 7ff0000000000001 gcc:4> h.d = h.d /* just copied itself change the bits ! */ gcc:5> printf("%I64x", h.u) 7ff8000000000001 The fix is hard, see In which the CPU changes my data under my nose --- I gave up 2 IEEE double approach, and return 4 32-bits integers. Below is the example that tripped my 2 IEEE double approach. Last 64 bits = 7ff4d423272049a1, silently converted to 7ffc ... lua> randomseed(599) 1122989783 -1462116390 2146751523 656427425 lua> state = { randomseed() } -- save state lua> random(), random(), random() 0.2792170183740439 0.4552087223210315 0.9415057196559153 lua> ; randomseed(unpack(state)) -- restore state lua> random(), random(), random() 0.2792170183740439 0.4552087223210315 0.9415057196559153 |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
Be quiet, NaN - Albert Chan - 10-14-2020 04:15 PM
RE: Be quiet, NaN - mpark - 10-14-2020, 09:22 PM
RE: Be quiet, NaN - Albert Chan - 10-15-2020, 12:55 AM
RE: Be quiet, NaN - cruff - 10-14-2020, 10:46 PM
|
User(s) browsing this thread: 2 Guest(s)