Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Not able to find the square of the floating number ...

Not able to find the square of the floating number ...

Scheduled Pinned Locked Moved C / C++ / MFC
lounge
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mybm1
    wrote on last edited by
    #1

    Quote:

    #include
    #include

    int main(int argc,int *argv[])
    {
    srand(12345); // generate same random number anytime it run
    //srand((unsigned int)time NULL)
    int i,j;
    float a=0.2;
    double x;
    double sqrt(double);
    printf("========================================================\n");
    printf("Generated random number\t || Squaring of random number:\n");
    printf("=========================================================\n");
    for(i=0;i<100;i++)
    {
    double f= ((float)rand()/(float)(RAND_MAX))*a; //rand return integer value from 0 to RAND_MAX(system dependent)

      printf("%f\\t",f);
    

    // printf("%f\t",((float)rand()/(float)(RAND_MAX))*a); // rand return integer value from 0 to RAND_MAX(system dependent)
    printf("\t\t%f\n",x*f);
    // printf("\t\t%f\n",f*f);
    // printf("\t\t%f\n",(((float)rand()/(float)(RAND_MAX))*a) * ((float)rand()/(float)(RAND_MAX) *a));
    }

      	FILE \*fout;
      	//FILE \*square;
      	fout=fopen("R\_Number","w");  // output file in write mode
      	j=0;
      	
      	srand(12345);                // to giving value 12345 generate same random number wen anytime it run
      	
      	while(j<100)                 // condition
      	{ 
      		 double f= ((float)rand()/(float)(RAND\_MAX))\*a;
      			 fprintf(fout,"%f\\t\\t\\t%f\\n",f,f\*f);
      			 
      	  //	 fprintf(fout,"%f\\n",((float)rand()/(float)(RAND\_MAX))\*a); // print file in output mode
      		j++;
      	}
      	fclose(fout);  // close the file
    

    return 0;
    }

    double sqrt (double f)
    {
    double x, z, tempf;
    unsigned long *tfptr = ((unsigned long *)&tempf) + 1;
    tempf = f;
    *tfptr = (0xbfcdd90a - *tfptr)>>1;
    x = tempf;
    z = f*0.5;
    x = (1.5*x) - (x*x)*(x*z); //The more you make replicates of this statement
    //the higher the accuracy, here only 2 replicates are used
    x = (1.5*x) - (x*x)*(x*z);
    return x*f;
    }

    J C 2 Replies Last reply
    0
    • M mybm1

      Quote:

      #include
      #include

      int main(int argc,int *argv[])
      {
      srand(12345); // generate same random number anytime it run
      //srand((unsigned int)time NULL)
      int i,j;
      float a=0.2;
      double x;
      double sqrt(double);
      printf("========================================================\n");
      printf("Generated random number\t || Squaring of random number:\n");
      printf("=========================================================\n");
      for(i=0;i<100;i++)
      {
      double f= ((float)rand()/(float)(RAND_MAX))*a; //rand return integer value from 0 to RAND_MAX(system dependent)

        printf("%f\\t",f);
      

      // printf("%f\t",((float)rand()/(float)(RAND_MAX))*a); // rand return integer value from 0 to RAND_MAX(system dependent)
      printf("\t\t%f\n",x*f);
      // printf("\t\t%f\n",f*f);
      // printf("\t\t%f\n",(((float)rand()/(float)(RAND_MAX))*a) * ((float)rand()/(float)(RAND_MAX) *a));
      }

        	FILE \*fout;
        	//FILE \*square;
        	fout=fopen("R\_Number","w");  // output file in write mode
        	j=0;
        	
        	srand(12345);                // to giving value 12345 generate same random number wen anytime it run
        	
        	while(j<100)                 // condition
        	{ 
        		 double f= ((float)rand()/(float)(RAND\_MAX))\*a;
        			 fprintf(fout,"%f\\t\\t\\t%f\\n",f,f\*f);
        			 
        	  //	 fprintf(fout,"%f\\n",((float)rand()/(float)(RAND\_MAX))\*a); // print file in output mode
        		j++;
        	}
        	fclose(fout);  // close the file
      

      return 0;
      }

      double sqrt (double f)
      {
      double x, z, tempf;
      unsigned long *tfptr = ((unsigned long *)&tempf) + 1;
      tempf = f;
      *tfptr = (0xbfcdd90a - *tfptr)>>1;
      x = tempf;
      z = f*0.5;
      x = (1.5*x) - (x*x)*(x*z); //The more you make replicates of this statement
      //the higher the accuracy, here only 2 replicates are used
      x = (1.5*x) - (x*x)*(x*z);
      return x*f;
      }

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      Where did you find that obscure sqrt() function? I guess it was originally using float rather than double. Remove that and use the C standard library sqrt() function by including math.h.

      M 1 Reply Last reply
      0
      • J Jochen Arndt

        Where did you find that obscure sqrt() function? I guess it was originally using float rather than double. Remove that and use the C standard library sqrt() function by including math.h.

        M Offline
        M Offline
        mybm1
        wrote on last edited by
        #3

        actually i changed it to double n include math.h also but output seem same as before. source for square function

        J 1 Reply Last reply
        0
        • M mybm1

          Quote:

          #include
          #include

          int main(int argc,int *argv[])
          {
          srand(12345); // generate same random number anytime it run
          //srand((unsigned int)time NULL)
          int i,j;
          float a=0.2;
          double x;
          double sqrt(double);
          printf("========================================================\n");
          printf("Generated random number\t || Squaring of random number:\n");
          printf("=========================================================\n");
          for(i=0;i<100;i++)
          {
          double f= ((float)rand()/(float)(RAND_MAX))*a; //rand return integer value from 0 to RAND_MAX(system dependent)

            printf("%f\\t",f);
          

          // printf("%f\t",((float)rand()/(float)(RAND_MAX))*a); // rand return integer value from 0 to RAND_MAX(system dependent)
          printf("\t\t%f\n",x*f);
          // printf("\t\t%f\n",f*f);
          // printf("\t\t%f\n",(((float)rand()/(float)(RAND_MAX))*a) * ((float)rand()/(float)(RAND_MAX) *a));
          }

            	FILE \*fout;
            	//FILE \*square;
            	fout=fopen("R\_Number","w");  // output file in write mode
            	j=0;
            	
            	srand(12345);                // to giving value 12345 generate same random number wen anytime it run
            	
            	while(j<100)                 // condition
            	{ 
            		 double f= ((float)rand()/(float)(RAND\_MAX))\*a;
            			 fprintf(fout,"%f\\t\\t\\t%f\\n",f,f\*f);
            			 
            	  //	 fprintf(fout,"%f\\n",((float)rand()/(float)(RAND\_MAX))\*a); // print file in output mode
            		j++;
            	}
            	fclose(fout);  // close the file
          

          return 0;
          }

          double sqrt (double f)
          {
          double x, z, tempf;
          unsigned long *tfptr = ((unsigned long *)&tempf) + 1;
          tempf = f;
          *tfptr = (0xbfcdd90a - *tfptr)>>1;
          x = tempf;
          z = f*0.5;
          x = (1.5*x) - (x*x)*(x*z); //The more you make replicates of this statement
          //the higher the accuracy, here only 2 replicates are used
          x = (1.5*x) - (x*x)*(x*z);
          return x*f;
          }

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          Quote:

          double sqrt (double f) { double x, z, tempf; unsigned long *tfptr = ((unsigned long *)&tempf) + 1; tempf = f; *tfptr = (0xbfcdd90a - *tfptr)>>1; x = tempf; z = f*0.5; x = (1.5*x) - (x*x)*(x*z); //The more you make replicates of this statement //the higher the accuracy, here only 2 replicates are used x = (1.5*x) - (x*x)*(x*z); return x*f; }

          That is really messy! You are using uninitialized variables (that is garbage). It looks you want to implement the Babylonian method[^] but you do nothing for computing the initial guess. The following code is based on that very Wikipedia page.

          void initial_guess(double r, int *pa, int *pn)
          {
          *pn = 0;

          while ( r < 1.0)
          {
          r *= 100.0;
          --(*pn);
          }
          while ( r >= 100.0 )
          {
          r /= 100.0;
          ++(*pn);
          }

          *pa = (r < 10.0) ? 2 : 6;
          }

          double square_root(double r)
          {
          int a, i, n;
          initial_guess(r, &a, &n);
          double x=1.0;

          while (n < 0)
          {
          x/=10.0;
          ++n;
          }
          while(n > 0)
          {
          x *= 10.0;
          --n;
          }

          x *= a;

          for (i = 0; i<10; ++i) // 10 is the arbitrary number of iterations I chose
          {
          x = 0.5 * (x + r/x);
          }

          return x;
          }

          THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

          M 2 Replies Last reply
          0
          • M mybm1

            actually i changed it to double n include math.h also but output seem same as before. source for square function

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #5

            You must remove your version of the sqrt() function. Otherwise it will be used instead of the standard library version. Your version uses ugly programming that directly accesses a single precision (float) value. By changing the type to double it won't work any more. [EDIT]: Forget that. It is an approximation that might be from http://www.azillionmonkeys.com/qed/sqroot.html[^]. But such hardware dependant code should not be used.

            1 Reply Last reply
            0
            • C CPallini

              Quote:

              double sqrt (double f) { double x, z, tempf; unsigned long *tfptr = ((unsigned long *)&tempf) + 1; tempf = f; *tfptr = (0xbfcdd90a - *tfptr)>>1; x = tempf; z = f*0.5; x = (1.5*x) - (x*x)*(x*z); //The more you make replicates of this statement //the higher the accuracy, here only 2 replicates are used x = (1.5*x) - (x*x)*(x*z); return x*f; }

              That is really messy! You are using uninitialized variables (that is garbage). It looks you want to implement the Babylonian method[^] but you do nothing for computing the initial guess. The following code is based on that very Wikipedia page.

              void initial_guess(double r, int *pa, int *pn)
              {
              *pn = 0;

              while ( r < 1.0)
              {
              r *= 100.0;
              --(*pn);
              }
              while ( r >= 100.0 )
              {
              r /= 100.0;
              ++(*pn);
              }

              *pa = (r < 10.0) ? 2 : 6;
              }

              double square_root(double r)
              {
              int a, i, n;
              initial_guess(r, &a, &n);
              double x=1.0;

              while (n < 0)
              {
              x/=10.0;
              ++n;
              }
              while(n > 0)
              {
              x *= 10.0;
              --n;
              }

              x *= a;

              for (i = 0; i<10; ++i) // 10 is the arbitrary number of iterations I chose
              {
              x = 0.5 * (x + r/x);
              }

              return x;
              }

              THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

              M Offline
              M Offline
              mybm1
              wrote on last edited by
              #6

              thanks....

              C 1 Reply Last reply
              0
              • M mybm1

                thanks....

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                You are welcome.

                THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                1 Reply Last reply
                0
                • C CPallini

                  Quote:

                  double sqrt (double f) { double x, z, tempf; unsigned long *tfptr = ((unsigned long *)&tempf) + 1; tempf = f; *tfptr = (0xbfcdd90a - *tfptr)>>1; x = tempf; z = f*0.5; x = (1.5*x) - (x*x)*(x*z); //The more you make replicates of this statement //the higher the accuracy, here only 2 replicates are used x = (1.5*x) - (x*x)*(x*z); return x*f; }

                  That is really messy! You are using uninitialized variables (that is garbage). It looks you want to implement the Babylonian method[^] but you do nothing for computing the initial guess. The following code is based on that very Wikipedia page.

                  void initial_guess(double r, int *pa, int *pn)
                  {
                  *pn = 0;

                  while ( r < 1.0)
                  {
                  r *= 100.0;
                  --(*pn);
                  }
                  while ( r >= 100.0 )
                  {
                  r /= 100.0;
                  ++(*pn);
                  }

                  *pa = (r < 10.0) ? 2 : 6;
                  }

                  double square_root(double r)
                  {
                  int a, i, n;
                  initial_guess(r, &a, &n);
                  double x=1.0;

                  while (n < 0)
                  {
                  x/=10.0;
                  ++n;
                  }
                  while(n > 0)
                  {
                  x *= 10.0;
                  --n;
                  }

                  x *= a;

                  for (i = 0; i<10; ++i) // 10 is the arbitrary number of iterations I chose
                  {
                  x = 0.5 * (x + r/x);
                  }

                  return x;
                  }

                  THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                  M Offline
                  M Offline
                  mybm1
                  wrote on last edited by
                  #8

                  using

                  for(i=0;i<5;i++)
                  { //float y,sum;
                  float f= ((float)rand()/(float)(RAND_MAX))*a;
                  y=square(f);
                  sum +=y;
                  }
                  printf("\n summation of squared number is:=%f\t",sum);

                  gives output but not the exact answer.. any solution actually i want to find the summation of that squared float number..

                  C 1 Reply Last reply
                  0
                  • M mybm1

                    using

                    for(i=0;i<5;i++)
                    { //float y,sum;
                    float f= ((float)rand()/(float)(RAND_MAX))*a;
                    y=square(f);
                    sum +=y;
                    }
                    printf("\n summation of squared number is:=%f\t",sum);

                    gives output but not the exact answer.. any solution actually i want to find the summation of that squared float number..

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #9

                    How do you check the result?

                    THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                    M 1 Reply Last reply
                    0
                    • C CPallini

                      How do you check the result?

                      THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                      M Offline
                      M Offline
                      mybm1
                      wrote on last edited by
                      #10

                      Quote:

                      Generated random number || Squaring of random number: ========================================================= 0.035679 ----- 0.001273 0.079935 ----- 0.006390 0.033320 ----- 0.001110 0.042424 ----- 0.001800 0.012387 ----- 0.000153 summation of squared number is:=0.010346

                      where the actual summation should be 0.10726

                      C J 2 Replies Last reply
                      0
                      • M mybm1

                        Quote:

                        Generated random number || Squaring of random number: ========================================================= 0.035679 ----- 0.001273 0.079935 ----- 0.006390 0.033320 ----- 0.001110 0.042424 ----- 0.001800 0.012387 ----- 0.000153 summation of squared number is:=0.010346

                        where the actual summation should be 0.10726

                        C Offline
                        C Offline
                        CPallini
                        wrote on last edited by
                        #11

                        You are wrong, the output of the following program:

                        int main()
                        {
                        int i;
                        double sum = 0.0;
                        double a[] =
                        {
                        0.035679,
                        0.079935,
                        0.03332,
                        0.042424,
                        0.012387
                        };

                        for (i=0; i<sizeof(a)/sizeof(a[0]); ++i)
                        {
                        double sq = square_root(a[i]);
                        printf("a[%d] = %g, sqrt(a[%d])=%g\n", i, a[i], i, sq);
                        sum += sq;
                        }
                        printf("sum of square roots = %g\n", sum);
                        }

                        is

                        a[0] = 0.035679, sqrt(a[0])=0.188889
                        a[1] = 0.079935, sqrt(a[1])=0.282728
                        a[2] = 0.03332, sqrt(a[2])=0.182538
                        a[3] = 0.042424, sqrt(a[3])=0.205971
                        a[4] = 0.012387, sqrt(a[4])=0.111297
                        sum of square roots = 0.971422

                        That is correct (at least according to Excel :-) ).

                        THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                        M 1 Reply Last reply
                        0
                        • M mybm1

                          Quote:

                          Generated random number || Squaring of random number: ========================================================= 0.035679 ----- 0.001273 0.079935 ----- 0.006390 0.033320 ----- 0.001110 0.042424 ----- 0.001800 0.012387 ----- 0.000153 summation of squared number is:=0.010346

                          where the actual summation should be 0.10726

                          J Offline
                          J Offline
                          jeron1
                          wrote on last edited by
                          #12

                          Are you needing the sqaure or the square root? PS and the sum of your numbers is 0.010726.

                          "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

                          M 1 Reply Last reply
                          0
                          • C CPallini

                            You are wrong, the output of the following program:

                            int main()
                            {
                            int i;
                            double sum = 0.0;
                            double a[] =
                            {
                            0.035679,
                            0.079935,
                            0.03332,
                            0.042424,
                            0.012387
                            };

                            for (i=0; i<sizeof(a)/sizeof(a[0]); ++i)
                            {
                            double sq = square_root(a[i]);
                            printf("a[%d] = %g, sqrt(a[%d])=%g\n", i, a[i], i, sq);
                            sum += sq;
                            }
                            printf("sum of square roots = %g\n", sum);
                            }

                            is

                            a[0] = 0.035679, sqrt(a[0])=0.188889
                            a[1] = 0.079935, sqrt(a[1])=0.282728
                            a[2] = 0.03332, sqrt(a[2])=0.182538
                            a[3] = 0.042424, sqrt(a[3])=0.205971
                            a[4] = 0.012387, sqrt(a[4])=0.111297
                            sum of square roots = 0.971422

                            That is correct (at least according to Excel :-) ).

                            THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                            M Offline
                            M Offline
                            mybm1
                            wrote on last edited by
                            #13

                            this is different program its squaring of number not square root...

                            C 1 Reply Last reply
                            0
                            • J jeron1

                              Are you needing the sqaure or the square root? PS and the sum of your numbers is 0.010726.

                              "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

                              M Offline
                              M Offline
                              mybm1
                              wrote on last edited by
                              #14

                              Actually i need square but its solve i got it but i am not able to get the summation value of the square number correctly .plz help me out..

                              1 Reply Last reply
                              0
                              • M mybm1

                                this is different program its squaring of number not square root...

                                C Offline
                                C Offline
                                CPallini
                                wrote on last edited by
                                #15

                                How do you compute the squares? This program:

                                #include <stdio.h>
                                int main()
                                {
                                double a [] =
                                {
                                0.035679,
                                0.079935,
                                0.033320,
                                0.042424,
                                0.012387
                                };
                                double sum, square;
                                int n;

                                sum = 0.0;
                                for (n=0; n<sizeof(a)/sizeof(a[0]); ++n)
                                {
                                square = a[n]*a[n];
                                sum += square;
                                printf("%g ----> %g\n", a[n], square);
                                }

                                printf("sum of squares: %g\n", sum);

                                return 0;
                                }

                                gives:

                                0.035679 ----> 0.00127299
                                0.079935 ----> 0.0063896
                                0.03332 ----> 0.00111022
                                0.042424 ----> 0.0017998
                                0.012387 ----> 0.000153438
                                sum of squares: 0.0107261

                                THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups