-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfisr.cpp
108 lines (79 loc) · 2.79 KB
/
fisr.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
float fast(float x) {
// An implementation of the fast inverse square root algorithm
float xhalf = 0.5f * x;
int i = *(int*)&x; //interpret the bits of x as an int
i = 0x5f3759df - (i >> 1); //Crazee constant minus half of i
x = *(float*)&i; //Re-interpret i as a float
x = x*(1.5f - (xhalf*x*x)); //One iteration of Newton's method for good
// measure
return x;
}
// int main(int argc, char * argv[]){
// /* Computes the inverse square root on a bunch of inputs.
// Usage:
// ./a.out outputfile float1 float2 float3 ...
// outputfile will contain all of the input numbers and their inverse
// square roots.
// This version of main uses c++ output streams to write the output. I'm
// not crazy about how the formatting for this type of output works.
// */
// int count = argc - 2;
// char * output = argv[1];
// ofstream fout;
// fout.open(output);
// fout<<setw(10)<<"Input"<<setw(10)<<"Output"<<endl;
// fout<<setiosflags(ios::fixed);
// for (int i = 0; i < count; i++) {
// float input = atof(argv[i+2]);
// fout<<setprecision(2)<<setw(10)<<input
// <<setprecision(7)<<setw(10)<<fast(input)<<endl;
// }
// fout.close();
// return 0;
// }
// int main(int argc, char *argv[]) {
// /* Computes the inverse square root on a bunch of inputs.
// Usage:
// ./a.out outputfile float1 float2 float3 ...
// outputfile will contain all of the input numbers and their inverse
// square roots.
// This version of main uses normal c format strings and fprintf for the
// output. I like it better because it's not totally stupid.
// */
// int count = argc - 2;
// char * output = argv[1];
// FILE * ofile;
// ofile = fopen(output,"w");
// fprintf(ofile, "%10s%10s\n", "Input","Output");
// for (int i = 0; i < count; i++) {
// float input = atof(argv[i+2]);
// fprintf(ofile,"%10.2f%10.7f\n",input,fast(input));
// }
// fclose(ofile);
// return 0;
// }
int main(int argc, char * argv[]) {
/* Computes the inverse square root on a bunch of inputs.
Usage:
./a.out
This version of main does not output anything. Instead it asks the
user to keep providing values to compute until the use inputs 0.
*/
float x;
char * input;
printf("Enter you want to find the inv. sqrt. of ('q' to quit)\n");
scanf("%f", &x);
while (x) {
// x = (float) atof(input);
// printf("%10.2f\n",x);
printf("%10.2f\n",fast(x));
scanf("%f", &x);
}
return 0;
}