Yeah, this system really only works when the scale factor is a constant.
Which might be common in formats dealing with currencies for example.
Such formats generally represent values as integers in number of cents
so there aren't any issues related to floating point precision loss, but
then the int is scaled by 100 to get the actual dollars value when needed.

But if the scale factor isn't constant, you would need to use
inputValueCalc, e.g.:

  <xs:element name="scaleFactor" type="xs:int" ... />
  <xs:element name="intValue" type="xs:int" ... />
  <xs:element name="decValue" type="xs:decimal" dfdl:inputValueCalc="{
    ../intValue div math:pow(10, ../scaleFactor)
  }" />

Though, it might be a nice extension of daffodil to allow
dfdl:binaryVirtualDecimalPoint to accept a DFDL expression. Then this
could just look like;

  <xs:element name="scaleFactor" type="xs:int" ... />
  <xs:element name="decValue" type="xs:decimal"
    dfdl:binaryDecimalVirtualPoint="{ ../scaleFactor }" />

>From an implementation perspective I don't think it would be difficult
to add.

Though, I think in the real world binary decimal formats I've seen just
use the standard IEEE float or double representations. I think integer +
scale for decimals is relatively uncommon, and when it does happen, I
think the scale tends to be constant.

- Steve

On 9/30/19 7:56 AM, Costello, Roger L. wrote:
> Thank you Steve! One follow-up question, please.
> 
>> In order to get a value of 19.95, your data 
>> would contain the bytes 0x07CB (1995 in 
>> two's complement binary) and you'd have 
>> dfdl:binaryDecimalVirtualPoint="2" to 
>> move the decimal point two places to the left.
> 
> Is that how most real world data formats express 19.95? If I understand 
> correctly, the 19.95 is treated as the integer 1995 and then the integer 1995 
> is expressed in binary. Then, the data format must, by some means, indicate 
> that there is a decimal point between 19 and 95. Wouldn't that information 
> about the location of the decimal point either require some additional 
> information within the binary file, or some out-of-band knowledge by the 
> applications that processes the binary file? It seems, based on your 
> description, you are assuming the latter.
> 
> /Roger
> 
> -----Original Message-----
> From: Steve Lawrence <[email protected]> 
> Sent: Monday, September 30, 2019 7:40 AM
> To: [email protected]
> Subject: [EXT] Re: How are decimal (number with decimal point) values 
> expressed in a binary file?
> 
> If the type were xs:float or xs:double, then the dfdl:binaryFloatRep property 
> defines how binary is converted to a number. Daffodil currently only supports 
> "ieee", which is IEEE 754-1985 floating point representation. And byte 
> lengths must be 4 for xs:float or 8 for xs:double.
> 
> Things are a bit different for xs:decimal. In that case, we parse the number 
> of bits as an integer (based on the dfdl:binaryNumberRep property), and then 
> move the decimal point of that integer base on the value of 
> dfdl:binaryDecimalVirtualPoint.
> 
> So in your example, let's say your field length was 2 bytes and 
> dfdl:binaryNumberRep="binary" (i.e. two's complement). In order to get a 
> value of 19.95, your data would contain the bytes 0x07CB (1995 in two's 
> complement binary) and you'd have dfdl:binaryDecimalVirtualPoint="2" to move 
> the decimal point two places to the left.
> 
> 
> On 9/30/19 7:19 AM, Costello, Roger L. wrote:
>> Hello DFDL community,
>>
>> Scenario: The book cost is: 19.95
>>
>> The book data is in binary.
>>
>> How is 19.95 expressed in binary?
>>
>> How would the cost be expressed in a DFDL schema? Simply this:
>>
>> <xs:element name="cost" type="decimal" />
>>
>> /Roger
>>
> 

Reply via email to