/**************************************************************** Copyright (C) : University of Michigan Project: NERS 544 - Class example File: conv.cpp Purpose: Conversion from integers to floats Solution to Chapter 3, Problem 1 Compiler: g++ -ansi -pedantic -Wconversion Programmer: Alex Bielajew Revision Date: 09/13/2016 The conversion is via the statement: double f = 0.5 + ixx[i]*2.328306436538696e-10; where ixx is the random int, and the constant multiplying it is 1/2^(32) evaluated in Matlab Note: The low order bits of the double is very unrandom! ****************************************************************/ #include #include #include #include #include using namespace std; int main(void) { vector ixx; bool verbose = 1; int Min = INT_MIN; if (verbose) {cout << "min = " << Min << endl;} int Max = INT_MAX; if (verbose) {cout << "max = " << Max << endl;} ixx.push_back(Min); ixx.push_back(Min + 1); ixx.push_back(Min + 2); ixx.push_back(-2); ixx.push_back(-1); ixx.push_back(0); ixx.push_back(1); ixx.push_back(2); ixx.push_back(Max - 2); ixx.push_back(Max - 1); ixx.push_back(Max); for (int i = 0; i < ixx.size(); i = i + 1) { double f = 0.5 + ixx[i]*2.328306436538696e-10; cout << "int = " << ixx[i] << " real = " << setprecision(16) << f << " log(real) = " << setprecision(16) << log(f) << endl; } return 0; }