02-07-2004, 12:35 PM | #1 (permalink) | |
Devils Cabana Boy
Location: Central Coast CA
|
another simple C question
Ok this program is the same as the last one, i am to enter 3 variables height width and length and it spits out the volume, the square footage and the ratio between the length and the width, it does everything except the ratio it rounds to the nearest hole number, even though it is stored as a double.
Thanks in advance. Quote:
__________________
Donate Blood! "Love is not finding the perfect person, but learning to see an imperfect person perfectly." -Sam Keen |
|
02-07-2004, 03:11 PM | #2 (permalink) |
paranoid
Location: The Netherlands
|
I'm no C - programmer but maybe the operands for the division need to be doubles for a result to return a double?
dValueResult = dValueWidth / dValueLength (or perhaps only one of the two)
__________________
"Do not kill. Do not rape. Do not steal. These are principles which every man of every faith can embrace. " - Murphy MacManus (Boondock Saints) |
02-07-2004, 03:33 PM | #3 (permalink) |
WARNING: FLAMMABLE
Location: Ask Acetylene
|
Your not really describing what your problem is.
I am going to let you in on a little secret, real programmers don't bother to save every teeny tiny bit here and there. If you designing an object that might have millions of instantiation... well then I might understand if you used shorts when you don't need the capacity. Mixing data types is generally a pain in C/Java like languages because there are strict rules that govern how the compiler will do things. You end up sending explicit casting instructions (which waste time) and don't end up with the hugest performance or space gains. And casting makes your code ugly... really ugly. In an object with 10 members if you make one of them a short, you only save a few bytes. Even with millions of them your only saving some constant multiple of space. It might sound important but in the big scheme of things these aren't the make or break performance determinants. The exception being realtime/interactive applications. In this case I would say just use floats throughout. If this were Java I would say doubles.
__________________
"It better be funny" |
02-07-2004, 03:51 PM | #4 (permalink) | |
Addict
Location: Ottawa, ON, Canada
|
Quote:
Let's say you have something like: Code:
int nDenominator = 2; int nNumerator = 3; double dResult; dResult = nNumerator / nDenominator; You can fix this code in either one of two ways. 1) Change the variables nNumerator or nDenominator to be a double 2) Typecast either one or both of the variables to be a double (i.e. (double) nNumerator / (double) nDenominator ). In either case, you end up getting the answer 1.5, which is exactly what you should be getting.
__________________
"A witty saying proves nothing" - Voltaire |
|
02-07-2004, 04:02 PM | #5 (permalink) | |
Addict
Location: Ottawa, ON, Canada
|
Quote:
I can see exactly where you're coming from, but you shouldn't cloud a simple issue with big talk like instanstiation and memory management. It's unnecessary, and just adds chaos to confusion.
__________________
"A witty saying proves nothing" - Voltaire |
|
02-07-2004, 04:22 PM | #7 (permalink) | |
Addict
Location: Ottawa, ON, Canada
|
Quote:
Not to put any words into Dilbert1234567's mouth, or anything.
__________________
"A witty saying proves nothing" - Voltaire |
|
02-07-2004, 10:18 PM | #9 (permalink) |
Location: Waterloo, Ontario
|
Yeah, I think Dilbert1234567 is confused about the use of data types. However, I disagree with kel as to why he's confused. I simply think that Dilbert1234567 has no understanding of the roles of the different variable types.
I mean, who would use integers to represent area or volume? Generally speaking, integers are used for counting or indexing, and floats (or doubles) are used for measurements... |
02-08-2004, 12:29 AM | #10 (permalink) |
Banned
Location: 'bout 2 feet from my iMac
|
*sigh* men. ok, long and short of it: an int, divided by an int, will always return an int. you need to use a more precise type of variable to store your data. or cast your ints as doubles before your devison. for the "why" sa well as the how, see above.
|
02-08-2004, 12:31 AM | #11 (permalink) |
Devils Cabana Boy
Location: Central Coast CA
|
i have to use the difrent memory sizes for the home work, to see waht and how they function, my problem is that the program is rounding dWidthToLength when it should not be rounding it off.
__________________
Donate Blood! "Love is not finding the perfect person, but learning to see an imperfect person perfectly." -Sam Keen |
02-08-2004, 02:49 AM | #12 (permalink) |
Banned
Location: 'bout 2 feet from my iMac
|
dWidthToLength = (nRoomWidth / nRoomLength);
ok. dWidthToLenght is a double. but nRoomWidth and nRoomLenght are INTEGERS. you need to open your book up, and look up Integer division. it's not the same as normal division. if you divide integers, you will get an integer answer, that is equal to the floor of the decimal division of the 2 values. so 2/4 = 2. 2/5 will ALSO =2. ALWAYS. because you're deviding INTEGERS. to prevent this, you can do 2 things. 1: cast nRoomWidth and nRoomLength as doubles or floats. 2: make nRoomWidth and nRoomLength doubles or floats. that's it. those are your options. |
02-09-2004, 10:51 PM | #13 (permalink) |
Devils Cabana Boy
Location: Central Coast CA
|
ya i just found that so i taked on *1.0 and that fixes it, int*int * float = float
__________________
Donate Blood! "Love is not finding the perfect person, but learning to see an imperfect person perfectly." -Sam Keen |
Tags |
question, simple |
|
|