Error-free sum in F#

108 Views Asked by At

I'm continuing with a port of GeographicLib into F#, and I'm wondering about the use of the error-free sum. In the C++ codebase, it is defined as

/**
 * The error-free sum of two numbers.
 *
 * @tparam T the type of the argument and the returned value.
 * @param[in] u
 * @param[in] v
 * @param[out] t the exact error given by (\e u + \e v) - \e s.
 * @return \e s = round(\e u + \e v).
 *
 * See D. E. Knuth, TAOCP, Vol 2, 4.2.2, Theorem B.  (Note that \e t can be
 * the same as one of the first two arguments.)
 **********************************************************************/
template<typename T> static inline T sum(T u, T v, T& t) {
  GEOGRAPHICLIB_VOLATILE T s = u + v;
  GEOGRAPHICLIB_VOLATILE T up = s - v;
  GEOGRAPHICLIB_VOLATILE T vpp = s - up;
  up -= u;
  vpp -= v;
  t = -(up + vpp);
  // u + v =       s      + t
  //       = round(u + v) + t
  return s;
}

For reference, the GEOGRAPHICLIB_VOLATILE constant gives you the ability to use the volatile keyword or not to use it, depending on your architecture and precision.

I must confess, I haven't read the Knuth book, and I don't have a copy to hand, so I can't read Theorem B, but the description is clear enough: the function is designed to return the sum and the floating point error of the sum.

My question is: are there any optimisations in the .NET Framework that I should know about before I implement this directly? Does F# manipulate floating point numbers in the same way as C++?

0

There are 0 best solutions below