Ad

Actual accuracy check. Added tests.

Code
Diff
  • #include <math.h>
    
    
    double custom_sqrt (int a,int accuracy=20) {
      double fIntegerPart;
      double fFloatingPart = modf((double)sqrt(a), &fIntegerPart);
      
      double fTempFloat = fFloatingPart * pow(10, accuracy);
      double fTempInt;
      fTempFloat = modf(fTempFloat, &fTempInt);
      
      fTempFloat /= pow(10, accuracy);
      
      fFloatingPart -= fTempFloat;
      
      return fIntegerPart+fFloatingPart;
    }
    • double sqrt (int a,int accuracy=20) {
    • double out = a;
    • for(int i=0;i<accuracy;i++) {
    • out = (out+a/out)/2;
    • }
    • return out;
    • #include <math.h>
    • double custom_sqrt (int a,int accuracy=20) {
    • double fIntegerPart;
    • double fFloatingPart = modf((double)sqrt(a), &fIntegerPart);
    • double fTempFloat = fFloatingPart * pow(10, accuracy);
    • double fTempInt;
    • fTempFloat = modf(fTempFloat, &fTempInt);
    • fTempFloat /= pow(10, accuracy);
    • fFloatingPart -= fTempFloat;
    • return fIntegerPart+fFloatingPart;
    • }
Fundamentals

Removed iostream and cout. Because using printf instead of cout is a lot faster and optimized for the compiler.

Proof : https://godbolt.org/g/qqNUwM

Code
Diff
  • #include <stdio.h>
    
    int helloCplusplus(){
      printf("Hello Cplusplus\n");
      return 0;
    }
    • #include <iostream>
    • #include <stdio.h>
    • using namespace std;
    • int helloCplusplus(){
    • cout << "Hello Cplusplus" << '\n';
    • printf("Hello Cplusplus\n");
    • return 0;
    • }