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
Best Answer
-
It can't be done
0
Answers
-
I mean directly, without converting to string and allocating a new RFA_string object with the SetFromString databuffer method.
0 -
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
0 -
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?
0 -
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
0 -
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"
0 -
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.
0 -
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.
0 -
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.
0
Categories
- All Categories
- 3 Polls
- 6 AHS
- 36 Alpha
- 166 App Studio
- 6 Block Chain
- 4 Bot Platform
- 18 Connected Risk APIs
- 47 Data Fusion
- 34 Data Model Discovery
- 684 Datastream
- 1.4K DSS
- 615 Eikon COM
- 5.2K Eikon Data APIs
- 10 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- 3 Trading API
- 2.9K Elektron
- 1.4K EMA
- 249 ETA
- 554 WebSocket API
- 37 FX Venues
- 14 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 23 Messenger Bot
- 3 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 60 Open Calais
- 275 Open PermID
- 44 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 22 RDMS
- 1.9K Refinitiv Data Platform
- 643 Refinitiv Data Platform Libraries
- 4 LSEG Due Diligence
- LSEG Due Diligence Portal API
- 4 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.2K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 12 World-Check Customer Risk Screener
- 1K World-Check One
- 46 World-Check One Zero Footprint
- 45 Side by Side Integration API
- 2 Test Space
- 3 Thomson One Smart
- 10 TR Knowledge Graph
- 151 Transactions
- 143 REDI API
- 1.8K TREP APIs
- 4 CAT
- 26 DACS Station
- 121 Open DACS
- 1.1K RFA
- 104 UPA
- 192 TREP Infrastructure
- 228 TRKD
- 915 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 90 Workspace SDK
- 11 Element Framework
- 5 Grid
- 18 World-Check Data File
- 1 Yield Book Analytics
- 46 中文论坛