fickludd
fickludd

Reputation: 1157

c language symmetry in different cmath log() and exp() implementations?

Follow-up from the question Are cmath exp() and log() functions always symmetrical?

double x;
double y = exp(log(x));
assert(x == y);

Essentially, if one writes log(x) to disk and calculated exp() on another system - to what extent does the c language guarantee symmetry? Considering a long series of exp(log()), potentially on different systems, how far from the original x should one fear the result to go?

Upvotes: 1

Views: 204

Answers (2)

Eric Postpischil
Eric Postpischil

Reputation: 222659

In this answer, I address the variation between systems, not the round-trip of exp(log(x)). As indicated in your other question, the round-trip result is generally not equal to the input, even with the best possible implementations of exp and log.

In practice, there is no guarantee of the accuracy of math library functions, and different providers use different algorithms and approximations, so results vary from platform to platform.

The best commercial libraries aim for sub-ULP accuracy, meaning that each result is within one unit in the last place (of the floating-point format) of the exact mathematical result. This is often not achieved for some of the more esoteric functions in the math library. Not all libraries aim for this. Even if two different libraries both guarantee sub-ULP accuracy, they will not always return identical results.

There is an academic project, CRlibm with the goal of providing correctly rounded implementations of the math library routines. A correctly rounded implementation returns the unique representable value that is closest to the exact mathematical result in the chosen rounding method (to nearest, toward zero, toward infinity, or toward –infinity). A correctly-rounded result is the most accurate result possible given the limitations of the floating-point format. crlibm-1.0beta3.pdf provides correctly-rounded implementations of log and exp, with proofs.

Because the correctly-rounded result is uniquely defined, two libraries with correctly-rounded implementations will return the same results as each other.

Upvotes: 2

Reinderien
Reinderien

Reputation: 15231

As a general rule, floating-point operations like this (especially between different implementations of log, etc.) can never be guaranteed to have exact inverses. So my answer would be

  1. there will definitely be some loss,
  2. if you really care about how much, you should implement it and run a test, and
  3. this really smells as if it's something you want to do, and there's a way to get around the problem, in which case I would ask: what are you trying to do, and why are you encountering this problem in the first place?

Upvotes: 1

Related Questions