steve_bank
Diabetic retinopathy and poor eyesight. Typos ...
Found a better way to create fractional random numbers using rand() on the net.
First floating point numbers. For a 64 bit number there are a fixed number of digits. Spread between the integer and fractional part of the number. If you run the first code you will see the frictional digiits decrease as the integer digits increase. There is plenty of information on floating point numbers on the net.
I never figured out exactly how print() works. It looks like there is a buffer the end of which can be printed. A simple test is to assign a sequence of decimal digits to a a number and see how many are correctly, but that does not give a complete picture. The smallest number is 1/nbits the lsb. Fractional numbers will be exact only when in multiples of the lsb. So when entering fractional numbers and then displaying it may not exactly match. Quantization errors.
So, when creating float random numbers you may want to set the number of fractional digits, The second code rounds at a specified number of digits. When doing chain calculations sometimes ti helps to round intermediate values to avoid underflow. You can end up with meaningless results at times. Carrying more digits than the digits of data often has little value. A false sense of accuracy and precision.
First floating point numbers. For a 64 bit number there are a fixed number of digits. Spread between the integer and fractional part of the number. If you run the first code you will see the frictional digiits decrease as the integer digits increase. There is plenty of information on floating point numbers on the net.
I never figured out exactly how print() works. It looks like there is a buffer the end of which can be printed. A simple test is to assign a sequence of decimal digits to a a number and see how many are correctly, but that does not give a complete picture. The smallest number is 1/nbits the lsb. Fractional numbers will be exact only when in multiples of the lsb. So when entering fractional numbers and then displaying it may not exactly match. Quantization errors.
So, when creating float random numbers you may want to set the number of fractional digits, The second code rounds at a specified number of digits. When doing chain calculations sometimes ti helps to round intermediate values to avoid underflow. You can end up with meaningless results at times. Carrying more digits than the digits of data often has little value. A false sense of accuracy and precision.
Code:
#include <iostream>
#include "math.h"
#include "stdlib.h"
#include "stdio.h"
#include<iomanip>
using namespace std;
#define FLOOR 1
#define CEIL 2
#define ROUND 3
double rand_float(int flag,int scale_factor, int dec_places,double lo_num ) {
//random float between lo_num and scale_fctor
double rfrac = 0.;
int rint = 0,max_iterations = 100000;
while(max_iterations--){
rint =(rand() % scale_factor) + 1;
if(rint > lo_num && rint < scale_factor)break;
}//while
if(!max_iterations){cout<<"failed to find number"<<endl;return 0;}
rfrac = double(rand()%RAND_MAX)/double(RAND_MAX);
rfrac = rfrac * pow(10.,dec_places);
switch(flag){
case ROUND:rfrac = double(round(rfrac));break;
case CEIL:rfrac = double(ceil(rfrac));break;
case FLOOR:rfrac = double(floor(rfrac));break;
}
return(rint + (rfrac/pow(10.,dec_places)));
}//rand_float()
double dec_round(double x, int flag,int dec_places){
// rounds the decimal part of a double
x = x * pow(10.,dec_places);
switch(flag){
case ROUND:x = double(round(x));break;
case CEIL:x = double(ceil(x));break;
case FLOOR:x = double(floor(x));break;
}
return(x/pow(10.,dec_places));
}//dec_round()
int main() {
double x;
int i,rint;;
unsigned seed = unsigned (time(NULL));
srand(seed);
//rounding
x = 123.098765432109876543210987654321;
printf("%.20f\n",x);
x = dec_round(x,FLOOR,4);
printf("X Rounded %f\n\n",x);
//random numbers
printf("random numbers\n");
for(i = 0;i<10;i++){
x = rand_float(FLOOR,10,3,5) ;
printf("%f\n",x);
}
//loss of digits
x = .098765432109876543210987654321;
printf("\n%.21f\n",x);
double z,y;
for(i = 0;i < 51;i++){
z = 0.;
y = 0.;
z = double(pow(2,i));
y = x + z;
printf("%d %.17f\n",i,y);
}
return(0);
}//main()