Created
August 17, 2019 14:27
-
-
Save YashasSamaga/f400d7f0fef56c485fc60b6180ead758 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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