Advertisement
paulogp

sine

Jul 27th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. // apple xcode
  2. // paulogp
  3.  
  4. /* sine */
  5. /* computes sine using very simple floating arithmetic. */
  6.  
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include <stdio.h>
  10.  
  11. static char *float_2_ascii (float the_num);
  12. float fix_float (float the_num);
  13. float factorial (float the_num);
  14.  
  15. static char *float_2_ascii (float the_num)
  16. {
  17.     static char the_result[15]; // place to put the number
  18.    
  19.     sprintf(the_result, "%8.3E", the_num);
  20.    
  21.     return (the_result);
  22. }
  23.  
  24. float fix_float (float the_num)
  25. {
  26.     float the_result; // result of the conversion
  27.     char the_ascii[15]; // ascii version of number
  28.    
  29.     sprintf(the_ascii, "%8.4e", the_num);
  30.     sscanf(the_ascii, "%e", &the_result);
  31.    
  32.     return (the_result);
  33. }
  34.  
  35. float factorial (float the_num)
  36. {
  37.     if (the_num <= 1.0)
  38.     {
  39.         return (the_num);
  40.     }
  41.     else
  42.     {
  43.         return (the_num *factorial(the_num - 1.0));
  44.     }
  45. }
  46.  
  47. int main (int argc, const char * argv[])
  48. {
  49.     float the_test_value = 1.4;
  50.    
  51.     float the_total; // total of series so far
  52.     float new_total; // newer version of total
  53.     float term_top; // top part of term
  54.     float term_bottom; // bottom of current term
  55.     float the_term; // current term
  56.     float the_exp; // exponent of current term
  57.     float the_sign; // +1 or -1 (changes on each term)
  58.     float the_value; // value of the argument to sin
  59.     int the_index; // index for countin terms
  60.    
  61.     printf("calculating...\n\n");
  62.    
  63.     the_value = fix_float(the_test_value);
  64.    
  65.     the_total = 0.0;
  66.     the_exp = 1.0;
  67.     the_sign = 1.0;
  68.    
  69.     for (the_index = 0;; ++the_index)
  70.     {
  71.         term_top = fix_float(pow(the_value, the_exp));
  72.         term_bottom = fix_float(factorial(the_exp));
  73.         the_term = fix_float(term_top / term_bottom);
  74.        
  75.         printf("x++%d   %s\n", (int)exp, float_2_ascii(term_top));
  76.         printf("%d!     %s\n", (int)exp, float_2_ascii(term_bottom));
  77.         printf("x**%d/%d! %s\n", (int)exp, (int)exp, float_2_ascii(the_term));
  78.         printf("\n");
  79.        
  80.         new_total = fix_float(the_total + the_sign * the_term);
  81.        
  82.         if (new_total == the_total)
  83.         {
  84.             break;
  85.         }
  86.        
  87.         the_total = new_total;
  88.         the_sign = -the_sign;
  89.         the_exp = the_exp + 2.0;
  90.        
  91.         printf(" total = %s\n", float_2_ascii(the_total));
  92.         printf("\n");
  93.     }
  94.    
  95.     printf("%d term computed\n", the_index + 1);
  96.     printf("rad sin(%s) = %s\n", float_2_ascii(the_value), float_2_ascii(the_total));
  97.     printf("Actual sin(%G) = %G\n", the_test_value, sin(the_test_value));
  98.    
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement