0

Problem 1:

I have tried to print the value of PI upto n-th digit after decimal pointer in C++. But in MinGW after a certain-th digit the rest of them are showing 0s. But this is not happening with Cygwin.

Even for printing Armstrong numbers upto a certain range, 153 is not shown in output when executed through MinGW. But the results are absolutely fine with Cygwin.

Can anyone please tell me why there is such error in MinGW.

Code used for PI(n th):

#include "bits/stdc++.h"
#include <iostream>
#include <cstdlib>
using namespace std;
 
void printValueOfPi(int N)
{
    // Find value of pi upto
    // using acos() function
    double pi = 2 * acos(0.0);
 
    // Print value of pi upto
    // N decimal places
    printf("%.*lf\n", N, pi);
}

int main()
{
    int N = 45;
 
    // Function that prints
    // the value of pi
    printValueOfPi(N);
    return 0;
}

Output with MinGW: enter image description here Output with Cygwin: enter image description here

Code for Armstrong:

#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;

#define cls system("cls");

int main(){
    // This is a template code

    int range,arm=0,digits=0;
    cin>>range;
    for (int i = 1; i <= range; i++)
    {
        int n=i;
        digits=arm=0;
        while(n!=0){
            n/=10;
            digits++;
        }
        n=i;
        while(n!=0){
            arm+=(pow((n%10),digits));
            n/=10;
        }
        if(arm==i){
            cout<<i<<endl;
        }
    }
    

    return EXIT_SUCCESS;
}

Output with MinGW: enter image description here Output with Cygwin: enter image description here

Problem 2:

Not only that I have another problem with Cygwin. If I copy the question test cases directly in the VS Code terminal or PowerShell and use to move with arrow keys of keyboard the shell start behaving like a text editor and if I press enter after that then the program just terminated throwing some error. If I avoid the arrow keys the result is fine.

Problem 2 details: https://youtu.be/x8RhxeGUroc

  • 1
    Take a closer look at the Cygwin output for the digits of Pi. It prints more digits than MinGW, but those digits... aren't actually correct. Presumably that's the point where you've gone beyond `double` precision and need a different way of calculating those digits. – Nathan Pierson Apr 05 '22 at 20:20
  • But also, a couple suggestions: One question per question. Include code as _text_, not images of code, so it can be searched and copied and pasted into IDEs for debugging. Ask questions _in your question_, instead of as a link to some offsite video. [Why I should not #include ](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). [Why using namespace std; is considered bad practice.](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Nathan Pierson Apr 05 '22 at 20:22
  • Are you seriously trying to calculate 45 digits using floating point arithmetic using `acos` and hope that the result is exact to the individual digits??? I guess that internally (even if it is on microcode) the acos needs to be calculated by summing up some series, and you can't expect this to be precise with floats. – user1934428 Apr 07 '22 at 07:12
  • @NathanPierson Then what about the armstrong. The result is incorrect for MinGW and fine in Cygwin. Even MinGW does not show 153 as armstrong when taken only one no not in range. – Archisman Karmakar Apr 07 '22 at 19:23
  • @NathanPierson using long double also does not change it. 3.141592653589793 this is the matched portion in case of both long double and double. I guess the problem is with acos. But leaving pi what problem is there with the armstrong? – Archisman Karmakar Apr 07 '22 at 19:30
  • I can't reproduce both of your issues on MSYS2 MinGW (on Linux). What mingw distribution are you using? Where did you find it? – HolyBlackCat Apr 10 '22 at 18:42
  • Note that using `pow` for integral calculations is not the best idea in any case, because of precision issues like this one. Use one of the algorithms here: https://en.wikipedia.org/wiki/Exponentiation_by_squaring – HolyBlackCat Apr 10 '22 at 18:47
  • @HolyBlackCat I am using MinGW downloaded from Source Forge, since I am doing it in Windows. – Archisman Karmakar Feb 05 '23 at 05:43
  • There are several different MinGW versions on Windows. [Guide on installing MSYS2](https://stackoverflow.com/q/30069830/2752075). – HolyBlackCat Feb 05 '23 at 05:47

0 Answers0