/
sqrtwcet.c
95 lines (86 loc) · 3.99 KB
/
sqrtwcet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* MDH WCET BENCHMARK SUITE. */
/* 2012/09/28, Jan Gustafsson <jan.gustafsson@mdh.se>
* Changes:
* - This program redefines the C standard function sqrt. Therefore, this function has been renamed to sqrtfcn.
* - qrt.c:79:15: warning: explicitly assigning a variable of type 'float' to itself: fixed
*/
/*************************************************************************/
/* */
/* SNU-RT Benchmark Suite for Worst Case Timing Analysis */
/* ===================================================== */
/* Collected and Modified by S.-S. Lim */
/* sslim@archi.snu.ac.kr */
/* Real-Time Research Group */
/* Seoul National University */
/* */
/* */
/* < Features > - restrictions for our experimental environment */
/* */
/* 1. Completely structured. */
/* - There are no unconditional jumps. */
/* - There are no exit from loop bodies. */
/* (There are no 'break' or 'return' in loop bodies) */
/* 2. No 'switch' statements. */
/* 3. No 'do..while' statements. */
/* 4. Expressions are restricted. */
/* - There are no multiple expressions joined by 'or', */
/* 'and' operations. */
/* 5. No library calls. */
/* - All the functions needed are implemented in the */
/* source file. */
/* */
/* */
/*************************************************************************/
/* */
/* FILE: sqrt.c */
/* SOURCE : Public Domain Code */
/* */
/* DESCRIPTION : */
/* */
/* Square root function implemented by Taylor series. */
/* */
/* REMARK : */
/* */
/* EXECUTION TIME : */
/* */
/* */
/*************************************************************************/
#include <stdio.h>
float fabs1(float x)
{
if (x < 0)
return -x;
else
return x;
}
float sqrtfcn(float val)
{
float x = val/10;
float dx;
double diff;
double min_tol = 0.00001;
int i, flag;
flag = 0;
if (val == 0 )
x = 0;
else {
for (i=1;i<20;i++)
{
if (!flag) {
dx = (val - (x*x)) / (2.0 * x);
x = x + dx;
diff = val - (x*x);
if (fabs1(diff) <= min_tol)
flag = 1;
}
else {} /* JG */
/* x =x; */
}
}
return (x);
}
int main(){
double a;
scanf("%lf",&a);
printf("%f\n",sqrtfcn(a));
}