Back
Close

Modern C++ idoms and recipes

meshell
9,920 views
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();
}
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants