~ubuntu-branches/ubuntu/dapper/cmake/dapper-backports

« back to all changes in this revision

Viewing changes to Tests/Tutorial/Step5/MathFunctions/mysqrt.cxx

  • Committer: Bazaar Package Importer
  • Author(s): A. Maitland Bottoms
  • Date: 2006-01-08 10:48:14 UTC
  • mfrom: (1.3.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060108104814-jmy1r2fydpohpc1g
Tags: 2.2.3-1
* New upstream release (Closes: #338324)
* support GNU/kFreeBSD in cmake (Closes: #340764)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include "MathFunctions.h"
 
3
#include "TutorialConfig.h"
 
4
 
 
5
// include the generated table
 
6
#include "Table.h"
 
7
 
 
8
#include <math.h>
 
9
 
 
10
// a hack square root calculation using simple operations
 
11
double mysqrt(double x)
 
12
{
 
13
  if (x <= 0)
 
14
    {
 
15
    return 0;
 
16
    }
 
17
  
 
18
  double result;
 
19
 
 
20
  // if we have both log and exp then use them
 
21
  double delta;  
 
22
 
 
23
  // use the table to help find an initial value
 
24
  result = x;
 
25
  if (x >= 1 && x < 10)
 
26
    {
 
27
    result = sqrtTable[static_cast<int>(x)];
 
28
    }
 
29
 
 
30
  // do ten iterations
 
31
  int i;
 
32
  for (i = 0; i < 10; ++i)
 
33
    {
 
34
    if (result <= 0)
 
35
      {
 
36
      result = 0.1;
 
37
      }
 
38
    delta = x - (result*result);
 
39
    result = result + 0.5*delta/result;
 
40
    fprintf(stdout,"Computing sqrt of %g to be %g\n",x,result);
 
41
    }
 
42
 
 
43
  return result;
 
44
}