32 bit float to 64 bit float


#1

In case anyone needs to convert a single precision float to a double precision float here is a subroutine for that:

Passed parameters:
fInput float
nnOutput int64


Move32Bits(fInput, nInputInt);


// pull out the sign bit (and first bit of the exponent)
nSignBits = nInputInt bitAND 0xC0000000;


// move the rest of the bits to an int64 for manipulation
nnValue = MakeInt64(nInputInt, 0);


// right shift 3, but pad with the the second most significant bit of the exponent instead of 0's
// this will correctly account for the different exponent bias values of 127 and 1023
nnValue = (nnValue << 2) / 32;


// put the sign bit back in
nOutputLow = GetLowBitsOfInt64(nnValue);
nOutputHigh = (GetHighBitsOfInt64(nnValue) bitAND 0x3FFFFFFF) bitOR nSignBits;


// assemble the output
nnOutput = MakeInt64(nOutputHigh, nOutputLow);

This seems to work for all valid 32 bit floats, however you should note that it may fill sudo-random digits beyond the precision of the original value instead of filling with 0’s.
For example:
fInput = 1.1000000
nnOutput = 1.1000000238418580

All (smallish) integer input values should still convert exactly:
fInput = 1.0000000
nnOutput = 1.0000000000000000

Obviously the output here is stored in a 64 bit integer variable, since there is no 64 float type in Opto. :wink:


#2

Hello Digi314,

Welcome to the OptoForums! You’re right there is no 64-bit or double-precision float currently in the 9.4 PAC Control (or older) data type options. However, I have some questions for you:

  1. If we were to add such a data type to some OptoThing in the future… (I’ve heard rumors, but I can’t say any more or I’d have to kill you :wink: ;))… what would you use/need them for? (Help me build a use case, or start a petition or something here!)

  2. Since we DON’T currently have a real double-precious floats, what are you up to with this? (I’m confused about the decimal places you’re showing on that nnOutput since that’s an int – are you looking at it with some other tool or something? Do you then send that value somewhere else?)

Do share!

Thanks,
-OptoMary


#3

Mary, didn’t we have a pretty heavy discussion about this some time back??? I though the required code would be significantly more complex than what Digi314 is proposing, which is why i decided it wasn’t worth it…your explanation had my eyes glazing.
To answer your question about possible uses, my use was connecting via modbus serial to a Krohne mag meter. Krohne is a high end mag meter and they use dpf to accommodate the flow totals over long periods. Obviously, the real world is not what everybody adheres to and this is especially true of oil/gas pipe line measurement systems. When passing millions of gallons over a substantial period, the error (regardless of how small) begins to produce large dollar figures, so the oil companies (clients of pipe line) get all excited. So these systems take into account ridiculous measures such as SG, temp, pressure, atmospheric pressure, temps, and so on in addition to using double precision floats.
In reality, unless you are using an extremely accurate measurement system, it probably doesn’t make sense but it gives the technically challenged a warm and fuzzy feeling when the readout appears to be so accurate. Of course we are talking about resolution, not actual accuracy.