Advertisement
Guest User

Untitled

a guest
Jun 24th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. float raiz1(float n) {
  5.     int i = 0;
  6.     float aux = 1.0;
  7.  
  8.     for (i = 0; i < 10; ++i) {
  9.         aux = aux - ((aux * aux) - n) / (2 * aux);
  10.     }
  11.  
  12.     return aux;
  13. }
  14.  
  15. typedef unsigned short u16;
  16.  
  17. #define P 3
  18.  
  19. u16 raiz2(u16 n) {
  20.     int i = 0;
  21.     u16 aux = (1 << P); /* 3 casas (binarias) de precisao */
  22.     n <<= P;
  23.  
  24.     for (i = 0; i < 10; ++i) {
  25.         aux = aux - ((((aux * aux) >> P) - n) << P) / (((2 << P) * aux) >> P);
  26.     }
  27.  
  28.     return aux;
  29. }
  30.  
  31. int main() {
  32.     float a = 56;
  33.  
  34.     printf("%f %f %f\n", sqrt(a), raiz1(a), raiz2(56) / pow(2.0, P));
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement