method will not return correct result - do-while

public static double infiniteSeries(int terms) {
int i = 0;
double result = 0;
int n = 2;
do{
result += 1/n;
i++;
n *= 2;
} while (i < terms);
return result;
}
here is the code that I have written so far. the goal of the method is to return the sum of an infinite series 1/2 + 1/4 + 1/8... + ... 1/n. So if the user calls the method with an input of 1, the method should return .5 and if the user inputs 2, it shoudl return .75 and so on. Any time I call this method it returns 0 no matter what terms I enter. I tried changing result += 1/n; to result += n; and that returns the sum of the n values the way I would expect. So it doesn't like the 1/n for some reason, but I have no idea why or how to fix it. Any help is appreciated!

Related

I built a function to calculate a price of an item each year. But my function won't read one of it's variable

I created a function to calculate the selling price of an item. Each year, the price of the item will decrease by 3/4 of its original price. The problem with my function is it doesn't want to read the year variable regardless of its value. My function always returns 60000000. Can someone please tell me what's wrong with it?
int add(double year, double price) {
int i = 0;
while (i < year) {
double final_price = price * 3 / 4;
i++;
return final_price.round();
}
}
void main(List<String> arguments) {
double x = 3;
double y = 80000000;
int result = add(x, y);
print(result);
}
int add(double year, double price) {
int i = 0;
double final_price=price; // change 1
while (i < year) {
final_price = final_price* 3 / 4; // change 2
i++;
}
return final_price.round(); // change 3
}
void main(List<String> arguments) {
double x = 3;
double y = 80000000;
int result = add(x, y);
print(result);
}
Here you go. Returning a value from inside the while loop will stop the function execution on the first traversal only.
Also you need to make final price equal to price because final_price's value is not being changed as price remains same and i increases.
what actually are you to trying to do ?
I fixed your code
int add(double year, double price) {
int i = 0;
double final_price = 0 ;
while (i < year) {
double f = price * 3 / 4;
i++;
final_price = f ;
}
return final_price.round();
}
void main() // you try to pass an arguments that never used I remove it
{
double x = 3;
double y = 800000;
int result = add(x, y);
print(result);
}

warning X3557: loop only executes for 0 iteration(s), forcing loop to unroll

The compiler produce a "warning X3557: loop only executes for 0 iteration(s), forcing loop to unroll" and I don't understand why.
Here is the source code. It is a revisited itoa() function for HLSL producing resulting ascii codes in an array of uint.
#define ITOA_BUFFER_SIZE 16
// Convert uint to ascii and return number of characters
uint UIntToAscii(
in uint Num, // Number to convert
out uint Buf[ITOA_BUFFER_SIZE], // Where to put resulting ascii codes
in uint Base) // Numeration base for convertion
{
uint I, J, K;
I = 0;
while (I < (ITOA_BUFFER_SIZE - 1)) { // <==== Warning X3557
uint Digit = Num % Base;
if (Digit < 10)
Buf[I++] = '0' + Digit;
else
Buf[I++] = 'A' + Digit - 10;
if ((Num /= Base) == 0)
break;
}
// Reverse buffer
for (K = 0, J = I - 1; K < J; K++, J--) { // <==== Warning X3557
uint T = Buf[K];
Buf[K] = Buf[J];
Buf[J] = T;
}
// Fill remaining of buffer with zeros to make compiler happy
K = I;
while (K < ITOA_BUFFER_SIZE)
Buf[K++] = 0;
return I;
}
I tried to rewrite the while loop but this doesn't change anything. Also tried to use attribute [fastopt] without success. As far as I can see the function produce the correct result.
Any help appreciated.
The warning you are getting is
WAR_TOO_SIMPLE_LOOP 3557 The loop only executes for a limited number
of iterations or doesn't seem to do anything so consider removing it
or forcing it to unroll.
The warning is pretty much self explanatory, if you consider that loops are considered inefficient in GPGPU, so the compiler tries to unroll them when it's possible. What the compiler is telling you is that you created some loops that can run more efficiently if unrolled, or can be removed because they never run. If a loop is unrollable, it means that you can predict at compile time the number of times it will run. Your loops on first look should not fulfill this criterium.
I = 0;
while (I < (ITOA_BUFFER_SIZE - 1)) { // <==== Warning X3557
uint Digit = Num % Base;
if (Digit < 10)
Buf[I++] = '0' + Digit;
else
Buf[I++] = 'A' + Digit - 10;
if ((Num /= Base) == 0)
break;
}
This while loop runs up to 15 times I < (ITOA_BUFFER_SIZE - 1), depending on (Num /= Base) == 0. Final value of I is between 1 and 15, depending on how if ((Num /= Base) == 0) evaluates on each cycle. Nonetheless, it still is unrollable, because the compiler may still insert a conditional jump over the iterations.
// Reverse buffer
for (K = 0, J = I - 1; K < J; K++, J--) { // <==== Warning X3557
uint T = Buf[K];
Buf[K] = Buf[J];
Buf[J] = T;
}
This second loop, instead should not be unrollable, because I should not be known to the compiler.
The warning you reported
warning X3557: loop only executes for 0 iteration(s), forcing loop to unroll
might refer to the first loop if if ((Num /= Base) == 0) always evaluates to true on first iteration. In that case, I would be equal to 1, and J would be equal to 0 on the second loop. That second loop would not run because K < J would evaluate to false on first iteration.
What you get in the end if you let it [unroll] is probably a single iteration on the while loop and the complete removal of the subsequent for loop. I highly suspect this is not your intended behaviour, and while it might suppress the warning, you might want to check the code and see if something does not run the way it should.

double datatype behaving in an undefined manner

Even though I know turbo c is completely obsolete now, my instructor has put a condition to code in it.
I am having an issue that when I am trying to pass a double value to a function it is not behaving properly. I am getting fluctuating output several of the time, sometimes even weird. Let's see my code first:
#include<stdio.h>
#include<conio.h>
double func(double input) {
return input * input;
}
double simpson1By3(double initial, double final, double parts) {
double sum = 0;
double h = (final - initial) / parts;
double oddSum = 0, evenSum = 0;
int i;
printf("%f %f %f\n", initial, final, parts);
printf("%f\n", h);
printf("%f %f\n", evenSum, oddSum);
sum += func(initial) + func(final);
printf("%f %f\n", evenSum, oddSum);
for (i = 1; i < parts; i = i + 2) {
oddSum += func(initial + (i * h));
}
for (i = 2; i < parts; i = i + 2) {
evenSum += func(initial + (i * h));
}
oddSum *= 4;
evenSum *= 2;
printf("%f %f\n", evenSum, oddSum);
sum += evenSum + oddSum;
sum *= h / 3;
return sum;
}
int main() {
clrscr();
printf ("%f", simpson1By3(0, 6, 6));
getch();
return 0;
}
sample output:
What am I doing wrong there? Why are the arguments passed printed erroneously there along with other variables there and why is that -0 printing? Please help. I have tried finding something similar to it in forums but completely failed. Please help.

How to create a random selector by custom number?

Traditionally if I wish to choose a case occurred 25%, I use "arc4random()%" function by integer to trigger the case of 1/4 chance.
Now I have 4 cases with float rate. Let's say,
A 0.3055
B 0.391
C 0.165
D 0.1485
A+B+C+D=1
How can I develop a random selector to trigger a case of 4 by properly selecting? Of course, case B gets the most chance to be selected.
Many thanks
I don't think this is perfect, but it'll get you close
int probability = arc4random_uniform(10000);
NSLog(#"Probability: %i", probability);
if (probability < 3055) {
// A
}
else if (probability <= (3055 + 3910)) {
// B
}
else if (probability <= (3055 + 3910 + 1650)) {
// C
}
else {
// D
}
By selecting with the appropriate probability from a uniform distribution.
int myOwnFunkyDistribution(void)
{
uint32_t A = 0.3055 * UINT32_MAX;
uint32_t B = A + 0.3910 * UINT32_MAX;
uint32_t C = B + 0.1650 * UINT32_MAX;
uint32_t r = arc4random();
if (r < A)
return 0;
if (r < B)
return 1;
if (r < C)
return 2;
return 3;
}
With more cases, this is definitely to be refactored using an array and a for loop.
You want to do a multinomial sample. You could do this in iOS with GSL or other C- or C++-based statistics libraries.

Shift (like Matlab function) rows or columns of a matrix in OpenCV

In Matlab there is a shift function in order to perform a circular shift of the columns or rows of a matrix. There is a similar function in OpenCV?
I was searching for same question but since there is none, I wrote by myself. Here is another option. In my code you can shift right or left n times: for left numRight is -n, right +n.
void shiftCol(Mat& out, Mat in, int numRight){
if(numRight == 0){
in.copyTo(out);
return;
}
int ncols = in.cols;
int nrows = in.rows;
out = Mat::zeros(in.size(), in.type());
numRight = numRight%ncols;
if(numRight < 0)
numRight = ncols+numRight;
in(cv::Rect(ncols-numRight,0, numRight,nrows)).copyTo(out(cv::Rect(0,0,numRight,nrows)));
in(cv::Rect(0,0, ncols-numRight,nrows)).copyTo(out(cv::Rect(numRight,0,ncols-numRight,nrows)));
}
Hope this will help to some people. Similarly, shiftRows can be written
Here is my implementation of the circular matrix shift. Any suggestion is welcome.
//circular shift one row from up to down
void shiftRows(Mat& mat) {
Mat temp;
Mat m;
int k = (mat.rows-1);
mat.row(k).copyTo(temp);
for(; k > 0 ; k-- ) {
m = mat.row(k);
mat.row(k-1).copyTo(m);
}
m = mat.row(0);
temp.copyTo(m);
}
//circular shift n rows from up to down if n > 0, -n rows from down to up if n < 0
void shiftRows(Mat& mat,int n) {
if( n < 0 ) {
n = -n;
flip(mat,mat,0);
for(int k=0; k < n;k++) {
shiftRows(mat);
}
flip(mat,mat,0);
} else {
for(int k=0; k < n;k++) {
shiftRows(mat);
}
}
}
//circular shift n columns from left to right if n > 0, -n columns from right to left if n < 0
void shiftCols(Mat& mat, int n) {
if(n < 0){
n = -n;
flip(mat,mat,1);
transpose(mat,mat);
shiftRows(mat,n);
transpose(mat,mat);
flip(mat,mat,1);
} else {
transpose(mat,mat);
shiftRows(mat,n);
transpose(mat,mat);
}
}
Short answer, no.
Long answer, you can implement it easily if you really need it, for example using temporary objects using cv::Mat::row(i), cv::Mat::(cv::Range(rowRange), cv::Range(cv::colRange)).
Or if you're using Python, just the roll() method.

Resources