views:

100

answers:

5

A read some posts here and google and don´t find a substancial answer. Supose I have a largest number and I have to assing to a variable. The problem raise when I have a GUI where the user will enter the number. How I can´t know what the value the user will put in a field (let´s think in a UItextField), when I get the number and then assign this to a variable, if the number is more large them max with the type of var I will have the overflow and a inconsistent number. Other situation is whem I have to some values and I have to sum all. If the total sum is more large of the type the variable I will get the overflow again.

How can a compare the value inputed by user whith the max of the type of variable? I know have a constats for the mim and max, but if someone have a example code will help.

A: 

Limit the number of digits the user can enter, use an integer type that can accommodate all the user's digits to check for overflow (floating point if necessary).

Arkadiy
Hy Arkadiy... Yes, it´s can solve one part of the problem. I not would want limit the number of digits, but it is resonably. But how to treat when I have to sum all values getted and the value is more large then var max? Have an idea? Have some kind of test to my application apoint this error? tks a lot... (sorry my bad english)
rwvaldivia
Again, your best bet is to use the data type with enough space to accommodate the sum. Failing that, if you're using signed integers, watch for sign change when adding positive and positive or negative and negative. For unsigned integers, assert that sum is greater than parts.
Arkadiy
+1  A: 

If you store the data in a normal unsigned int, the highest number that can be represented is ~0.

If you are really serious about letting the user input arbitrarily high numbers, you can look into Arbitrary-precision arithmetic.

Example libraries are BigDigits and GNU MPFR. These are pure C and thus suitable for inclusion in Objective-C projects.

You also did not mention if you are using integer values or floats.

Amigable Clark Kant
A: 

If you can limit each number to no more then INT_MAX (2147483647 on most platforms with Cocoa) use a long long (max of 0x7fffffffffffffffLL on most platforms) for the sum. That fits more numbers then anyone will type in. If you have to multiply you'll need to check for overflow.

The macros in checkint.h are handy for that.

Or you could us NSDecimalNumber (38 digits, plus an exponent).

Or you could go looking for a bignum package.

What kind of input are you expecting? How big do your numbers really need to go? Do you really need to handle very very large values, or just produce a visible failure message and not just the wrong values on overflow? If it is just "error message, not wrong answer" you are after use int/long/long long and checkint.h.

Stripes
Hi Stripes. Tks a lot for your answer! It´s was very useful. I will use the checkint.h functions approuch.
rwvaldivia
A: 

Use an NSNumberFormatter and set the maximum and minimum values appropriatelty to the size of the type you want to put the input in.

JeremyP
A: 

It is very simple. You can't know how big of a value the user will type in, but you know the maximum value allowed based on the data type of the variable to hold it.

You go one way or the other....

1) The user needs to enter values of this large, then I need to use variables of this data type to capture them.

OR

2) If I declare variables of this specific data type, then user will only be allowed to enter values as large as the data type permits. In this case you have to restrict the input field to the appropriate length of digits. And you don't have to assign the value to a variable before checking that it is larger than your variables can hold.

Remember, you know what the maximums are for each numeric data type. Usually there are constants declared for these in the header files.
Good luck.

Julio
Hi Julio. Tks a lot. Good answer! Your approuch is very similar to Stripe user and I will use it.
rwvaldivia