Skip to content

Instantly share code, notes, and snippets.

@YashasSamaga
Created August 17, 2019 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YashasSamaga/f400d7f0fef56c485fc60b6180ead758 to your computer and use it in GitHub Desktop.
Save YashasSamaga/f400d7f0fef56c485fc60b6180ead758 to your computer and use it in GitHub Desktop.
int main () {
auto dp = [](double x) {
if (x <= -37)
return std::exp(x);
else if (x <= 18)
return std::log1p(std::exp(x));
else if (x <= 33.3)
return x + std::exp(-x);
else
return x;
};
auto sp = [](float x) {
if (x <= -20)
return std::expf(x);
else if (-20 < x && x <= 9.0)
return std::log1pf(std::expf(x));
else if(9.0 < x && x <= 14.6)
return x + std::exp(-x);
else
return x;
};
std::cout << std::setprecision(100);
for (float i = -60; i < 60; i += 0.001f) {
float actual = float(sp(i));
float expected = float(std::log1p(std::exp(double(i))));
float delta = actual - expected;
std::cout << i << "\t" << delta << std::endl;
if (delta != 0.0) {
float prev_error = std::nextafter(float(sp(i)), -FLT_MAX) - float(std::log1p(std::exp(double(i))));
float next_error = std::nextafter(float(sp(i)), FLT_MAX) - float(std::log1p(std::exp(double(i))));
std::cout << "prev: " << prev_error << std::endl;
std::cout << "next: " << next_error << std::endl;
if (prev_error != 0.0 && next_error != 0.0)
std::cout << "ERROR MORE THAN CLOSEST REPRESENTABLE NUMBER\n";
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment