Does 1 / x / (1 / x) always give 1 for floating point calculation if I use highest optimization of compilers (with simd) for x < 0 <= 1.0
and 1/x
doesn't overflow and becomes inf
or -inf
.
Or, I have to ensure that vector x
and vector y
in the following program have the same memory alignment?
Program:
#include <vector>
#include <iostream>
#include <cstdlib>
#include <time.h>
int main() {
int a = 1;
int length = 65537;
srand(time(NULL));
double c = 0;
while (c == 0) c = (double)(rand()) / (RAND_MAX);
std::cout << c << "\n";
std::vector<double> x(length, a);
std::vector<double> y(length, a);
for (auto& a: x) a /= c;
for (auto& a: y) a /= c;
for (int i = 0; i < length; ++i) x[i] /= y[i];
for (int i = 0; i < length; ++i) {
if (x[i] != 1) std::cout << "X";
}
std::cout<<"\n";
}
Result (no X):
0.22659