Przejdź do zawartości

C/sqrt

Z Wikibooks, biblioteki wolnych podręczników.
< C

Indeks alfabetyczny funkcji z biblioteki standardowej

Wykres funkcji y = sqrt(x)

Wersje

[edytuj]

w pliku math.h :

 #include <math.h> : 
 double 	sqrt (double x)
 long 	sqrtl (long double x)
 float 	sqrtf (float x)

W pliku complex.h :

 #include <complex.h>
 complex 	sqrt (complex x)
 double complex 	csqrt (double complex x)
 float complex 	csqrtf (float complex x)
 long double complex 	csqrtl (long double complex x)

Przykład użycia

[edytuj]

podstawowe użycie

[edytuj]
#include <stdio.h> // printf
#include <math.h> // sqrt


int main(){

  // 
  double      d = 2.0;
  long double l = 2.0L;
  
  // wynik
  double      sd;
  long double sl; 
  
  sd = sqrt (d);
  sl = sqrtl(l);
  
  printf(" sqrt(%.16f)  = %.16f  \n", d, sd);
  printf(" sqrt(%.18LF) = %.18LF \n", l, sl);

  return 0; 
}

Kompilacja:

  gcc s.c -lm -Wall

Uruchomienie :

 ./a.out

Wynik :

  sqrt(2.0000000000000000)  = 1.4142135623730951  
  sqrt(2.000000000000000000) = 1.414213562373095049


Porównanie z wynikami wyższej precyzji:

  1.41421 35623 73095 04880 168872420969807856967187537694807317667973799 // truncated to 65 decimal places from wikipedia 
  1.41421 35623 73095 1  // using sqrt = double precision: error on 16 position after decinal point
  1.41421 35623 73095 049  // using sqrt = usung long double precision: error on 18 position after decimal point

zakresy

[edytuj]
/*
  gcc s.c -Wall -lm
  
*/  
#include <math.h> /* isnormal */
#include <float.h>//DBL_MIN
#include <stdio.h>

int main ()
{
  double x = 2.0;
  int i=0;

  
 
  
 while (1)
    {
     
      
      printf ("i = %3d ; x= %.16lf so  ", i, x);
      //
      if (x<DBL_MIN) {printf ("x < DBL_MIN and "); break; }
          else printf ("x > DBL_MIN  and  ");
      //
      if (isnormal(x)) printf ("x is normal and ");
          else {printf ("x is  subnormal \n "); break;}
      //
      if (x==1.0) {printf ("equal to 1.0\n"); break;}
      else printf ("not equal to 1.0\n");
      
      //
       x=sqrt(x);
       i+=1;
    }

return 0;
}

Wynik:

./a.out
i =   0 ; x= 2.0000000000000000 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =   1 ; x= 1.4142135623730951 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =   2 ; x= 1.1892071150027210 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =   3 ; x= 1.0905077326652577 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =   4 ; x= 1.0442737824274138 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =   5 ; x= 1.0218971486541166 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =   6 ; x= 1.0108892860517005 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =   7 ; x= 1.0054299011128027 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =   8 ; x= 1.0027112750502025 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =   9 ; x= 1.0013547198921082 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  10 ; x= 1.0006771306930664 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  11 ; x= 1.0003385080526823 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  12 ; x= 1.0001692397053021 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  13 ; x= 1.0000846162726942 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  14 ; x= 1.0000423072413958 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  15 ; x= 1.0000211533969647 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  16 ; x= 1.0000105766425498 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  17 ; x= 1.0000052883072919 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  18 ; x= 1.0000026441501502 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  19 ; x= 1.0000013220742012 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  20 ; x= 1.0000006610368821 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  21 ; x= 1.0000003305183864 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  22 ; x= 1.0000001652591797 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  23 ; x= 1.0000000826295865 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  24 ; x= 1.0000000413147925 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  25 ; x= 1.0000000206573960 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  26 ; x= 1.0000000103286979 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  27 ; x= 1.0000000051643489 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  28 ; x= 1.0000000025821745 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  29 ; x= 1.0000000012910872 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  30 ; x= 1.0000000006455436 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  31 ; x= 1.0000000003227718 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  32 ; x= 1.0000000001613858 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  33 ; x= 1.0000000000806928 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  34 ; x= 1.0000000000403464 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  35 ; x= 1.0000000000201732 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  36 ; x= 1.0000000000100866 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  37 ; x= 1.0000000000050433 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  38 ; x= 1.0000000000025215 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  39 ; x= 1.0000000000012608 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  40 ; x= 1.0000000000006304 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  41 ; x= 1.0000000000003151 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  42 ; x= 1.0000000000001574 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  43 ; x= 1.0000000000000786 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  44 ; x= 1.0000000000000393 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  45 ; x= 1.0000000000000195 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  46 ; x= 1.0000000000000098 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  47 ; x= 1.0000000000000049 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  48 ; x= 1.0000000000000024 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  49 ; x= 1.0000000000000011 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  50 ; x= 1.0000000000000004 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  51 ; x= 1.0000000000000002 so  x > DBL_MIN  and  x is normal and not equal to 1.0
i =  52 ; x= 1.0000000000000000 so  x > DBL_MIN  and  x is normal and equal to 1.0


Porównaj z

[edytuj]
  • arb_sqrt - dowolna precyzja
  • csqrt dla liczb zespolonych

Źródła

[edytuj]