Bad math (and code) while incrementing values - processing

Warning: I'm a total beginner. Very rookie mistakes ahead. The language used is Processing (Java).
I'm using functions to add numbers consecutively (i.e. 1+2+3+4+5+6 and so on) up to 10. I use the float "num" represents how high it should count up in this incremental manner, which is 10.
Next, I'm calculating factorials (1*2*3*4*5*6 and so on) up to 10.
My teacher gave the example in class for adding the numbers consecutively, which looks like:
float Addition(float num) {
float val1=1;
float val=0;
while (val1 <=num){
val=val+val1;
val1++;
}
return val;
}
This adds to 55, as it should, since we're incrementing until we hit 10. Could someone please explain the concept of this for me? I'm working on a bit now that adds in increments of 4 (i.e. 0+4+8+12+16+20 and so on) up to 10, but my math is WAY is off; it should equal to 180, but instead equals 45:
float Addition2(float num) {
float val1=1;
float val=1;
while (val1 <=num){
val=val*val1;
val1=val1+val2+4;
}
return val;
}
I'm not looking for anyone to fix the math for me, but to explain the concept itself and how I would properly calculate this (if that makes sense).
Thanks in advance.
P.S.
As a bonus, here is my work on the factorial, again, also wrong. If someone could also explain the concept of this, that would be smashing:
float Multiplication1(float num) {
float val1=1;
float val=1;
while (val1 <=num){
val=val*val1;
val1=val1+2;
}
return val;
}

To understand code, try to take it line by line. It might help to add comments to it to understand. It might also help to use longer and more descriptive variable names. Let's try with the function that works:
//this function adds up 1+2+...maxNumberToAdd
float addition(float maxNumberToAdd) {
//start at 1
float currentNumberToAdd = 1;
//keep track of your total sum
float totalSoFar = 0;
//loop 1,2,3...maxNumberToAdd
while (currentNumberToAdd <= maxNumberToAdd){
//add the current number to the total
totalSoFar = totalSoFar + currentNumberToAdd;
//go to the next number to add
currentNumberToAdd++;
}
//return the total
return totalSoFar;
}
Now that you have that, you can think about modifying it to do your next task.
You say you want to start at 0 instead of 1. Find the line of code responsible for starting at 1. What happens if you change it to something else?
You say you want to add only every 4th number. Find the line of code responsible for going to the next number. What happens if you increase it by something other than 1?

Related

C++ srand() repeating the same string of numbers [duplicate]

So, I'm trying to create a random vector (think geometry, not an expandable array), and every time I call my random vector function I get the same x value, though y and z are different.
int main () {
srand ( (unsigned)time(NULL));
Vector<double> a;
a.randvec();
cout << a << endl;
return 0;
}
using the function
//random Vector
template <class T>
void Vector<T>::randvec()
{
const int min=-10, max=10;
int randx, randy, randz;
const int bucket_size = RAND_MAX/(max-min);
do randx = (rand()/bucket_size)+min;
while (randx <= min && randx >= max);
x = randx;
do randy = (rand()/bucket_size)+min;
while (randy <= min && randy >= max);
y = randy;
do randz = (rand()/bucket_size)+min;
while (randz <= min && randz >= max);
z = randz;
}
For some reason, randx will consistently return 8, whereas the other numbers seem to be following the (pseudo) randomness perfectly. However, if I put the call to define, say, randy before randx, randy will always return 8.
Why is my first random number always 8? Am I seeding incorrectly?
The issue is that the random number generator is being seeded with a values that are very close together - each run of the program only changes the return value of time() by a small amount - maybe 1 second, maybe even none! The rather poor standard random number generator then uses these similar seed values to generate apparently identical initial random numbers. Basically, you need a better initial seed generator than time() and a better random number generator than rand().
The actual looping algorithm used is I think lifted from Accelerated C++ and is intended to produce a better spread of numbers over the required range than say using the mod operator would. But it can't compensate for always being (effectively) given the same seed.
I don't see any problem with your srand(), and when I tried running extremely similar code, I did not repeatedly get the same number with the first rand(). However, I did notice another possible issue.
do randx = (rand()/bucket_size)+min;
while (randx <= min && randx >= max);
This line probably does not do what you intended. As long as min < max (and it always should be), it's impossible for randx to be both less than or equal to min and greater than or equal to max. Plus, you don't need to loop at all. Instead, you can get a value in between min and max using:
randx = rand() % (max - min) + min;
I had the same problem exactly. I fixed it by moving the srand() call so it was only called once in my program (previously I had been seeding it at the top of a function call).
Don't really understand the technicalities - but it was problem solved.
Also to mention, you can even get rid of that strange bucket_size variable and use the following method to generate numbers from a to b inclusively:
srand ((unsigned)time(NULL));
const int a = -1;
const int b = 1;
int x = rand() % ((b - a) + 1) + a;
int y = rand() % ((b - a) + 1) + a;
int z = rand() % ((b - a) + 1) + a;
A simple quickfix is to call rand a few times after seeding.
int main ()
{
srand ( (unsigned)time(NULL));
rand(); rand(); rand();
Vector<double> a;
a.randvec();
cout << a << endl;
return 0;
}
Just to explain better, the first call to rand() in four sequential runs of a test program gave the following output:
27592
27595
27598
27602
Notice how similar they are? For example, if you divide rand() by 100, you will get the same number 3 times in a row. Now take a look at the second result of rand() in four sequential runs:
11520
22268
248
10997
This looks much better, doesn't it? I really don't see any reason for the downvotes.
Your implementation, through integer division, ignores the smallest 4-5 bit of the random number. Since your RNG is seeded with the system time, the first value you get out of it will change only (on average) every 20 seconds.
This should work:
randx = (min) + (int) ((max - min) * rand() / (RAND_MAX + 1.0));
where
rand() / (RAND_MAX + 1.0)
is a random double value in [0, 1) and the rest is just shifting it around.
Not directly related to the code in this question, but I had same issue with using
srand ((unsigned)time(NULL)) and still having same sequence of values being returned from following calls to rand().
It turned out that srand needs to called on each thread you are using it on separately. I had a loading thread that was generating random content (that wasn't random cuz of the seed issue). I had just using srand in the main thread and not the loading thread. So added another srand ((unsigned)time(NULL)) to start of loading thread fixed this issue.

Is there any way to combine Funcs into a Func has one more dimension?

I started to learn Halide from last month.
And finally encounterd big problem for me.
I'm trying to implement function like following C-like code in Halide.
for( int y = 0; y < 3; ++y ){
for( int x = 0; x < 3; ++x ){
out(x, y) = out(x-1, y-1) + 1;
}
}
so assuming initial image is below.
0 0 0
0 0 0
0 0 0
output image will be …(0 out of bound)
1 1 1
1 2 2
1 2 3
so I thought two possible solutions.
・Solution1
Define above algorithm like this recursive function.
Func algorithm1(Func input, int n)
{
Func src, clamped, dst;
Var x, y;
if(n == 1){
src = input;
}else{
src = algorithm1(input, n-1);
src.compute_root();
}
clamped = BoundaryConditions::constant_exterior(src, 0, 0, SIZE, 0, SIZE);
dst(x, y) = clamped(x-1, y-1) + 1;
return dst;
}
And use above function like following code.
Func input, output;
input(x, y) = src(x, y);
output = algorithm1(input, SIZE);
output.realize(src);
This implementation barely works. But obviously rebundunt.
Because most of the computation result of the each stage(Func) are not match the final result although each Func computes across over entire image.
And I need to handle more large(normal) images.
So I thought another possible solution.
・Solution2
At first of second solution.
Declare a function defines relationship between one column and another one.
Func algorithm2(Func src)
{
Func clamped, dst;
Var x;
clamped = BoundaryConditions::constant_exterior(src, 0, 0, SIZE);
dst(x) = clamped(x-1) + 1;
return dst;
}
Then, let's combine this.
Func output[3];
output[0](x) = cast<uint32_t>(0);
for(int i = 1; i < SIZE; ++i){
output[i] = algorithm2(output[i-1]);
}
Alright... Here's the problem. How can I combine this array of Funcs as a Func?
Of cource, I can get an Image if I realize this array of Funcs at each func to an pointer of the column's head. But What if I want to pass it to the next Func?
I looked around entire Halide examples(test, apps) these days. But I think there's no similar example.
And you might already noticed my discomfort of English, actually I'm a japanese. So if there are useful example for this problem, I'm so sorry in advance. If so, please tell me where it is. If there's another good implementation idea, please teach me. Anyway I need someone's help!
I appreciate for your reading.
[edit 2]
edit 1 is my foolish question. I can schedule it compute_root().
I've decided to left them on here, really embarrassing though.
I hope this will be helpful to another foolish man.
[edit 1]
I'm appreciate to your fast and detailed response from bottom of my heart!
I'm sorry for late response, I wanted to reply to you after succeeding to implement my algorithm. However, my Halide code still doesn't work what I wanna do and got some things to confirm.
First off, I would like to tell you I realized my misunderstanding of Halide thanks to you. At first of my algorithm's implementation step, I wrote definition using only pure 'Var's.
So I got following error.
All of a functions recursive references to itself must contain the same pure variables in the same places as on the left-hand-side.
I thought this error occured because of scheduling flexibility. If such definition is allowed and schedule it to split, It means that scheduling changes algorithm. This comprehension is correct? From such comprehension, although I already read reduction part of Tutorials and blur example, I misunderstood that I cannot access neighbor pixels in all of Func definitions. I don't know why though.
And reduction domain couldn't be split because of same reason. I think I got it now.
Here's another question to your code. Thanks to your Halide implementation example, I've almost succeeded to implement what I wanna do with no consideration. However, this implementation is desperately slow although I'm handling 20x20 cropped image for ease of debugging.
I'm considering this slowness is caused by reduction domain. In your example, for example when calculating the value g(10, 10), Halide calculation is scheduled from f(0, 0) to f(0, 0) and finally get there value. In the other hand, C implementation just loads the value at g(9, 9) and just increment it though. We can confirm such calculation from printing loop nest.
produce g:
for y:
for x:
produce f:
for y:
for x:
f(...) = ...
for range:
for range:
f(...) = ...
consume f:
g(...) = ...
I would like to confirm that Avoiding this recomputation is impossible? and so you suggested it?
And I would like to ask you another simple question. If there is reverse-dependency like this,
for( int y = 2; y > 0; --y ){
for( int x = 2; x > 0; --x ){
out(x, y) = out(x+1, y+1) + 1;
}
}
Is Halide able to express this code?
The algorithm1 and algorithm2 parts here are not very clear to me. I understand the initial problem statement and the English seems fine so I will endeavor to provide some help answering the question I think you are asking. I'll do this by illustrating a few Halide mechanisms you may not know about or that aren't obvious for use here. Hopefully this will be helpful.
First off, to map a dimension of a Halide Func to different expressions, you pretty much have to use a select statement:
Var x, y, n;
Func f_0, f_1, f_both;
f_0(x, y) = ...;
f_1(x, y) = ...;
f_both(x, y, n) = select(n == 0, f_zero, f_one);
This can be expanded to more cases via adding arguments to the select. This is more useful for piecewise computations than for recursive structures but seems the most direct answer to the question in the title.
The second mechanism is Tuple. This allows a Func to have more than one value, which can be indexed with compile time constants. I don't think this is the answer you are looking for, but i tis convered in tutorial/lesson_13_tuples.cpp .
Finally, Halide supports reductions, which are designed to handle the case in the first code example. This looks like so:
Var x, y;
Func f, g;
RDom range(0, 3, 0, 3); // Form is min/extent, not start/end
f(x, y) = 0; // Initial condition
f(range.x, range.y) = f(range.x - 1, range.y - 1) + 1;
g(x, y) = f(x, y);
Buffer<int32t> result = g.realize(3, 3);
This should produce the output from your first example. Reductions, or "update definitions" are covered in tutorial/lesson_09_update_definitions.cpp .

Algorithm to Determine Width of Text

In a program I'm writing in an unnamed language, I have a block of text for which the width is unknown, and all I know is the maximum width that this block of text could be. Given this information, I need to find out the smallest possible width that this text could be (assume that I can't use the metrics of the characters / glyphs or the character count). So far I just have a brute force solution which looks like follows:
for (int i = .1; i < maxTextWidth; i += .1)
{
if (textFitsInGivenWidth(text, i))
{
textWidth = i;
break;
}
}
I'd like to try and optimize this as much as I can. My first thought was to use a binary search, but I'm having trouble implementing this in the proper way (and am not sure if it's even possible). Does anyone have any suggestions on what I could do here to improve the run time using only what I've given in the above solution?
Binary Search is the answer indeed.
http://en.wikipedia.org/wiki/Binary_search_algorithm
for integer binary search, it can be:
minW=0, maxW=maxTextWidth
while(minW<=maxW){
mid=(minW+maxW)/2;
if (textFitsInGivenWidth(text, mid)){
maxW=mid-1;
}else{
minW=mid+1;
}
}
textWidth=minW
The idea is, if you have textFitsInGivenWidth(text, mid) == True,
then you must have textFitsInGivenWidth(text, i) == True for all i>=mid,
and if it's False, then you have textFitsInGivenWidth(text, i) == False for all i<=mid
so each time we check the middle of the interval to be checked, and reduce the interval into half . The time is O(logN), in which N=maxTextWidth
update: for float support, see the example below :
float minW=0, maxW=maxTextWidth
while(1){
if (maxW-minW<0.05)
break;
float mid=(minW+maxW)/2;
if (textFitsInGivenWidth(text, mid)){
maxW=mid;
}else{
minW=mid;
}
}
textWidth=minW
and to get a precision of .1, simply change the last line to :
textWidth=int(minW*10)/10.0

How to compare int values?

I would like to know how to compare int values.
I would like to know that once I compare both 2 int values, I would like to know how far apart these 2 values are and if it is possible to put this in a 'if' statement.
The only problem I have is that (lets say int HELLO), HELLO's value always changes at random, so I would like to know how do I always compare HELLO's value and a different int's value on the go, so that at any moment if the result of both values are only 50 numbers off (negative or positive), it would trigger let's say timer2->Stop();.
Thank you.
If you have two int values, then you can subtract them to find out the difference between the two. Then in your if-test you just check if they are within 50 of each other and then execute the code...
Here's some pseudocode for you to work off of:
int valueOne = 100;
int valueTwo = 50;
int differenceBetweenValues = valueOne - valueTwo;
if ( (differenceBetweenValues >= 50) || (differenceBetweenValues >= -50) ) {
timer2->Stop();
}
You could then make that as a function and pass your values in (as you've stated they're different each time).
The distance between two int numbers is calculated as an absolute value of their difference:
int dist = abs(value1 - value2);
You can put it in an if statement or do anything you wish with the result:
if (abs(value1 - value2) > 50) ...

Adding the sum of numbers using a loop statement

I need serious help dividing the positive numbers and the negative numbers.
I am to accumulate the total of the negative values and separately accumulate the total of the positive values. After the loop, you are then to display the sum of the negative values and the sum of the positive values.
The data is suppose to look like this:
-2.3 -1.9 -1.5 -1.1 -0.7 -0.3 0.1 0.5 0.9 1.3 1.7 2.1 2.5 2.9
Sum of negative values: -7.8 Sum of positive
values: 12
So far I have this:
int main () {
int num, num2, num3, num4, num5, sum, count, sum1;
int tempVariable = 0;
int numCount = 100;
int newlineCount = 0, newlineCount1 = 0;
float numCount1 = -2.3;
while (numCount <= 150)
{
cout << numCount << " ";
numCount += 2;
newlineCount ++;
if(newlineCount == 6)
{
cout<< " " << endl;
newlineCount = 0;
}
}
**cout << "" << endl;
while (numCount1 <=2.9 )
{
cout << numCount1 << " ";
numCount1 += 0.4;
newlineCount1 ++;
} while ( newlineCount1 <= 0 && newlineCount >= -2.3 );
cout << "The sum is " << newlineCount1 << endl;**
return 0;
}
I do not know C/C++ but here is a general idea of the loop assuming the values are coming from an array. (since I am unaware of how they are coming in, i.e. user input, etc.)
Logic:
Use a for loop structure opposed to a while, to loop over each element of the array.
Initialize two variables to keep count, positiveSum and negativeSum.
At each iteration of the element, check to see if it's greater than 0. That's how you can divide the positive and negative numbers accordingly.
If greater than zero, add the element onto the running positiveSum, else add it to the running sum of negativeSum.
When the loop finishes, positiveSum and negativeSum should have the calculated sum.
If this is homework, (I don't remember if the homework tag was there prior to the question, or was added on later) this pseudo code should point you in the right direction without explicitly doing the entire work for you.
Pseudo Java Code (not tested or compiled)
// as a good convention, I always initialize variables,
// for numbers I always use zero's.
double positiveSum, negativeSum = 0.0;
// assuming array holds the array of values.
for (i=0; i < array.length; i++) {
// if positive, add it to the count
if (array[i] > 0) positiveSum = positiveSum + array[i];
// else negative
else negativeSum = negativeSum + array[i];
}
Once it's completed, both positiveSum and negativeSum should hold the correct calculated sum.
If you have any questions along the way, I can edit my answer to help you achieve the correct answer, I wish I could give it away but that's what your responsibilities are for homework.
I would loop through each number individually, let's call it currentValue
if the number is negative, negativeNumberTotal += currentValue
else if positive, positiveNumberTotal += currentValue
You will get your individual totals that way. Very simple.
You are clearly overcomplicating the problem. First of all you don't need two separate loops for the numbers, as there is a constant 0.4 difference between them, even between -0.3 and 0.1. You only have to check if it's negative or not to know how to sum them up.
Loops are simpler if you use an integer as counter. As you want 14 numbers you can simply count from 0 to 13, and from that you can easily calculate the corresponding floating point value.
Example C# code:
double negSum = 0.0, posSum = 0.0;
for (int i = 0; i < 14; i++) {
double number = -2.3 + (double)i * 0.4;
if (number < 0) {
negSum += number;
} else {
posSum += number;
}
}
You can of course use a floating point number in the loop, but then you need to take into account the inexact nature of floating point numbers. You should make sure to use an ending interval that is something like halfway between the last number that you want and the next.
double negSum = 0.0, posSum = 0.0;
for (double number = -2.3; number < 3.1; number += 0.4) {
if (number < 0) {
negSum += number;
} else {
posSum += number;
}
}
Also, when repeatedly accumulating floating point numbers (like adding 0.4 over and over again), you also accumulate rounding errors. Most numbers can't be represented exactly as floating point numbers, so it's likely that you are actually adding something like 0.3999999999999994225 instead of 0.4 each iteration. It's not likely to add up to something that is enough to show up in this small example, but you should be aware of this effect so that you can anticipate it in situations with more numbers.
You have several magic numbers, whose purpose I'm not sure of, e.g numcount1 = -2.3 In general you want to avoid magic numbers.
You might want to give your variables more descriptive names than num1, num2, etc.
Could you explain more precisely what the parameters are for your assignment?
EDIT:
I've noticed that you are using very weird conditions to control your loop. You are continuing until numcount1 is = 2.9, which is a very fragile sort of setup. The first thing I would do in your shoes is to rewrite your program so that the loop terminates when there are no more numbers to add. (Alternatively, you could just make it stop after, say 12 values.)
EDIT AGAIN:
OK, how about this
int sumOfPos = 0, sumOfNeg = 0, currentValue = -2.3, terminationValue = 2.9;
while (currentValue <= terminationValue) {
if ( /* there is a missing condition here */ ) {
// need a statement here to increment your negative counter
} else {
// need a statement here to increment your positive counter
}
}
// put some statements here to do output
This is easily solvable with pure math:
lowerBound = -2.3
upperBound = 2.9
incAmount = 0.4
signChange = lowerBound % incAmount
numBelowChange = -(lowerBound-signChange)/incAmount
avgNegValue = -(numBelowChange+1)/2.0*incAmount + signChange
sumOfNegative = numBelowChange*avgNegValue
numAboveChange = (upperBound-signChange)/incAmount
avgPosValue = (numAboveChange+1)/2.0*incAmount + signChange
sumOfPositive = numAboveChange*avgPosValue + signChange
It's more accurate, and more efficient than looping and adding.
With the constants you provided,
signChange = 0.1
numBelowChange = 6.0
avgNegValue = -1.3
sumOfNegative = -7.8
numAboveChange = 7.0
avgPosValue = 1.7
sumOfPositive = 12.0
If you're unfamiliar with the % operator, x%y means "divide x by y and return the remainder". So 5%2=1
http://en.wikipedia.org/wiki/Modulo_operator
I'm not a programmer, but I thought it was best to keep the inside of a loop as lean as possible. This version works without any if statements inside the loop, just simple addition. It's late at night, and I've had a couple of whiskies, but I think this algorithm would work (it worked when I tried implementing it in python). It's psuedo code, so you'll have to write it up in yr language of choice:
neg_total =0
abs_total=0
loop with i in all_your_numbers
total += i
abs_total += abs(i)
end
neg_total=(total-abs_total)/2
pos_total=abs_Total+neg_Total
print "sum of negative values =", neg_total
print "sum of positive values =", pos_total
I'm not sure if this is good programming practice, I'm just chuffed that it worked. Points if you can explain how.

Resources