Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TFormula: support locale settings for which the decimal separator is "," #17327

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmake/modules/RootConfiguration.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ if((tbb OR builtin_tbb) AND NOT MSVC)
else()
set(hastbb undef)
endif()
if(geom)
set(hasgeom define)
else()
set(hasgeom undef)
endif()
if(root7)
set(hasroot7 define)
else()
Expand Down
1 change: 1 addition & 0 deletions config/RConfigure.in
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#@hasroot7@ R__HAS_ROOT7 /**/
#@use_less_includes@ R__LESS_INCLUDES /**/
#@hastbb@ R__HAS_TBB /**/
#@hasgeom@ R__HAS_GEOM /**/

#if defined(R__HAS_VECCORE) && defined(R__HAS_VC)
#ifndef VECCORE_ENABLE_VC
Expand Down
4 changes: 3 additions & 1 deletion hist/hist/src/TFormula.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2263,7 +2263,9 @@ void TFormula::ProcessFormula(TString &formula)
map<TString, Double_t>::iterator constIt = fConsts.find(fun.GetName());
if (constIt != fConsts.end()) {
TString pattern = TString::Format("{%s}", fun.GetName());
TString value = TString::Format("%lf", (*constIt).second);
// #17225: we take into account LC_LOCALE settings for which the decimal separator
// is , instead of ., e.g. de_AT.UTF-8
TString value = TString::Format("%lf", (*constIt).second).ReplaceAll(",", ".");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be dependent on the value of LC_LOCALE? (in some local we might have 1,200.00 for one thousand two hundreds).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a good observation. But how can we adapt to all possible locale?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually it seems that there are only 3 decimal separators: comma, period and arabic decimal separator (https://randombits.dev/articles/number-localization/locale-list)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more about it... If we have an arabic decimal separator, it means that we also have arabic digits. At that point, I am unsure whether the decimal separator would be the first problem we hit...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think we need a larger conversation about local and how to handle them. It seems odds/confusion to all support local for this single functions. Until we have this conversation, I recommend we issue an explicit error when encountering a comma here.

formula.ReplaceAll(pattern, value);
fun.fFound = true;
// std::cout << "constant with name " << fun.GetName() << " is found " << std::endl;
Expand Down
4 changes: 1 addition & 3 deletions hist/hist/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ ROOT_ADD_GTEST(testTHn THn.cxx LIBRARIES Hist Matrix MathCore RIO)
ROOT_ADD_GTEST(testTH1 test_TH1.cxx LIBRARIES Hist)
ROOT_ADD_GTEST(testTHStack test_THStack.cxx LIBRARIES Hist)
ROOT_ADD_GTEST(testProject3Dname test_Project3D_name.cxx LIBRARIES Hist)
if(geom)
ROOT_ADD_GTEST(testTFormula test_TFormula.cxx LIBRARIES Hist)
endif()
ROOT_ADD_GTEST(testTFormula test_TFormula.cxx LIBRARIES Hist)
ROOT_ADD_GTEST(testTKDE test_tkde.cxx LIBRARIES Hist)
ROOT_ADD_GTEST(testTH1FindFirstBinAbove test_TH1_FindFirstBinAbove.cxx LIBRARIES Hist)
ROOT_ADD_GTEST(test_TEfficiency test_TEfficiency.cxx LIBRARIES Hist)
Expand Down
25 changes: 24 additions & 1 deletion hist/hist/test/test_TFormula.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,31 @@

#include "TFormula.h"

#include <locale.h>

#ifdef R__HAS_GEOM
// Test that autoloading works (ROOT-9840)
TEST(TFormula, Interp)
{
TFormula f("func", "TGeoBBox::DeclFileLine()");
TFormula f("func", "TGeoBBox::DeclFileLine()");
}
#endif

class localeRAII {
std::string fLocale;

public:
localeRAII() : fLocale(setlocale(LC_NUMERIC, nullptr)){};
~localeRAII() { setlocale(LC_NUMERIC, fLocale.c_str()); }
};

// #17225
TEST(TFormula, Locale)
{
localeRAII lraii;
setlocale(LC_NUMERIC, "de_AT.UTF-8");
TFormula f0("f0", "gausn(x)");
EXPECT_TRUE(f0.IsValid());
TFormula f1("f1", "landau(x)");
EXPECT_TRUE(f1.IsValid());
}
Loading