Page 1 of 1

Using persist.UserData with doubles?

Posted: Thu Aug 29, 2019 12:01 pm
by gonzalo
Hi All,

I need to pass data to a thread from the PC and for that I see that we can use the persist.UserData[] array. My problem is that my data is float for example 10.34, and the persist.UserData[] is integer. There is any fast method to do it without using 2 variable positions for each number?

many thanks for your support!!

Re: Using persist.UserData with doubles?

Posted: Thu Aug 29, 2019 5:09 pm
by TomKerekes
Hi gonzalo,

'doubles' are 64 bits and 'floats' are 32 bits. So storing a double will require 2 persist variables.

You can convert your double to a float and save it into one persist variable. It will still have over 6 digits of precision.

Or you might use "fixed point". Multiply your value by some power of 10 and save as an integer. Then when reading it divide by the power of 10. For example for 10.34 multiply by 10000.0 and save integer 103400. Then when reading 103400/10000.0 = 10.3400

Re: Using persist.UserData with doubles?

Posted: Fri Aug 30, 2019 8:20 am
by gonzalo
Great solution Tom, many thanks!!