(very) simple math not working perhaps due to to large numbers?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 111
Reputation: Bladtman242 is an unknown quantity at this point 
Solved Threads: 3
Bladtman242 Bladtman242 is offline Offline
Junior Poster

(very) simple math not working perhaps due to to large numbers?

 
0
  #1
Jan 12th, 2009
Hi again, first; i realise this peace of code is stupid, it has been taken out of context, altered for the sake of simplicity etc.

I wonder if the problem is the quite large number stored in int to (changed to unsigned long int)
i have read on cplusplus.com that even a signed int should be enough though? (i think?) but i believe this varies from compiler to compiler (or is it just from system to system?)

here goes.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int udregn (int tal);
  6. int main()
  7. {
  8. unsigned long int to=2;
  9. for (int i=0;i<64;i=i+1) {
  10. to = to*2;
  11. cout<< to <<endl;
  12. }
  13. cout<< to <<"hej";
  14. return 0;
  15. }

the output is just increasing numbers until 2147483648 and from there on it's just zeros?
Yo god, where do i report a bug?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 947
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: (very) simple math not working perhaps due to to large numbers?

 
0
  #2
Jan 12th, 2009
Did you check limits.h?
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 111
Reputation: Bladtman242 is an unknown quantity at this point 
Solved Threads: 3
Bladtman242 Bladtman242 is offline Offline
Junior Poster

Re: (very) simple math not working perhaps due to to large numbers?

 
0
  #3
Jan 12th, 2009
Nope, to be honest i do not know what that is? sounds like max values? how do i check it? like cout<< limits.h ?
Yo god, where do i report a bug?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 947
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: (very) simple math not working perhaps due to to large numbers?

 
0
  #4
Jan 12th, 2009
No....
"Compiler Dir\Include\limits.h", just check the file with any flavor of text editor.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 111
Reputation: Bladtman242 is an unknown quantity at this point 
Solved Threads: 3
Bladtman242 Bladtman242 is offline Offline
Junior Poster

Re: (very) simple math not working perhaps due to to large numbers?

 
0
  #5
Jan 12th, 2009
lol.. okay.

what am i looking for? i opened limits.h but what do then look for? there are a lot of comments about the safety of limits.h, and suggestions to use something else, also alot of names and numbers. could you please tell me what to look for?
Yo god, where do i report a bug?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 947
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: (very) simple math not working perhaps due to to large numbers?

 
0
  #6
Jan 12th, 2009
Something like: #define LONG_MAX 2147483647L
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,400
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 113
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: (very) simple math not working perhaps due to to large numbers?

 
0
  #7
Jan 12th, 2009
Basically, inside the limits.h file, there will be lots of macros which define the maximum value for many different data types, It would look something like this.
  1. #define MB_LEN_MAX 5 /* max. # bytes in multibyte char */
  2. #define SHRT_MIN (-32768) /* minimum (signed) short value */
  3. #define SHRT_MAX 32767 /* maximum (signed) short value */
  4. #define USHRT_MAX 0xffff /* maximum unsigned short value */
  5. #define INT_MIN (-2147483647 - 1) /* minimum (signed) int value */
  6. #define INT_MAX 2147483647 /* maximum (signed) int value */
  7. #define UINT_MAX 0xffffffff /* maximum unsigned int value */
  8. #define LONG_MIN (-2147483647L - 1) /* minimum (signed) long value */
  9. #define LONG_MAX 2147483647L /* maximum (signed) long value */
  10. #define ULONG_MAX 0xffffffffUL /* maximum unsigned long value */
  11. #define LLONG_MAX 9223372036854775807i64 /* maximum signed long long int value */
  12. #define LLONG_MIN (-9223372036854775807i64 - 1) /* minimum signed long long int value */
  13. #define ULLONG_MAX 0xffffffffffffffffui64 /* maximum unsigned long long int value */
You don't have to worry about all that, but as you are using the data type unsigned long int and want to know the maximum value it holds, the maximum value will be defined as ULONG_MAX like MosaicFuneral said. If this data type isn't big enough for you, try using a larger one like unsigned long long (with ULLONG_MAX as the macro containing it's maximum possible value).

Hope this helps clear things up for you.
Last edited by William Hemsworth; Jan 12th, 2009 at 4:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 111
Reputation: Bladtman242 is an unknown quantity at this point 
Solved Threads: 3
Bladtman242 Bladtman242 is offline Offline
Junior Poster

Re: (very) simple math not working perhaps due to to large numbers?

 
0
  #8
Jan 12th, 2009
Hmm thanks both of you. Certainly cleared out some things for me. for some reason my long_max is only 1 larger than ulong_max. this is for my "transportable" IDE (i have installet it on a USB stick, thats all) that might have something to do with it?

can i just change those values? either just by editing limits.h or by changing the value for a specific program? or can i make a variable of unlimmited size? i believe i have seen something about foo in that context, but i am unable to find it again??
Yo god, where do i report a bug?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: (very) simple math not working perhaps due to to large numbers?

 
0
  #9
Jan 12th, 2009
if you want even bigger numbers then you can always make your own number data type using a string

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 111
Reputation: Bladtman242 is an unknown quantity at this point 
Solved Threads: 3
Bladtman242 Bladtman242 is offline Offline
Junior Poster

Re: (very) simple math not working perhaps due to to large numbers?

 
0
  #10
Jan 12th, 2009
^^ make a string and just give numbers to it or?.. sorry i dont quite get it :-)
Yo god, where do i report a bug?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC