Integer arithmetic


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.36.06
Platform: Mac OSX
Compiler: Not Provided


I am getting different results depending on whether I load some code that contains a a function or just simply code to be executed. Here is the file

int test() { // comment out this line for second example
  double xLo = 0.0;
  double xHi = 1.0;
  int nBins=10;
  double delta = (xHi - xLo) / nBins;
  for (int iBin = 0; iBin < nBins; iBin++) {
    double x = iBin * delta;
    std::cout << iBin << " " << x << " " << delta << " " << iBin * delta <<  std::endl;
  }
  return 1;
} //comment out this line for second example

loading and executing this code gives the expected result:


root [0] .L test.cc
root [1] test()
0 0 0.1 0
1 0.1 0.1 0.1
2 0.2 0.1 0.2
3 0.3 0.1 0.3
4 0.4 0.1 0.4
5 0.5 0.1 0.5
6 0.6 0.1 0.6
7 0.7 0.1 0.7
8 0.8 0.1 0.8
9 0.9 0.1 0.9
(int) 1

However, if I comment out the two lines declaring and ending the function I get a different result in which either integer arithmetic is done instead of double:


root [0] .L test.cc
0 0 0.1 0
1 0 0.1 0.1
2 0 0.1 0.2
3 0 0.1 0.3
4 0 0.1 0.4
5 0 0.1 0.5
6 0 0.1 0.6
7 0 0.1 0.7
8 0 0.1 0.8
9 0 0.1 0.9

My root details:


   ------------------------------------------------------------------
  | Welcome to ROOT 6.36.04                        https://root.cern |
  | (c) 1995-2025, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosxarm64 on Sep 17 2025, 18:52:01                   |
  | From tags/6-36-04@6-36-04                                        |
  | With Apple clang version 17.0.0 (clang-1700.3.19.1)              |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------

I cannot reproduce the issue, I obtain the same result for both version… Maybe it’s an issue with MacOS? maybe @couet can give it a try

@bellenot On a Linux (x86_64) machine, when trying the test.cc example with “commented lines”, ROOT 6.36.04 and 6.34.10 show the same problem as reported. ROOT 6.32.18 dies with a segmentation violation. ROOT 6.30.08 and older (e.g., 6.28.12) report and “error: expected unqualified-id” (for the “for” loop).

@preimer It seems to me that you are trying to create/use a so-called “unnamed macro”.
In this case, you need to “encapsulate” the whole source code in a {...} block.
So, in your test.cc example, replace the line:
int test() {
With a simple line:
{
I also recall that many older ROOT versions required the very first line of an “unnamed macro” file to be the “{” line (so, do not add any “comment lines”, or any preprocessor directives, right in the beginning of your macro file; add them after the initial “{” line, if needed).
Be also aware that there are known problems with “unnamed macros” in ROOT 6, e.g., the “return” from it is broken (reported in 2018, still unsolved in 2025).

Interesting… Thanks for trying! Can one of you open a GitHub issue for this?

Maybe related:

https://its.cern.ch/jira/browse/ROOT-7033

For the unnamed macro - return behavior, it slipped through the cracks, I opened now:

Yes, enclosing the code

{
  double xLo = 0.0;
  double xHi = 1.0;
  int nBins=10;
  double delta = (xHi - xLo) / nBins;
  for (int iBin = 0; iBin < nBins; iBin++) {
    double x = iBin * delta;
    std::cout << iBin << " " << x << " " << delta << " " << iBin * delta <<  std::endl;
  }
  return 1;
}

Works as expected on my MacBook.