question

Upvotes
Accepted
1 1 2 4

Converting doubles to Real type

I have a C# application that generates quotes to be published on TREP. The generated quotes are of IEEE double type, but the field is of OMM RWF_TYPE_REAL type.

Is there a convenient way to convert from double to Real databuffers? The Real type itself does not seem to expose any conversion methods.

-Anders


treprfarfa-apic#
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

I mean directly, without converting to string and allocating a new RFA_string object with the SetFromString databuffer method.

Upvotes
Accepted
1 1 2 4

It can't be done

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Upvotes
20.3k 73 10 20

Hi @ansn

I am not 100% sure what you are asking - have you referred to the Provider type examples that come with the RFA.NET SDK which demonstrate how to publish a real type field?

For example the Provider_Interactive.MarketPriceStreamItem class has an EncodeMarketPriceFieldList method which encodes a few fields including a Real type field.

e.g. you would convert your double into a long and then set the magnitude type to indicate the decimal point when encoding the Real databuffer

priceVal = 12345678; // e.g. double value of 123456.78 converted to a long
RFA.Data.Real real = new RFA.Data.Real();
real.MagnitudeType = MagnitudeTypeEnum.ExponentNeg2; // set this accordingly
real.Value = priceVal;  
dataBuffer.Real = real;
field.Data = dataBuffer;

The above sets the price to 123,456.78

You can find the full list of MagnitudeTypeEnum values in the documentation

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

That's all fine if you know the number at compile time. But if I hand you an arbitrary double, how do you select the appropriate MagnitudeType and Value such that the Real representation is closest to the double representation?

Hi @ansn

Work out how far you are moving the decimal point either way and use that to offset the MagnitudeTypeEnum either side of the Exponent0 value

That is as inelegant as it gets. My question was about how to avoid that. To do what you say you'd have to jump through large switch statements and either work with logarithms to convert between base 2 and base 10 or essentially be doing the .ToString()ing yourself. I'm looking for a simple, continuous, one-liner way that requires no handling of special cases. I'll take the answer as "it can't be done"

Upvotes
20.3k 73 10 20

Hi @ansn

In previous versions of the RFA API we used to offer functions for converting double / float values to Real. However, because IEEE 754 doubles cannot always represent exact decimals, the value were sometimes truncated as part of this conversion.

I am sure you will agree that is not a good idea if those values are then used for calculations, based on which actions are taken such as trades etc.

The mechanism described above the developer retains control and avoids any API caused rounding issues.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

That would depend on the application. More often than not, relative errors of around 10^-15 are quite ignorable. But yes, there are edge cases where carelessness will bite you. The Real type can also not always represent exact decimals, having a resolution of about 10^-19. See

"What every computer scientist should know about floating-point arithmetic" by D. Goldberg.

Even worse, being in radix 10 means subtraction of Reals is subject to much worse rounding errors than doubles, 900 % off vs. 100% off in the worst case.

Click below to post an Idea Post Idea