Skip to content

Commit b556baf

Browse files
committed
[RF] Don't use numerals directly as results in codegen
Numerals should be assigned to variables first, so they can also used in functions that expect `double` references.
1 parent 4bbcda9 commit b556baf

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

roofit/roofitcore/src/RooFit/CodegenContext.cxx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <algorithm>
2222
#include <cctype>
23+
#include <charconv>
2324
#include <fstream>
2425
#include <type_traits>
2526
#include <unordered_map>
@@ -216,14 +217,18 @@ void CodegenContext::addResult(RooAbsArg const *in, std::string const &valueToSa
216217
// std::string savedName = RooFit::Detail::makeValidVarName(in->GetName());
217218
std::string savedName = getTmpVarName();
218219

219-
// Only save values if they contain operations.
220-
bool hasOperations = valueToSave.find_first_of(":-+/*") != std::string::npos;
220+
// Only save values if they contain operations or they are numerals. Otherwise, we can use them directly.
221+
222+
// Check if string is numeric.
223+
char *end;
224+
std::strtod(valueToSave.c_str(), &end);
225+
bool isNumeric = (*end == '\0');
226+
227+
const bool hasOperations = valueToSave.find_first_of(":-+/*") != std::string::npos;
221228

222229
// If the name is not empty and this value is worth saving, save it to the correct scope.
223230
// otherwise, just return the actual value itself
224-
if (hasOperations) {
225-
// If this is a scalar result, it will go just outside the loop because
226-
// it doesn't need to be recomputed inside loops.
231+
if (hasOperations || isNumeric) {
227232
std::string outVarDecl = "const double " + savedName + " = " + valueToSave + ";\n";
228233
addToCodeBody(in, outVarDecl);
229234
} else {

0 commit comments

Comments
 (0)