Discussion Forums

Re: Representing a double as a Fast scaled decimal
Walter Mascarenhas / GeoCAD <>
8 Oct 2008 9:58PM ET

 Hi everybody,

   I believe you should first handle the rounding question
(truncation is much worse than rounding, I would not even
 consider it) Once you decide how to round you can decide
 how to pack the rounded result.

   Let me explain why with the example x = 2.5555 mentioned
earlier. This number cannot be represented exactly as
a double. If you do something like
   double xr = atof("2.5555")
then the xr you get is not exactly equal to 2.5555.
Actually, xr is the horrendous number

   xr = 2555499999999999882760448599583469331264495849609375 x 10^(-51)

In principle you could convert xr back to decimal
with no rounding (its is always possible to
convert from IEEE float,double to decimal with
no rounding errors if you are wiling to use
enough bits to represent the result)
However, the exact conversion is clearly a
bad idea because you would get a mantissa with
too many digits. Therefore, it is best to
round this number somehow before sending it.

  Once you decide what is a reasonable rounding
policy I may try to help you with the algorithm.

     Walter.

> Thanks, Daniel.
>
> My first instinct was to use frexp() for double->decimal and ldexp()
> from decimal to double. When I ran some initial benchmarks today I did
> not notice any big overhead.
>
> fxrexp() returns an integer exponent and normalized double mantissa
> (between 0.5 and 1.0). The real challenge is how to represent the
> results in a FAST wire representation - as the mantissa is still
> represented as a double/float rather then integer field. It is possible
> the send mantissa as a bit vector but then it will use at least 4/8
> bytes) not counting the length. So, I don't really like this approach.
>
> Dimitry
>
>
> > Dimitry, I had posted a related thread a while back, see
> > http://fixprotocol.org/discuss/read/eebb5ac9
> >
> > I think the optimal way to do this is to determine the amount of
> > precision you want from the IEEE float or double, and extract the
> > mantissa and exponent to create a scaled decimal. As you would expect,
> > this needs to be optimized, and I am not sure the standard C lib
> > methods are the way to go. Then comes the question of rounding vs.
> > truncation.
> >
> > So, does someone want to submit a good algorithm for conversion ?
> >
> > /Daniel


Representing a double as a Fast scaled decimal
Dimitry London / Morgan Stanley   8 Oct 2008 12:50PM ET
Re: Representing a double as a Fast scaled decimal
Anders Furuhed / Pantor Engineering   8 Oct 2008 2:13PM ET
Re: Representing a double as a Fast scaled decimal
Dimitry London / Morgan Stanley   8 Oct 2008 3:06PM ET
Re: Representing a double as a Fast scaled decimal
Rolf Andersson / Pantor Engineering   8 Oct 2008 3:22PM ET
Re: Representing a double as a Fast scaled decimal
Dimitry London / Morgan Stanley   8 Oct 2008 3:47PM ET
Re: Representing a double as a Fast scaled decimal
Rolf Andersson / Pantor Engineering   8 Oct 2008 3:56PM ET
Re: Representing a double as a Fast scaled decimal
Dimitry London / Morgan Stanley   8 Oct 2008 4:04PM ET
Re: Representing a double as a Fast scaled decimal
Daniel May / SpryWare, LLC   8 Oct 2008 5:27PM ET
Re: Representing a double as a Fast scaled decimal
Dimitry London / Morgan Stanley   8 Oct 2008 9:07PM ET
Re: Representing a double as a Fast scaled decimal
Walter Mascarenhas / GeoCAD   8 Oct 2008 9:58PM ET
Re: Representing a double as a Fast scaled decimal
Daniel May / SpryWare, LLC   9 Oct 2008 9:47AM ET
Re: Representing a double as a Fast scaled decimal
Daniel May / SpryWare, LLC   9 Oct 2008 10:48AM ET
Re: Representing a double as a Fast scaled decimal
Walter Mascarenhas / GeoCAD   9 Oct 2008 11:45AM ET
Re: Representing a double as a Fast scaled decimal
Dimitry London / Morgan Stanley   9 Oct 2008 11:16PM ET
Re: Representing a double as a Fast scaled decimal
Walter Mascarenhas / GeoCAD   10 Oct 2008 7:30AM ET
Re: Representing a double as a Fast scaled decimal
Dimitry London / Morgan Stanley   9 Oct 2008 11:04PM ET