LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Store decimal in 16 bits?

Can I store a number, say... 9x10^-3 in 16 bits without rounding to zero?

0 Kudos
Message 1 of 7
(3,860 Views)

What do you mean by "store"?

 

Obviously, you don't have an integer. What precision do you need?

 

Look into the fixed point data types.

0 Kudos
Message 2 of 7
(3,851 Views)

Store as in take a decimal control and write it in a binary file. Precision about 10^-5. Thanks for your help!

0 Kudos
Message 3 of 7
(3,835 Views)

You still have not provided enough information to give you a good answer.  Please tell us the range of values you will have.

 

Do you mean you need to resolve 9.01E-3 from 9.00E-3? Or something else? Do you have some way of storing a reference value in or near the range? For example if your reference value is 1E-5, then you need only store integers from 0 to 900 to cover all values from 0.01E-3 to 9.01E-3.

 

Lynn

0 Kudos
Message 4 of 7
(3,829 Views)

You can scale it into a 16 bit integer. It will reduce the resolution, but that might not be a problem for you.

 

We do this all the time, not to save it to disk, but to transfer values on Modbus, which originally only gave you 16 bits (a "register") for each value (nowadays it's more common to use 2 or 4 such registers to transfer the data as a float, but some old systems still only support 16 bits and/or have limited bandwidth that makes it sensible to only use 16 bits pe rvalue.

 

Exactly how you scale the data depends on the value range. Sometimes it is OK to just multiply the data by 10^n and round the result, then divide the data by 10^n when you read it back...A more common solution is to choose a range that is just large enough to cover the variation of your data, and translat5e the minimum of the range to the integer value 0 (if dealing with the 16 bits as an unsigned integer), and the maximum of the range to the value 65535 (or -32768 to + 32767 if you interpret the data as a signed integer). Let's say that your values typically vary from 1x10^-4 to 9x10^-2. Then you can store each fractional value x into an integer y by scaling it this way:

 

y = round to nearest ( (65535-0)* (x-1*10^-4))/(9x10^-2 - 1^10-4))

 

So in this case 9*10^-3 would scale to:

 

y = 65535 * (9*10~3 - 10^-4) / 0,0899  6488

 

When you read it you reverse the scaling to get:

 

x = (9*10^-2-1*10^-4)*6488/65535  + 1^10-4 = 0,0090001480...

 

As you can see the result is slightly different than the original value due to the rounding. The maximum error / resolution depends on how large your range needs to be compared to (divided by half of) the number of integer values available...

Message 5 of 7
(3,801 Views)

A third option could be to use a half-precision float...

0 Kudos
Message 6 of 7
(3,788 Views)

Is there any reason you can't just store it in it's original format (float, 32bits or double 64 bits)? No point making things difficult for yourself.

0 Kudos
Message 7 of 7
(3,775 Views)