![]() |
|
#1
|
|||
|
|||
Result of this calculation is zeroHey guys I was writting a program when I came up with a problem I couldn't explain.
Here is my questionable part of code, i include only whats needed. CPP / C++ / C Code:
Before writing the code as seen (working now) What i wrote was minthr=maxhr*(50/100); maxthr=maxhr*(85/100); Printf results were zeroes. Why was there such a problem using parenthesis? |
|||
|
#2
|
|||
|
|||
Re: Another newbs question.Good question.
First, the arithmetic is done from left to right. So if maxhr was 90, in the statement would first do 90*50 and then 4500/100. The answer to that would be 45. If you were to use the parentheses as you show you would first do the (50/100). In C that would return an int - a whole number with no fraction. ie: the number of times 100 goes into 50, which is zero. And of course maxhr*0 will always return 0. To get that to return a fraction you can simply introduce an decimal point like this: CPP / C++ / C Code:
In C this is done automatically according to what type is requested as the statement is processed as determined by order of precedence. You can also specify a type in your code. (casting). CPP / C++ / C Code:
It's a bit confusing at first but you'll get used to it. |
|
#3
|
|||
|
|||
Re: Result of this calculation is zeroOh, I see. Thank you!
|
Recent GIDBlog
Match IP in CIDR by gidnetwork
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help!!! A Problem with Camera Resolution | Melvin Lin | C++ Forum | 8 | 18-Jun-2004 21:36 |
| A.I. Question | calculus87 | Open Discussion Forum | 1 | 03-May-2004 12:48 |
| question of practice | magiccreative | C++ Forum | 1 | 06-Feb-2004 07:17 |
| a C input question | tmike | C Programming Language | 1 | 16-Sep-2003 02:31 |
| Motherboard and CPU Question | pcxgamer | Computer Hardware Forum | 2 | 29-Oct-2002 06:15 |
Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The