How to know if a number is larger then the maximum value in iphone objective-c
A read some posts here and google and don´t find a substancial answer. Supose I ha开发者_C百科ve 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.
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.
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).
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.
Use an NSNumberFormatter and set the maximum and minimum values appropriatelty to the size of the type you want to put the input in.
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.
精彩评论