-
Very simple mathematic question
here comes for the most people a very simple question that I can't understand. How did I calculate the percent of a number?
If I have a variable with the number 1000 stored in it, and I want to calculate what 15,55 % of that number is and store the result in another variable. How? Can someone give me an example?
-
Re: Very simple mathematic question
This should help - http://www.purplemath.com/modules/percntof.htm
As far as code is concerned, what could you write?
-
Re: Very simple mathematic question
-
Re: Very simple mathematic question
shall I do something like this:
If we take 10 percent of 1000.
Code:
int result;
int number = 1000;
result = 10 % number;
I don't know if I shall use the % sign? Probably this is totaly wrong :D
-
Re: Very simple mathematic question
10 % of 1000 here would be 10*1000/100. % is for finding remainder.
-
Re: Very simple mathematic question
Quote:
Originally Posted by GCDEF
number times .1555
Can you explain this more? What is 'times'?
-
Re: Very simple mathematic question
Quote:
Originally Posted by exterminator
10 % of 1000 here would be 10*1000/100. % is for finding remainder.
Yes, but if I want 0.65% of 1000?
0.65*1000/65 is this correct?
-
Re: Very simple mathematic question
Quote:
Originally Posted by Lindberg
Can you explain this more? What is 'times'?
Hmm, no math knowledge, no language knowledge... ts ts ts :)
He means 1000 * 0.1555 :)
-Andy
-
Re: Very simple mathematic question
>> Yes, but if I want 0.65% of 1000?
>. 0.65*1000/65 is this correct?
No. It would be
Or more concisely
-
Re: Very simple mathematic question
ok, I have now came to a conclusion.
Code:
double result;
int number = 1000;
char chresult[15];
result = number * 0.15;
So simple it was :) Thanks for all help!
-
Re: Very simple mathematic question
Quote:
Originally Posted by Lindberg
here comes for the most people a very simple question that I can't understand. How did I calculate the percent of a number?
If I have a variable with the number 1000 stored in it, and I want to calculate what 15,55 % of that number is and store the result in another variable. How? Can someone give me an example?
No disrespect for Lindberg, who is asking an honest question, but this is a truly pathetic commentary on the school system in Sweden.
And I thought that the school system here in the U.S. was pathetic.
-
Re: Very simple mathematic question
Perhaps he is only 7 years old? :-)
-
Re: Very simple mathematic question
Quote:
Originally Posted by MikeAThon
And I thought that the school system here in the U.S. was pathetic.
Hmm... Are you sure you mean pathetic - or pathologic? ;)
-
Re: Very simple mathematic question
It's nothing wrong with the school system in sweden, i have learned the most mathematic thing in my school. But it is a quite different thing to calculate mathematics in a programming language like c++, especially if you are a beginner.
Quote:
Originally Posted by Mark Xa
Perhaps he is only 7 years old? :-)
I'm 30 years old :D
-
% in C++ is different
I do not think it says anything about the school where Lindberg has studied. The problem is that the way we do mathematical calculations on paper and the way we write programs for that are often different. In the context of this thread, consider %. In Mathematics, we know that % means percentage, but in C++, it means remained when one integer is divided by another.
Several years back, when I first learnt about computer programming (it was Fortran 77), then I was surprised that there is an upper limit to integer. From Mathematics, I had learnt that there is no upper limit to integer. :-)
Just a few days back, I was writing a program in VB where I had to calculate probability using Binomial distribution. It requires factorial. Using paper and pen I can do calculations in a very short time but my program was failing even though the sample size was as small as 30. I tried integer, long and even currency data type. For very high precision and to store large numbers, currency data type is used in VB. That also did not help. Then I used Sterling approximation to find factorials. That worked for sample size upto 50. Then I did lots of other optimizations and could somehow make it work for sample size of 3,500.
So, this is the problem. The way we do calculations using pen and paper and the way we need to write programs to do mathematical calculations are often different.
Coming the the question that Lindberg asked. This is how you can write program:-
//Declare the variables. I am assuming that the number whose percentage
//is to be calculated does not have fractional parts. Otherwise,
//instead of long, use double.
long lNumber = 0;
double dblPerc = 0;
//Enter the values
lNumber = 1000;
dblPerc = 15.55;
double dblResult = lNumber * dblPerc/100;
-
Re: % in C++ is different
Quote:
Originally Posted by visharad
I do not think it says anything about the school where Lindberg has studied. The problem is that the way we do mathematical calculations on paper and the way we write programs for that are often different. In the context of this thread, consider %. In Mathematics, we know that % means percentage, but in C++, it means remained when one integer is divided by another.
Yup it is different but what do you have to say about someone who did not understand: "number times .1555". 5 times 5 is equal to = ?
Quote:
Originally Posted by visharad
Several years back, when I first learnt about computer programming (it was Fortran 77), then I was surprised that there is an upper limit to integer. From Mathematics, I had learnt that there is no upper limit to integer. :-)
Yes, there is no limit to integer. But when you would do calculations on paper.. did the size of the paper restrict you for the maximum value? You could only write a number to fit the paper (or say 2/3 sheets joined together) but still there is a limitation on that. Isn't there? The same is with computers. It is not really that unbelievable.
Quote:
Originally Posted by visharad
Just a few days back, I was writing a program in VB where I had to calculate probability using Binomial distribution. It requires factorial. Using paper and pen I can do calculations in a very short time but my program was failing even though the sample size was as small as 30. I tried integer, long and even currency data type. For very high precision and to store large numbers, currency data type is used in VB. That also did not help. Then I used Sterling approximation to find factorials. That worked for sample size upto 50. Then I did lots of other optimizations and could somehow make it work for sample size of 3,500.
Discussing VB in VC++ does not make much sense. I bet if you wrote a factorial program in C++, it would work faster than you can think assuming you don't remember the direct value. For example if you are calculating 10! so that will be equal to 10*9*8*7*6*5! Now, for a human being memory comes into play... if you know 5! you can directly put its value but computers do computation. Moreover, you can use this with programs as well... and terminate the factorial call to 5! substituting its value. Plus it depends on what code you actually wrote.
-
Re: % in C++ is different
Quote:
Originally Posted by exterminator
Yup it is different but what do you have to say about someone who did not understand: "number times .1555". 5 times 5 is equal to = ?
That I agree. I thought that MikeAthon was talking about %. I think when Lindbrg saw the word times, he might have tried to use that directly in his program.
I think Lindberg should read some entry level books on C/C++.
Quote:
Yes, there is no limit to integer. But when you would do calculations on paper.. did the size of the paper restrict you for the maximum value? You could only write a number to fit the paper (or say 2/3 sheets joined together) but still there is a limitation on that. Isn't there? The same is with computers. It is not really that unbelievable.
I agree. Now I understand quite well as to why there should be a limit. I am not at all surprised that there is a limit. But it was a bit of surprise when I heard it the first time. But the surprise was over after learning about computer memory.
Note:- I am not arguing over why there should be a limit. I am just trying to mention the confusions a person faces when he starts programming the first time.
Quote:
Discussing VB in VC++ does not make much sense. I bet if you wrote a factorial program in C++, it would work faster than you can think assuming you don't remember the direct value. For example if you are calculating 10! so that will be equal to 10*9*8*7*6*5! Now, for a human being memory comes into play... if you know 5! you can directly put its value but computers do computation. Moreover, you can use this with programs as well... and terminate the factorial call to 5! substituting its value. Plus it depends on what code you actually wrote.
I have been programming in both VB and C++. I agree with you that C++ will run faster. It is just that the person for whom I wrote the program wanted it in VB only. I did do something similar to terminating the factorial all. I had to calculate C(n,k). I found whether k or n - k is larger. I wrote code to find the product of n*(n-1) ... upto that larger number + 1. But that also was giving overflow error. Then, I used Sterling's approximation.
BTW, as I mentioned I wrote code to calculate probability using Binomial distribution.
-
Re: Very simple mathematic question
Quote:
Originally Posted by Lindberg
shall I do something like this:
If we take 10 percent of 1000.
Code:
int result;
int number = 1000;
result = 10 % number;
I don't know if I shall use the % sign? Probably this is totaly wrong :D
Yup, that is wrong. Sorry. ;) The % sign is a modulus operator. It gives you the remainder of a division operation. For example, 2 % 10 = 0 because two divides evenly into 10 leaving no remainder; however, 3 % 7 = 1 because seven divided by three equals two and one remainder.
Greg Dolley
-
Re: Very simple mathematic question
Quote:
Originally Posted by greg_dolley
Yup, that is wrong. Sorry. ;) The % sign is a modulus operator. It gives you the remainder of a division operation. For example, 2 % 10 = 0 because two divides evenly into 10 leaving no remainder; however, 3 % 7 = 1 because seven divided by three equals two and one remainder.
Greg Dolley
Is it??
You mean 7 % 3 = 1...
-
Re: Very simple mathematic question
Quote:
Originally Posted by Lindberg
here comes for the most people a very simple question that I can't understand. How did I calculate the percent of a number?
If I have a variable with the number 1000 stored in it, and I want to calculate what 15,55 % of that number is and store the result in another variable. How? Can someone give me an example?
Very easy:
Code:
void do_something(void)
{
double number;
double percent;
double result;
number = 1000;
percent = 0.1555;
result = number*percent;
}
Greg Dolley
-
Re: % in C++ is different
Quote:
Originally Posted by visharad
I did do something similar to terminating the factorial all. I had to calculate C(n,k). I found whether k or n - k is larger. I wrote code to find the product of n*(n-1) ... upto that larger number + 1. But that also was giving overflow error. Then, I used Sterling's approximation.
Overflow for what? The result or recursive calls? You could have had used the iteration approach rather than recursion.
If it was the result that was overflowing, the approximation technique wouldn't solve it either. You would then have to rely on a numeric library (or cast one on your own be it VB or VC++) capable of handling that big numbers.
Quote:
Originally Posted by visharad
I think Lindberg should read some entry level books on C/C++.
There is no alternative to it! :)
-
Sterling's approximation did help
Quote:
Originally Posted by exterminator
Overflow for what? The result or recursive calls? You could have had used the iteration approach rather than recursion.
If it was the result that was overflowing, the approximation technique wouldn't solve it either. You would then have to rely on a numeric library (or cast one on your own be it VB or VC++) capable of handling that big numbers.
There was no recursion. Approximation technique did help.
This is because I had to calculate probability using Binomial distribution. Assuming total number of trials is n, probability of success in a single trial is p and we have to calculate the probability of k number of trials, then the answer is
C(n,k) * p^k * (1-p)^(n-k)
Here by ^, I mean power. C(n,k) is the number of ways k items can be selected from n items.
Suppose that n is 30 and k is 15. What is C(n,k)? It is 30!/(15! * 15!). Since 15! appears in denominator, in order to calculate 30!, we do not have to multiply 30 * 29 * ... upto 1 (or 2). We have to multiple only upto 16. Because the rest will be cancelled by one of 15!.
I wrote a simple for loop to do that multiplication. But, if you multiply 30*29*28* ... *16, you will get overflow error.
Sterling's approximation helped. This is because Sterling's approximation says that for large n, n! is very close to
sqrt(2*pi*n) * (n/e)^n.
On a piece of paper, I wrote down expression for n! using Sterling's approximation and also for k!. Then I multiplied p^k and (1-p)^k. Did simplification. I used the simplified expression in my program.
Note:- I did not directly use Sterling's approximation for factorial. Rather, I did lots of simplifications on paper for the entire C(n,k) * p^k * (1-p)^(n-k) and used that in my code.
-
Re: Very simple mathematic question
Quote:
Originally Posted by laitinen
Is it??
You mean 7 % 3 = 1...
Yes, that is how % operator works in C/C++.
-
To Lindberg
Lindberg,
Suppose you are not writing a program. In Mathematics, how do you calculate k% of n?
You multiply k and n and divide the result by 100. Right?
Do the same in program. Just remember that the operator for multiplication is * and for division is /.
So, result is n*k/100
-
Re: Sterling's approximation did help
Quote:
Originally Posted by visharad
Note:- I did not directly use Sterling's approximation for factorial. Rather, I did lots of simplifications on paper for the entire C(n,k) * p^k * (1-p)^(n-k) and used that in my code.
This helps actually. Good thing to do. I recall now that we had to calculate the price of a 30Y (30 years maturity) UST bond. That would be a summation with about 60 terms if the coupon frequency was 2 (paid twice a year). When I had a closer look at it, it formed a geometric sequence atleast until the second last term. And then I applied the summation formula for a G.P. That simplified it a lot and in fact enhanced the program efficiency where we had to deal with real time prices for about 250-300 bonds coming in.
Whenever dealing with expressions it helps to simply them first before putting into a program.
-
Re: % in C++ is different
Quote:
Originally Posted by visharad
That I agree. I thought that MikeAthon was talking about %.
No, I was referring to the same point made by Exterminator. It's a pathetic commentary on the school system when someone who is 30 years old does not understand "number times .1555".
Adding to the patheticism (word?? it should be!!), in a further question after receiving the above answer, the OP wrote:
Quote:
Originally Posted by OP
Yes, but if I want 0.65% of 1000?
0.65*1000/65 is this correct?
So, clearly the OP does not understand the concept of "percentage". 30 years old and his school system failed to teach him percentages....
In a well-intended attempt to draw a comparison, visharad wrote:
Quote:
Originally Posted by visharad
Just a few days back, I was writing a program in VB where I had to calculate probability using Binomial distribution. It requires factorial. Using paper and pen I can do calculations in a very short time but my program was failing even though the sample size was as small as 30. I tried integer, long and even currency data type. For very high precision and to store large numbers, currency data type is used in VB. That also did not help. Then I used Sterling approximation to find factorials. That worked for sample size upto 50. Then I did lots of other optimizations and could somehow make it work for sample size of 3,500.
In my mind, there's no comparison between the two situations. It's not a question of programming skills; rather it's a question of underlying knowledge. Visharad knows probability, the Binomial distribution, factorials, and the existence of approximations to them. His school system did not fail him. It provided him with basic knowledge, which he could confirm with paper and pencil, despite the difficulties in translating that into working code. The OP, on the other hand, doesn't even understand that percentages involve an implied division by 100. He could not confirm any percentages with paper and pencil. His school system failed miserably, at providing even the most rudimentary amount of mathematical knowledge.
Mike
-
Re: % in C++ is different
Quote:
Originally Posted by MikeAThon
The OP, on the other hand, doesn't even understand that percentages involve an implied division by 100. He could not confirm any percentages with paper and pencil. His school system failed miserably, at providing even the most rudimentary amount of mathematical knowledge.
Mike
Scary, isn't it. We had another programmer here a while back about 40 years old, lots of experience programming. Had no idea how to compute the mean of a series of numbers.
-
Re: Very simple mathematic question
There can also be occasions where the use of a certain word is well accepted in a given language, but does not translate well to another language.
I ran into this issue many times in the years I dealt with the RNLN (Royal Netherlands Navy). The most hilarious occasion was on my first trip there. I asked someone "Where is the Bathroom?". They looked very puzzled and questioned why I wanted to take a bath in the middle of the work day.
It may be that "times" is not directly associated with "multiplied by".
-
Re: Very simple mathematic question
I always wonder why people call things 'simple' when they have no idea what they are talking about :) I think Lindberg just kidding, coz hardly one can find a person who finished only an elementary school but doesn't know what is a percentage...though I've heard middle statistical american doesn't know it either :)
-
Re: Very simple mathematic question
CPUWizard, that's a funny example, but it's not the same as the situation here. We are not talking about differences in terminology; we're talking about underlying concepts. Look carefully at the OP's follow-up question, which is quoted here again:
Quote:
Originally Posted by OP
Yes, but if I want 0.65% of 1000?
0.65*1000/65 is this correct?
Ask yourself: Why is he dividing by 65 instead of 100? It's because he does not understand percentages. It's not because of some failure in terminology, such as a failure to understand that the english word is "percentage" whereas the Swedish word is "procenten" (or whatever it might be). It's a failure of underlying knowledge, and again, since it's such basic mathematical knowledge, I place at least partial blame on the school system.
Mike