Numerical precision

1. Introduction

The API lets the caller state the number of bits of significand (precision) and of exponent (range) it needs, so that an implementation can change its arithmetic without changing the API. The default is IEEE 754 double precision, 53 bits of significand and 11 bits of exponent.

In the reference implementation the precision sets the machine epsilon ϵ=21−n and the thresholds that follow from it: the exponent at which the exponential of a Gaussian primitive is dropped, where the evaluation of an orbital stops, and the smallest Jastrow term that a sparse product keeps. In an --enable-hpc build the same value chooses between single and double precision: the atomic-orbital kernels evaluate the primitive exponentials in single precision at 23 bits or less, and the molecular-orbital kernels switch to their single-precision variants at 24 bits or less for the values and 26 bits or less for the VGL. The range is stored and returned; no kernel reads it.

qmckl_get_numprec_epsilon returns the epsilon of the requested precision, and qmckl_test_precision_64 compares two doubles and returns how many of their 53 significand bits agree. With --enable-fpe the library enables the invalid, division-by-zero and overflow floating-point exceptions when it is loaded, and installs a SIGFPE handler that prints a backtrace and exits.

2. Control of the numerical precision

Precision is the number of significand bits, the implicit leading bit included, and range the number of exponent bits, as in IEEE 754. The context starts with the values that binary64 uses:

  • 53 bits of precision (52 explicit and 1 implicit)
  • 11 bits of exponent
QMCKL_DEFAULT_PRECISION 53
QMCKL_DEFAULT_RANGE 11

The setters qmckl_set_numprec_precision and qmckl_set_numprec_range take a context and write one of the two 32-bit unsigned integers that qmckl_numprec_struct holds, under the context lock. They accept a precision from 2 to 53 and a range from 2 to 11, and return QMCKL_INVALID_ARG_2 outside those bounds, QMCKL_INVALID_CONTEXT when the context is not valid, and QMCKL_SUCCESS otherwise. The getters qmckl_get_numprec_precision and qmckl_get_numprec_range return the stored value as a 32-bit integer, or an exit code on an invalid context.

3. Precision

qmckl_set_numprec_precision sets the number of significand bits in the context, and qmckl_get_numprec_precision returns it.

C
qmckl_exit_code qmckl_set_numprec_precision(const qmckl_context context, const int32_t precision);

qmckl_get_numprec_precision returns the value of the numerical precision in the context.

C
int32_t qmckl_get_numprec_precision(const qmckl_context context);

4. Range

qmckl_set_numprec_range sets the number of exponent bits in the context, and qmckl_get_numprec_range returns it.

C
qmckl_exit_code qmckl_set_numprec_range(const qmckl_context context, const int32_t range);
Fortran
interface
   integer (qmckl_exit_code) function qmckl_set_numprec_range(context, range) bind(C)
     use, intrinsic :: iso_c_binding
     import
     integer (qmckl_context), intent(in), value :: context
     integer (c_int32_t), intent(in), value :: range
   end function qmckl_set_numprec_range
end interface

qmckl_get_numprec_range returns the value of the numerical range in the context.

C
int32_t qmckl_get_numprec_range(const qmckl_context context);

5. Helper functions

5.1. Epsilon

qmckl_get_numprec_epsilon returns ϵ=21−n where n is the precision. For an invalid context it returns NAN, so that a caller cannot mistake an exit code for an epsilon.

C
double qmckl_get_numprec_epsilon(const qmckl_context context);

5.2. Testing the number of unchanged bits

To check that an approximation keeps a given number of bits, the library counts how many leading significand bits two numbers share. It first counts by how many units in the last place (ulps) the two differ: countUlpDifference_64 reads the doubles as 64-bit integers, sums the two distances to zero when the signs differ, and saturates at INT64_MAX instead of overflowing.

qmckl_test_precision_64 turns that distance into bits: 53 for two equal values, and otherwise 53 minus the number of halvings the distance needs to reach zero, with a floor at zero. qmckl_test_precision_32 promotes its two floats to double and calls the 64-bit version.

C
int32_t qmckl_test_precision_64(double a, double b);
int32_t qmckl_test_precision_32(float a, float b);

6. Approximate functions

6.1. Exponential

Two fast exponential functions, adapted from Johan Rade's implementation (https://gist.github.com/jrade/293a73f89dfef51da6522428c857802d), based on Schraudolph's paper. No kernel calls them at present. Both build a floating-point value by biasing its exponent field and clamp the scaled input to the range where that works.

N. Schraudolph, "A Fast, Compact Approximation of the Exponential Function", Neural Computation 11, 853–862 (1999). (available at https://nic.schraudolph.org/pubs/Schraudolph99.pdf)