# ROOT rounds to Integers

Does anyone know why?

user@NODE:~/Analysis\$ root
root [0] cerr << 1/20 << "\n"
0
(class ostream)4427552
root [1] cerr << 1/2 << "\n"
0
(class ostream)4427552
root [2] cerr << 10/2 << "\n"
5
(class ostream)4427552
root [3] cerr << 10/3 << "\n"
3
(class ostream)4427552
root [4] .q

==============================

I also lose all decimal points when using either strod() or atof() while converting strings like

1 -18.6 621117.3894 614207.7979 640695.2632
2 -18 730278.1551 716520.8021 748896.0643
3 -17.4 552596.9868 538294.754 563293.6959
4 -16.8 753505.4521 735623.5401 768908.1111
5 -16.2 723389.7822 704333.1016 736883.2339

I get:

1 -18.6 621117 614208 640695
2 -18 730278 716521 748896
3 -17.4 552597 538295 563294
4 -16.8 753505 735624 768908
5 -16.2 723390 704333 736883

I would like to keep all digits as they are used for direct comparisons rather than calculations.
** notice that -18.6 etc in column 2 converts just fine. Same way used in col 3,4,5 but it rounds.

``````root [0] cout << 1./20.
0.05(class ostream)140735281520400
root [1] ``````

```root [0] std::cout << 10./3. << std::endl; 3.33333 root [1] std::cout.precision(16); root [2] std::cout << 10./3. << std::endl; 3.333333333333333 root [3] std::cout << std::setprecision(10) << 10./3. << std::endl; 3.333333333``` See the std::ios_base::precision and std::setprecision descriptions.

Great, understood, thank you very much.
What about converting string read from a text file, into an >>exact<< Double_t?

So far, both strod() and atof() are rounding to integers.
data.txt (2.72 KB)

There is NO " >>exact<< Double_t". This is a “Floating point” value in a “Double-precision floating-point format”.
See especially “Floating point - Representable numbers, conversion and rounding”.

If you "#include ", both “strtod” and “atof” return a “double” value:
double strtod(const char *nptr, char **endptr);
double atof(const char *nptr);
(See “man strtod” and “man atof”.)

BTW. In your “ReadData.C”, you are missing something like “Double_t val;” (and “continue;// exit if file not found” should be changed into “return;// exit if file not found”). Try to precompile your macro using (inspect all reported warnings and errors):

This works great in VC++:

#include<stdlib.h>

``````   char* psz = "123456.7891";
char* pszOut = NULL;

printf(psz);
double d = strtod(psz, &pszOut);
``````

===================
Does NOT work with ROOT. WHY???
I use interpreter, I do not usually want to compile. The above suggestion about #include did not do me any good.

root [0] char psz = “123456.7891”;
root [1] char pszOut;
root [2] double d = strtod(psz, &pszOut);
root [3] d
(double)1.23456789099999995e+05
root [4] psz
(char
0x8f3dba4)"123456.7891"
root [5] pszOut
(char
0x8f3dbaf)""

BTW. If you "#include ", you should probably explicitly say “std::strtod(…)” and “std::atof(…)”.

Thank you very much! I really appreciate everyone’s help. Have a great day!