How can I prevent a number from being rounded when trading?

Viewed 69

I am trying to get random numbers between 10 and 20 with decimals, first I take the integer part and then the decimal part, this so that the probabilities of the integer part have a greater possibility of varying, and then the decimal part, but when adding the two parts begins to be rounded, I don't want it to be rounded because I want to get an exact number of decimal places, in this case 7 decimal places.

#include<iostream>
#include<math.h>
double random_decimal(int inicio, int fin, int numero_decimales)
{
    double int_part = (rand() % (inicio + 1 - fin) + inicio);
    int num = pow(10, numero_decimales);
    double decimal_part = (double)(rand() % (1 - num)) / num;
    cout << "Int: "<< int_part << " Dec: "<< decimal_part << " Sum: "<< int_part + decimal_part << endl;
    return (double)(int_part + decimal_part);
}
int main()
{
    int ed;
    double ps, estat;
    for(int i = 0; i < 10; i++)
    {
        double x = random_decimal(10, 20, 7);
    }
}
2 Answers

For starters, don't use rand() if you can use one of the C++11 random classes instead: https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

For example:

std::default_random_engine generator;
std::uniform_real_distribution<double> distribution(10.0,20.0);

As far as "rounding" or "exact number of decimal places" - I wouldn't worry about that. Specifically:

https://peter.bloomfield.online/decimal-places-in-a-floating-point-number/

Significant figures != decimal places

It’s easy to make the mistake of thinking that the code above answers our decimal places question. However, consider the following numbers:

0.12345123451234512345
0.00000000000000012345

Both of them are written with 20 decimal places. However, the second one would only require 5 significant figures (or the equivalent of 5 decimal digits in the mantissa). The extra leading zeroes after the decimal point can be represented by simply decreasing the exponent (i.e. making it more negative), leaving the entire mantissa available for precision.

Read the rest of the article for more details. But I suspect "rounding" (as I think you're using the term) probably isn't an issue for your particular application.

Your numbers are only being rounded when you are printing them:

$ cat que.cpp 
#include <iomanip>
#include <iostream>
#include <math.h>

using namespace std;

double random_decimal(int inicio, int fin, int numero_decimales) {
  double int_part = (rand() % (inicio + 1 - fin) + inicio);
  int num = pow(10, numero_decimales);
  double decimal_part = (double)(rand() % (1 - num)) / num;

  cout << setprecision(numero_decimales) << fixed;

  cout << "Int: " << int_part << " Dec: " << decimal_part
       << " Sum: " << int_part + decimal_part << endl;
  return (double)(int_part + decimal_part);
}
int main() {
  int ed;
  double ps, estat;
  for (int i = 0; i < 10; i++) {
    double x = random_decimal(10, 20, 7);
  }
}

$ g++ -o que que.cpp && ./que 
Int: 11.0000000 Dec: 0.6930970 Sum: 11.6930970
Int: 10.0000000 Dec: 0.4637086 Sum: 10.4637086
Int: 15.0000000 Dec: 0.4238377 Sum: 15.4238377
Int: 11.0000000 Dec: 0.9760656 Sum: 11.9760656
Int: 16.0000000 Dec: 0.9641539 Sum: 16.9641539
Int: 15.0000000 Dec: 0.0490162 Sum: 15.0490162
Int: 15.0000000 Dec: 0.2520169 Sum: 15.2520169
Int: 15.0000000 Dec: 0.7514122 Sum: 15.7514122
Int: 16.0000000 Dec: 0.0383580 Sum: 16.0383580
Int: 17.0000000 Dec: 0.3455866 Sum: 17.3455866
Related