Lost Password?


Go Back   CodeCall Programming Forum > Software Development > General Programming

General Programming Non language specific, Assembly, Linux/Unix, Mac and anything not covered in other topics. Talk about Programming Theory here.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-05-2007, 09:38 AM
kphilux kphilux is offline
Newbie
 
Join Date: Mar 2007
Posts: 2
Rep Power: 0
kphilux is on a distinguished road
Default Will loop ever terminate???

Hi i am trying to discuss wether this loop will ever terminate any help would be much appreciated!

signed int i=1
while (i>0)
i=i*1000;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-05-2007, 11:20 AM
Deathcry's Avatar   
Deathcry Deathcry is offline
Learning Programmer
 
Join Date: Feb 2007
Posts: 69
Rep Power: 7
Deathcry is on a distinguished road
Default

no since i will always be greater than 0
__________________
the code is with you
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-05-2007, 12:16 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,276
Last Blog:
wxWidgets is NOT code ...
Rep Power: 36
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default

Yes, under most languages. When you multiply by 1000, you are not dealing with real multiplication. As a result you will start getting rollovers and hit negative values.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-05-2007, 04:02 PM
daniel daniel is offline
Banned
 
Join Date: Mar 2007
Location: Earth
Age: 28
Posts: 34
Rep Power: 0
daniel is on a distinguished road
Send a message via MSN to daniel Send a message via Yahoo to daniel
Default

I'm guessing... no
but im not really that good at programming, so i probably am not right
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-06-2007, 03:28 AM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,433
Last Blog:
Google Web Toolkit
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

Quote:
Originally Posted by WingedPanther View Post
Yes, under most languages. When you multiply by 1000, you are not dealing with real multiplication. As a result you will start getting rollovers and hit negative values.
Interesting! Can you elaborate more? What do you mean by "real" multiplication and "rollovers?"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 03-06-2007, 11:40 AM
icepack's Avatar   
icepack icepack is offline
Programmer
 
Join Date: Jul 2006
Location: North Carolina
Posts: 115
Rep Power: 9
icepack is on a distinguished road
Send a message via AIM to icepack
Default

you have an integer value

int
Occupies 32 bits or 4 bytes, which is:
-231 to 231-1 or -2,147,483,648 to 2,147,483,647.
Default value of 0.

what he's saying is, what happens when it gets past 2,147,483,647?
it starts off at the next number, negative 2 billion something.

imagine the number line as a circle and not a straight finite line


when i ran the loop, i printed the value after the loop ended.
here it is:

#include <iostream>
using namespace std;

int main()
{
signed int i=1;
while (i>0)
i=i*1000;
cout << i << endl;
}
-----output-------
[mjd0605@curly:~]$ g++ loop.cpp
[mjd0605@curly:~]$ ./a.out
-727379968
[mjd0605@curly:~]$

Last edited by icepack; 03-06-2007 at 11:47 AM. Reason: clearer
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-06-2007, 12:11 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,276
Last Blog:
wxWidgets is NOT code ...
Rep Power: 36
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default

Elaborating on what IcePack said:
you start at 1, then get 1000, then 1,000,000, then 1,000,000,000, then 1,000,000,000,000 which is outside the range supported by an integer. The high bits get thrown out and you get a completely different number, possibly interpretted as positive or negative (depending on the system). Keep this up for a while and you will eventually get to a negative number.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-06-2007, 02:38 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,433
Last Blog:
Google Web Toolkit
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

Very interesting, thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-07-2007, 10:55 AM
kphilux kphilux is offline
Newbie
 
Join Date: Mar 2007
Posts: 2
Rep Power: 0
kphilux is on a distinguished road
Default Cheers

That was a great response many thanks for the help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript: Loop through ids dirkfirst JavaScript and CSS 1 07-02-2007 03:28 PM
Sounds and Sound Loop Resources TVDinner Website Design 0 03-10-2007 09:20 AM
Terminate a method..... Ronin Java Help 1 02-08-2007 03:03 PM
Javaa:Tutorial - The Loop John Java Tutorials 0 12-09-2006 11:01 AM


All times are GMT -5. The time now is 01:18 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 98%

Ads