Adjust AI constructions algorithms#2225
Conversation
| namespace sys { | ||
|
|
||
| // US49 StackedCalculationWithExplanations allows uniting number calculations for backend with explanation tooltips for the UI | ||
| class StackedCalculationWithExplanations { |
There was a problem hiding this comment.
Unfortunately, there are some issues with this design. Turning a formula in the code into an instance of this type is a non-trivial performance sacrifice. I do understand the impulse behind it, however, and the idea can be rescued, even if this implementation isn't on the right track. What you want to do is develop a system of expression templates (the technique even has its own wikipedia page: https://en.wikipedia.org/wiki/Expression_templates ) that encodes the formula and any explanatory strings into a type at compile time, which in turn allows the calculation of that expression to be realized such that it can be compiled to the same end result as if it had been written out directly in the source code.
| StackedCalculationWithExplanations(float initialValue = 0.0f) | ||
| : currentValue(initialValue) { | ||
| // Apply a single operation to a value | ||
| static float apply(Operation op, float current, float val) { |
There was a problem hiding this comment.
This is getting closer to having zero overhead, but writing apply as you have means that, when compiled, the code will still be deciding what arithmetical operations to execute at runtime. Instead this logic can me moved into the step_node struct, allowing it to resolve the choice of operation at compile time.
| } else if constexpr(Op == Operation::MULTIPLY) { | ||
| return current * value; | ||
| } else if constexpr(Op == Operation::DIVIDE) { | ||
| if(value == 0.0f) throw std::runtime_error("Division by zero"); |
There was a problem hiding this comment.
exceptions are not enabled in PA. You can call std::abort or std::terminate instead. That said, checking every division instead of just allowing it to produce inf is slower; I would suggest making this just an assert so that it doesn't appear in the release build
No description provided.