home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Wiley While loops

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1939
Discussions: 200,992, Posts: 2,379,902, Members: 246,360
Old February 21st, 2008, 11:29 PM   Digg it!   #1 (permalink)
Ultimate Member
 
dchw_dude's Avatar
 
Join Date: Nov 2004
Location: Provo, UT
Posts: 1,337
Angry
Wiley While loops

Well, I have this beautiful program written that elegantly taked decimals and takes them to a binary state. At least I think so. For some reason, my algorithm hangs on even numbers! Odds concatenate right and come out correct. Why?

Code:
		
while(decimal != 0)
{
	if((decimal%2) == 0)
		binary = binary.concat("0");
		System.out.println("evens are concatenating");
			
	if((decimal%2)!= 0)
		binary = binary.concat("1");
		System.out.println("odds are concatenating");
			
	decimal--;
	decimal /= 2;
}
My output looks like the following for odds:

Code:
Input you number please:
:3
As a decimal: 3.0
evens are concatenating
odds are concatenating
evens are concatenating
odds are concatenating
As binary: 00011
Process completed.

But for evens, it just repeats over and over again "evens are concatenating
odds are concatenating". Hmmmm . . . whats broken in my logic?


edit: I changed the decimal in my loop from a float to an int, and it no longer hangs on evens. (what was happening the float with its huge amount was divising crazy small numbers as it went down) Now its giving me wrong numbers in answer to my even input. 4 came out as what 2 should have been.
__________________
Team TechIMO - Team # 111
http://folding.stanford.edu/

Last edited by dchw_dude : February 21st, 2008 at 11:39 PM.
dchw_dude is offline   Reply With Quote
Old February 21st, 2008, 11:53 PM     #2 (permalink)
Ultimate Member
 
dchw_dude's Avatar
 
Join Date: Nov 2004
Location: Provo, UT
Posts: 1,337
Thumbs up

Wow! I got it. I was decrementing by 1 in my loop, when I should not do that - for some reason I though I had to subtract the remainder, but its a simple division thing. Neat! Here is my final loop:

Code:
while(num != 0)
{
	if((num%2) == 0)
		binary = binary.concat("0");
	if((num%2) != 0)
		binary = binary.concat("1");
	
	num /= 2;
}
Yes!
dchw_dude is offline   Reply With Quote
Old February 25th, 2008, 09:15 AM     #3 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
technically, there's still an error in your logic...When it's odd, you should decrement by 1, then divide by two, when it's even, you just divide by two.
sr71000 is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
SuperPI - how many loops is "stable"?? LostBok Processors, Memory, and Overclocking 1 June 1st, 2006 03:24 PM
IE loops when sign in to hotmail BustedAvi General Tech Discussion 7 April 28th, 2006 06:22 PM
Fruity Loops dzee Multimedia and Audio 7 February 6th, 2006 10:02 AM
Anyone experienced with Fruity Loops Studio? thekingofpain Multimedia and Audio 4 August 28th, 2004 03:27 AM
MemTest86 Loops.... woodbutcher General Tech Discussion 2 July 6th, 2003 02:33 AM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3083)
Charges against non-tippers dropped.. (20)
Health Care Rationing (13)
Delete an OS (17)
Nvidia GTX 260 problem (10)
Laptop with wireless problem. (12)
Wireless Televisions. (12)
windows vista security holes (19)
CPU fan stops spinning randomly (11)
Regular Build (11)
Point and Shoot Camera Suggestions. (8)
[F@H SPAM 11/16/09] ! 1/2 months to.. (40)
windows 7 problem (7)
Internet Lost (5)
Recent Discussions
Print spooler problem (17)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (40)
Foxconn Blackops x48 MoBo (3)
Q9650 vs. Q9550 (2)
Desktop Calendar Application (2)
Looking for new motherboard (1)
soundmon.exe (8)
Jedi Academy Problem (3)
Can a page file be "too big".. (1)
Nvidia GTX 260 problem (10)
Point and Shoot Camera Suggestions. (8)
Size after cutting 700Mb file is 2.5 .. (0)
Delete an OS (17)
windows vista security holes (19)
updating BIOS via winflash, claims fi.. (1)
New Server Configuration Suggestions (0)
cheap gaming laptop? (12)
Unallocated Space (2)
help me pls laptop just stopped worki.. (1)
C# + LINQ Help (7)
Dynex DX E-402 (3)
EVGA 9800 gtx help with finding a goo.. (12)
Multiple Restarts Required at Boot (5)
cell phone won't work (0)
Is the PSU I received dead? (15)


All times are GMT -4. The time now is 09:41 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28