Getting a weird result [duplicate] - c++

This question already has answers here:
Display a number decimal format instead as an exponential in cout
(2 answers)
Closed 2 years ago.
float k,n,s,p;
float a,d;
cin>>k>>n>>s>>p;
a=n/s;
a=ceil(a);
d=k*a;
d=d/p;
cout<<ceil(d);
Am getting an output = 1e+008 when the inputs are 10000, 10000, 1, 1.
The actual result should be 100000000.
I tried using double instead of float but no success.
Kindly throw some light

Use std::fixed
cout<<fixed<<setprecision(8)<<d<<endl;

Related

How to fix rand()/RAND_MAX in a method that always produces 0.0000000? [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Integer division always zero [duplicate]
(1 answer)
Random number c++ in some range [duplicate]
(6 answers)
Closed 3 years ago.
I need to produces numbers between 0 and a max (seen in code as assetMax). In the code, the rand()/RAND_MAX always produces 0 and I cannot seem to figure out why. I use the rand() function immediately before it to produce values in a range and it works completely fine. However, here it does not.
I have tried to switch the order of the variables, create the random number in a separate double before multiplying the two, and the header.
void cPortfolio::randomize(cProblem &portfolioProblem) {
int assetCount = 6 * rand() / RAND_MAX + (portfolioProblem.assetMax-8); //this line works as expected
int test;
for (int i = 0; i < assetCount; i++) {
double num = rand() / RAND_MAX; //this always produces 0.0000
int test = num * (portfolioProblem.assetNum); } `} //cannot format these correctly please ignore the brackets

C++ Random doubles from -1 to 1 [duplicate]

This question already has answers here:
generate random double numbers in c++
(10 answers)
Closed 4 years ago.
I need to randomly generate numbers (using random()) from -1 to 1 that are doubles. For example the output would be something like:
1,
-0.3324,
0.7821,
0.9823,
-0.111
etc... this is what I was trying to do walk_Length = (double)rand()%2 - 1;
You might get away with something like
double walk_Length = static_cast<double>(rand()) / RAND_MAX * 2 - 1;

Efficient extraction of several numbers from a character string in R [duplicate]

This question already has answers here:
R: convert list of numbers from character to numeric
(3 answers)
Closed 7 years ago.
Suppose I have the character string
x <- " 1.1325 -0.9022 -0.1832 -0.5479 0.1236 -0.6556 -1.0599 -0.8881 -0.2136"
and I want to extract the floats to end up with this vector as output:
c(1.1325, -0.9022, -0.1832, -0.5479, 0.1236, -0.6556, -1.0599, -0.8881, -0.2136)
What I managed to achieve is:
na.omit(as.numeric(strsplit(samp, split = " ")[[1]]))
My question: Is there a more efficient way?
We can use scan
scan(text=x, what=numeric(), quiet=TRUE)
#[1] 1.1325 -0.9022 -0.1832 -0.5479 0.1236 -0.6556 -1.0599 -0.8881 -0.2136

How to Set certain digits for generating random numbers in C++ [duplicate]

This question already has answers here:
How do I scale down numbers from rand()?
(9 answers)
Closed 8 years ago.
Hi. Is there any way to set the size of random numbers?( in random number generator "rand()")
For example I want to generate 10 digits random numbers.
and one more question, how can i set random function to generate numbers between 0 and 1 (for example 0100110110) ?
Im not sure about setting the size of the numbers. However I dont think it would be possible to get each digit to produce just a 0 or 1.
What you can do however is something like below:
ostringstream 10digitNumber;
for(int i = 0 ; i < 10 ; i ++){
v1 = rand() % 2;// generate o or 1
10digitNumber<< v1;// build up a string of 1 and 0
}
int real10DigitNumber = static_cast<int>10digitNumber); // typecast to integer
Please forgive me if my syntax isn't 100 %. Its being awhile since I used c++.

How to quickly determine how many digits in an int? [duplicate]

This question already has answers here:
Finding the length of an integer in C
(29 answers)
Closed 9 years ago.
I know I can do:
n = floor(log10(i)) + 1;
Or I can do a quick loop:
while(i) {
n++;
i/=10;
}
Is there any better way than a complicated math operation, or a loop to achieve the goal? For example: if i = 1234, then n = 4.
The shortest way I know of (not computationally, just in terms of typing) is to call snprintf(3):
int n = snprintf(NULL, 0, "%d", i);
convert it to a string (itoa) and count the number of characters? (might be not the best performance-wise though)