Modern C++ idoms and recipes

meshell
59.3K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
Previous: Optional Next: Variant

std::optional

Possible solution

#include <functional>
#include <sstream>
#include <string>
#include <optional>
#include <cmath>

// symmetric difference quotient
std::optional<double> differentiate(std::function<double(double)> f, double h, double x)
{
    const auto result = (f(x + h) - f(x - h)) / (2 * h);
    if (std::isnan(result)) {
        return {};
    }
    return result;
}

std::string estimated_velocity(std::function<double(double)> distance, double time, double precision)
{
    auto velocity = differentiate(distance, precision, time);

    std::stringstream output;
    if (velocity) {
        output << "Estimated velocity after " << time << " seconds is " << velocity.value() << " m/s.";
    } else {
        output << "Estimation failed.";
    }
    return output.str();
}
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content