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.

#include <iostream>

using namespace std;

int udregn (int tal);
int main()
{
    unsigned long int to=2;
    for (int i=0;i<64;i=i+1) {
        to = to*2;
        cout<< to <<endl;
    }
    cout<< to <<"hej";
    return 0;
}

the output is just increasing numbers until 2147483648 and from there on it's just zeros?

Recommended Answers

All 16 Replies

Did you check limits.h?

Nope, to be honest i do not know what that is? sounds like max values? how do i check it? like cout<< limits.h ?

No....
"Compiler Dir\Include\limits.h", just check the file with any flavor of text editor.

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?

Something like: #define LONG_MAX 2147483647L

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.

#define MB_LEN_MAX    5             /* max. # bytes in multibyte char */
#define SHRT_MIN    (-32768)        /* minimum (signed) short value */
#define SHRT_MAX      32767         /* maximum (signed) short value */
#define USHRT_MAX     0xffff        /* maximum unsigned short value */
#define INT_MIN     (-2147483647 - 1) /* minimum (signed) int value */
#define INT_MAX       2147483647    /* maximum (signed) int value */
#define UINT_MAX      0xffffffff    /* maximum unsigned int value */
#define LONG_MIN    (-2147483647L - 1) /* minimum (signed) long value */
#define LONG_MAX      2147483647L   /* maximum (signed) long value */
#define ULONG_MAX     0xffffffffUL  /* maximum unsigned long value */
#define LLONG_MAX     9223372036854775807i64       /* maximum signed long long int value */
#define LLONG_MIN   (-9223372036854775807i64 - 1)  /* minimum signed long long int value */
#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.

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??

if you want even bigger numbers then you can always make your own number data type using a string

Chris

^^ make a string and just give numbers to it or?.. sorry i dont quite get 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?

Generally, you cannot. The contents of limits.h generally reflects technical limits on capability of your compiler on your target operating system. Most compilers will need to be rewritten/extended/modified to support a change of those values.

okay, is there anything i can do?

You can implement a class type that has the attributes you want (or find such a class type that someone else has implemented). Internally, that type might use an array of integers or (as Freaky_Chris suggested) a string to represent larger values. You would need to define mathematical operations and also things like streaming (so you can read or write the data to a file stream).

well file i/o sounds fine, but about defining mathematical operations and.. still the string and class thing? i dont get it :-( i havent worked with structures and classes for a looong while, so i will re-read some documentation on the subject:-) what do you mean with defining mathematical operations?

would splitting up the large numbers and assigning them to an array of ints help anything??

Mathematical operations: operators like * (multiplication), + (addition), - (subtraction or negation), / (division), etc.

Splitting up large number and assigning them to an array of ints is one way: such things can be implemented using a class.

By defining mathematical operations he means you need to deal with things such as addition, multiplication etc.
You would do this by overloading the different operators such as operator+().
Also the you will want to overload the stream operators as grumpier suggested that would be the << and >> so that the data can be outputted into files and the standard output. Aswell as read into your data type.

Chris

I really dont mean to waste anyone's time, so i hope you will bare with me once again.

@Chris, you want me to save the data in files instead of in variables? and then make the calculation by changing the file instead of a variable?

@grumpier, thanks, at least that makes sence to me now ;-)

EDIT: If i use a file instead of a variable i would still have to asign the value to variable when i want to use it, right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.