QuestionMaker3 wrote: |
---|
Not very sure what I did to even deserved this...
|
You didn't do anything wrong :+) It's just that we seem to have a member who likes to report everyone. Because you had a low post count, once reported enough the account is automatically closed.
So back to the task at hand.
In
closed account 5a8Ym39o6 code, the
precision
was not something that had to be specified by the user. It was sent to the
round_d
function un-initialised, but then set inside the function. It is only there to provide some randomness for the input, for demonstration purposes. But you have real input, so you might not need that function at all.
When I compile that code I get these warnings, most of them came from
closed account's code:
List of warning options here:
http://www.cplusplus.com/forum/general/183731/#msg899203
../Rounding/main.cpp:28:17: warning: implicit conversion turns floating-point number into integer: 'double' to 'int' [-Wfloat-conversion]
precision = ceil(log10(b));
~ ^~~~~~~~~~~~~~
../Rounding/main.cpp:14:15: warning: implicit conversion loses integer precision: 'time_t' (aka 'long') to 'unsigned int' [-Wshorten-64-to-32]
srand(time(NULL));
~~~~~ ^~~~~~~~~~
../Rounding/main.cpp:46:52: warning: implicit conversion changes signedness: 'int' to 'unsigned long' [-Wsign-conversion]
if(dot != string::npos && ((out.length() - 1 - dot) >= 2) && (round = true))
~ ^~~
../Rounding/main.cpp:50:16: warning: implicit conversion changes signedness: 'int' to 'size_type' (aka 'unsigned long') [-Wsign-conversion]
out.resize(n);
~~~ ^
../Rounding/main.cpp:44:18: warning: implicit conversion loses integer precision: 'size_type' (aka 'unsigned long') to 'int' [-Wshorten-64-to-32]
int n, dot = out.find(".");
~~~ ^~~~~~~~~~~~~
../Rounding/main.cpp:46:12: warning: comparison of integers of different signs: 'int' and 'const size_type' (aka 'const unsigned long') [-Wsign-compare]
if(dot != string::npos && ((out.length() - 1 - dot) >= 2) && (round = true))
~~~ ^ ~~~~~~~~~~~~
../Rounding/main.cpp:48:14: warning: implicit conversion loses integer precision: 'size_type' (aka 'unsigned long') to 'int' [-Wshorten-64-to-32]
else n = out.length();
~ ^~~~~~~~~~~~
7 warnings generated. |
So I fixed up all the warnings, removed the side effect code, changed the types, renamed a variable, removed namespace std, moved the functions to after main.
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
|
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib>
double random_d(std::streamsize& input_precision);
double round_d(double d, std::streamsize input_precision);
int main()
{
std::streamsize input_precision = 0; // I like to avoid naming variables the same as something in the STL, despite knowing what std:: does
double d = 0.0;
const int test_cases = 25;
for(int N = 0; N < test_cases; ++N)
{
std::cout.setf(std::ios::fixed);
std::cout << "in : ";
// std::cin >> d;
// std::cout << "precision : ";
// std::cin >> input_precision;
d = random_d(input_precision);
std::cout.precision(input_precision);
std::cout << d << "\n";
d = round_d(d, input_precision);
std::cout.precision(1);
std::cout << "out : " << d << "\n\n";
}
}
// generates some random numbers with a random amount of precision
double random_d(std::streamsize& input_precision)
{
static bool need_random = true;
if(need_random) {
srand(static_cast<unsigned int>( time(NULL)) );
need_random = false;
}
int n = (rand() % 9) + 1;
if((rand() % 100) >= 85) n = 1;
double a = (rand() % 15000);
double b = 0;
for(int i = 0, j = 0; i < n; i++)
{
j = (rand() % 9) + 1;
b = b * 10 + j;
}
input_precision = static_cast<std::streamsize>( std::ceil( log10(b)) ) ;
a += (b / std::pow(10, input_precision));
return a;
}
// does the rounding
double round_d(double d, std::streamsize input_precision)
{
std::stringstream ss;
ss.setf(std::ios::fixed);
ss.precision(input_precision) ;
ss << d;
bool round = false;
std::string out = ss.str();
std::string::size_type n;
std::size_t dot = out.find(".");
if(dot != std::string::npos && (out.length() - 1 - dot) >= 2) {
round = true;
n = dot + 2;
}
else {
n = out.length();
}
out.resize(n);
ss.str(""); ss.clear();
ss.str(out);
ss >> d;
if(round) d += 0.1;
return d;
}
|
As I said you could alter this to not use the
random_d
function or any precision. Although you could have a variable which controls how many decimal places the output should have - possibly 2 or 3 or whatever number of dp.
Hope this helps :+)