UniTensor class's "<<operator" member method changes precision of cout
Summary
The <<operator
for the UniTensor class alters the global default precision for cout
. I discovered bug wile working with @pcs155251.
What is the current bug behavior?
This bug occurs using the 14 Feb version of the master branch (ea666188), but I don't believe the relevant files (below) have been updated since then.
We fist found it on my mac using the apple C compilers, but @pcs155251 then confirmed that it occurred on another machine.
When using the <<
method in the UniTensor class to write to screen, such as
cout << some_nonscalar_unitensor_object;
afterwards the default precision for cout
is left altered in a way that could cause unexpected behavior for a user. For example:
cout << 1.005 << endl;
would round the display the float as a single digit fixed point number:
1
What is the expected correct behavior?
Ideally the <<operator
method should not alter the global environment in any unexpected ways.
Minimal working example
I wrote up a minimal working example that shows this bug in action. I've attached a file mwe.cpp which, when compiled with the attached Makefile should produce the error.
Here is the output of that program (also attached as example_output.txt )
---->First we print a number '1.995':
---->1.995
---->Then we print Gs to screen using cout:
***************** Gs *****************
REAL(0)
____________
| |
0___|2 2|___1
| |
|____________|
================BONDS===============
IN : (U1 = 0, P = 0, 0)|2, Dim = 2
OUT: (U1 = 0, P = 0, 0)|2, Dim = 2
===============BLOCKS===============
--- (U1 = 0, P = 0, 0):
2 x 2 = 4 [ Real ElemNum: 0 ], REAL
[
0.9501 -0.0692
-0.1926 -0.6762
];
Total elemNum: 4
====================================
---->And now we print the same number again, but it is rounded up to 2:
---->2
---->This happens in reverse as well. If we contract Gs with itself (generating a scalar) and cout the result:
Scalar: 1.4018541104
---->And now we print the same number a third time it displays correctly:
---->1.9950000000
---->although with different precision than initially
Possible fixes
Here I'm not sure. I believe the bug occurs around line 669 in UniTensor.h, but it may also be present in other implementations of the <<operator
in other uni10 classes.