Using two different orders to calculate the negative power of n, the answers obtained by different orders are different, but the answers obtained by the two orders in actual calculation are the same?

  c++, question

In the book of numerical calculation methods, there is a topic that makes the range of calculating the negative power n of the sum n to be 1-10000 approximately equal to 1.644834.644725
However, I made n from 1 to 10,000 and n from 10,000 to 1 in the actual calculation, and the answer was 1.644834. there was no error. how did this happen?
Is it a code problem?

clipboard.png

clipboard.png

1.644834 1.644725 is calculated by float. Low precision.

Your code is calculated by double with high precision.

With double calculation, the data calculated twice will also be different.
But your code shows the same, because you output double data in float format.
If you output all decimals of double, the result will be different.
This is the kind of data.
1.64483407184805963
1.64483407184806518
Finally, it was different from the others.

If you use float, this is the data.
1.64483404159545898
1.64472532272338867

With c++, double calculation code

#include <iostream>
 int main()
 {
 double s=0;
 for(int i=10000;  i>0;  i--)
 {
 double t = 1.0/(i*i);
 s +=t;
 
 }
 std::cout.precision(17);
 std::cout<<std::fixed<<s<<std::endl;
 
 s = 0;
 for(int i=1;  i<=10000;  i++)
 {
 double t = 1.0/(i*i);
 s +=t;
 }
 std::cout.precision(17);
 std::cout<<std::fixed<<s<<std::endl;
 }

Single-precision floating-point numbers take up 4 bytes in the computer and are described by 32-bit binary.
Double-precision floating-point numbers occupy 8 bytes in the computer and are described by 64-bit binary.