Using persist.UserData with doubles?

Moderators: TomKerekes, dynomotion

Post Reply
gonzalo
Posts: 13
Joined: Wed Feb 27, 2019 4:03 pm

Using persist.UserData with doubles?

Post by gonzalo » Thu Aug 29, 2019 12:01 pm

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!!

User avatar
TomKerekes
Posts: 2527
Joined: Mon Dec 04, 2017 1:49 am

Re: Using persist.UserData with doubles?

Post by TomKerekes » Thu Aug 29, 2019 5:09 pm

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
Regards,

Tom Kerekes
Dynomotion, Inc.

gonzalo
Posts: 13
Joined: Wed Feb 27, 2019 4:03 pm

Re: Using persist.UserData with doubles?

Post by gonzalo » Fri Aug 30, 2019 8:20 am

Great solution Tom, many thanks!!

Post Reply