-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflconversionbin.cpp
More file actions
63 lines (57 loc) · 2.01 KB
/
flconversionbin.cpp
File metadata and controls
63 lines (57 loc) · 2.01 KB
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
/* CmSc 117 Exercise 2
* Gisselle Derije & Benjamin Julien Roque
* 15 October 2020
*/
#include <iomanip>
#include <cmath>
#include <utility>
#include <limits>
#include "binary64.hpp"
double chop(double x, int p ) {
double shift = pow(2.0, p);
x *= shift;
x = (x > 0) ? floor(x) : ceil(x);
return x/shift;
}
double round(double x, int p) {
double shift = pow(2.0, p);
x *= shift;
x = (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
return x/shift;
}
void printChopped(double x) {
std::cout << "p\tChopped Float\t\tRelative Error\t\tRatio: RelErr/PredErr" << std::endl;
for(int p = 0; p <= 51; p++){
std::cout << std::fixed << std::setprecision(14);
//Chopped FLoat
std::cout << std::dec << p + 1 << "\t" << chop(x, p - 3);
std::cout << std::scientific << std::setprecision(10);
//Relative Error
std::cout << "\t" << std::abs(chop(x, p - 3) - x) / std::abs(x);
//Ratio: RellError/PredError
std::cout << "\t" << (std::abs(chop(x, p - 3) - x))/(pow(2, -p)*(std::abs(x))) << std::endl;
}
}
void printRounded(double x) {
//rounding
std::cout << std::endl << "p\tRounded Float\t\tRelative Error\t\tRatio: RelErr/PredErr" << std::endl;
for(int p = 0; p <= 51; p++){
std::cout << std::fixed << std::setprecision(14);
//Chopped Float
std::cout << std::dec << p + 1 << " " << round(x, p - 3);
std::cout << "\t" << std::scientific << std::setprecision(10);
//Relative Error
std::cout << std::abs((round(x, p - 3) - x) / x);
//Ratio: RelError/PredError
std::cout << "\t" << (std::abs((round(x, p - 3) - x)))/(pow(2, - p - 1)*std::abs(x)) << std::endl;
}
}
int main(){
double x = -10.0 * M_PI;
std::cout << "USING BINARY CONVERSION:" << std::endl;
std::cout << std::scientific << std::setprecision(15);
std::cout << Binary64(x) << std::endl;
printChopped(x);
printRounded(x);
return 0;
}