|
L5
Nov 4, 2001 23:33:03 GMT -5
Post by EurasianVixen on Nov 4, 2001 23:33:03 GMT -5
Ok, where does integer i come from? Do we convert the double d into integer i and then keep decreasing it by one until d/2??
|
|
|
L5
Nov 5, 2001 6:59:52 GMT -5
Post by Brutal_Chicken on Nov 5, 2001 6:59:52 GMT -5
You can cast the double d into an integer like:
private double d = whatever; private int i = (int) d;
Then you can just decrease i by 1 until it's greater or equal to d/2. Print out every int though.
|
|
|
L5
Nov 5, 2001 18:33:53 GMT -5
Post by EurasianVixen on Nov 5, 2001 18:33:53 GMT -5
Yep, I thought so. Thanx for the clarification.
|
|
|
L5
Nov 5, 2001 19:37:10 GMT -5
Post by Majin_Blues on Nov 5, 2001 19:37:10 GMT -5
just want to clarify something...
when you have "someCount >= d/2", (and d is an integer, and you did integer division), suppose d = 25:
shouldn't the last number that prints out be a 13 since 12 < 12.5? wouldn't d/2 just be 12 doing integer division? => it'll print out 12 when it's not supposed to?
(just a caution check for everyone...)
|
|
|
L5
Nov 5, 2001 19:50:42 GMT -5
Post by Brutal_Chicken on Nov 5, 2001 19:50:42 GMT -5
But d/2 isn't an int, it's a double so in your case if d = 25 then half of that = 12.5. i starts at 25 and keeps on decreasing till it's greater or equal to d/2. That means i's final value is 13.
The problem running in my head is that an int = 4 and a double = 4.0 aren't equal using "==". So imagine this;
d = 26.0 i = 26 d/2 = 13.0
i will keep on decreasing until it's just a bit greater than 13.0. Meaning i will go down to 14 instead of 13. Hmm, perhaps using the modulus "%" operator might be of use here.
|
|
|
L5
Nov 5, 2001 19:53:58 GMT -5
Post by Majin_Blues on Nov 5, 2001 19:53:58 GMT -5
so you did it that way... ok, i thought it'd be dangerous comparing ints to doubles... but looks like you got that covered... 8)
i did it the complicated/stupid way as usual (floors & ceilings... lol), just double checking...
|
|
|
L5
Nov 5, 2001 19:58:06 GMT -5
Post by Sky on Nov 5, 2001 19:58:06 GMT -5
WOW, i don't even know there is sometihing called cast that can change a double into an integer...my prof never taught me that trick....damn it now i finally get to know that when i finished everything...i just use charAt like the previous programs to look for the decimal and when there is none...i assume it's an integer...no wonder my program is long...but it works fine
|
|
|
L5
Nov 5, 2001 20:06:59 GMT -5
Post by Majin_Blues on Nov 5, 2001 20:06:59 GMT -5
WOW!!! I've never seen so many ways to do this program... interesting! (more variations make it harder for TA's to mark stuff.. YAY!!! ;D ;D)
|
|
|
L5
Nov 5, 2001 20:13:06 GMT -5
Post by Brutal_Chicken on Nov 5, 2001 20:13:06 GMT -5
Actually, I was considering doing it your way Sky. I think that might be the only way to get 13 printed out in my second post. Checking if there's a decimal in d/2 after converting it into a string and checking if a zero follows and nothing else then it's equal and you can print it out. But I think there's another way: converting an int into a double. Heresy, you say! It can be done once I find my notebook... found it; doesn't need a cast. Dang, so how do we make 13 equal 13.0 if you can't cast it as a double since there's so many possiblities if you went from double to an int (ie: 13.2 casted would be 13). Answer 1) convert to a string and check. Unless someone thinks of a better way... and someone probably will. or 2) IF a double = 13 is the same as a double 13.0 then we're fine. Someone answer 2) real quick. Also a lesson for casting and such: Narrowing conversions - going from a double to an int requires a cast. double d = 3.24; int i = (int) d; Also useful for rounding numbers. i = (int) Math.round( 5.8 ) // i = 6 Widening conversions - going from a short to a long/int/double short s = 2; int i = 1; s = i; Here's a tricky one: char x = "a"; char y = (char) (x + 2); What's y? It's "c"! Hope I've been able to help with my meaningless knowledge. EDIT: more mistakes... surprise, surprise.
|
|
|
L5
Nov 5, 2001 20:16:02 GMT -5
Post by Brutal_Chicken on Nov 5, 2001 20:16:02 GMT -5
Double post.
|
|
|
L5
Nov 5, 2001 20:23:36 GMT -5
Post by Majin_Blues on Nov 5, 2001 20:23:36 GMT -5
hmm... doubles are not exact... wouldn't 13 as a double would look like:
13.00000000.... 00001
what happened to good old fashioned integer division or rounding?
|
|
|
L5
Nov 5, 2001 20:31:08 GMT -5
Post by Brutal_Chicken on Nov 5, 2001 20:31:08 GMT -5
Gone the way of the Dodo I'm afraid. Perhaps dividing d/2 with i and getting a denominator of 0 would suffice.
|
|
|
L5
Nov 5, 2001 20:42:20 GMT -5
Post by Majin_Blues on Nov 5, 2001 20:42:20 GMT -5
how can you get a denominator of 0 when the denominator is 2?
i mean, isn't it d/2, not d/i ?
EDIT: sorry... why go d/2/i?
|
|
|
L5
Nov 5, 2001 21:16:18 GMT -5
Post by bladehunter on Nov 5, 2001 21:16:18 GMT -5
;D
I think you CAN compare and integer with a double (ie. no need for casting)
It's just like assigning an integer value to a double variable - it is a legal operation
|
|
|
L5
Nov 5, 2001 22:16:18 GMT -5
Post by Brutal_Chicken on Nov 5, 2001 22:16:18 GMT -5
Errmm that's remainder not denominator...
You can certainly compare an int with a double but the question is when you compare 4 and 4.0 together does it return true?
|
|