Check if number is of Integer type

Boris Mocialov

I want to be able to check if a number (initially Integer) after an operation would fit into Integer again, and if it does not, either jump to an exception or maybe there is some function that does it, like if isInteger() then ...

I do not want to convert to String and then to Integer and checking the length.

Why I need it? There is a variable (Integer) that keeps update interval, which is doubled every time the request to server fails (no network, whatever). So I want to turn off automatic updates when interval reaches the maximum possible Integer value, which is 2,147,483,647

Ive look for possible answers and it seems that everywhere people use String datatype to check for some conditions on the number, but i do not want to use intermediate datatypes

David Heffernan

I'm going to attempt here to describe a variety of ways to solve your specific problem, and some more general problems of this nature.


The most general way to deal with overflow is to let the compiler write the code for you. Enable the overflow checking option and an exception will be raised if you perform an operation whose result does not fit into the data type range.

From the documentation:

The $Q directive controls the generation of overflow checking code. In the {$Q+} state, certain integer arithmetic operations (+, -, *, Abs, Sqr, Succ, Pred, Inc, and Dec) are checked for overflow. The code for each of these integer arithmetic operations is followed by additional code that verifies that the result is within the supported range. If an overflow check fails, an EIntOverflow exception is raised (or the program is terminated if exception handling is not enabled).

So if you wish to trap such a condition you can enable overflow checking and catch the EIntOverflow exception.


That will work in full generality. However let us consider a more specific scenario. Suppose that you have a positive integer that you are incrementing by a positive value. Let us say your counter is count, and your increment is incr. You are trying to detect when

count + incr > MaxInt

Rearrange the inequality like this:

count > MaxInt - incr

You can simply test that inequality before attempting the increment operation.


In fact your specific case is even simpler than that. You are doubling the value rather than incrementing using addition. So simply check whether or not

count <= MaxInt div 2

Yet another option would be to take base 2 logarithms of the quantities. This would convert your doubling process into increment by 1. In which case all you are actually doing is counting up to 31! And converting back to a true interval is performed with 1 shl count.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Check if number is of Integer type

From Dev

Check if a number is an integer or non-integer rational

From Dev

Check if a number is an integer with basic arithmetic

From Dev

Check if variable is of type int/Integer

From Dev

How to check if number is integer number, with good precision?

From Java

How do I check that a number is float or integer?

From Dev

Regular expression to check for integer or decimal number in iOS

From Dev

Check if variable is a number and positive integer in PHP?

From Dev

C - How to check if the number is integer or float?

From Dev

Twig - How to check if variable is a number / integer

From Dev

How to check if a big number is a square of an integer in R?

From Dev

Check integer number is between two values

From Dev

Android - Check the phone number type

From Dev

Check whether a factor variable is of type integer or float

From Dev

Large number failing validation as type xs:integer

From Dev

want to check the value of the input type number

From Dev

want to check the value of the input type number

From Dev

How to Check if type of Map extends Number

From Java

How can I check, the number that scanner received is an integer certainly in java?

From Dev

How can I check when integer is incremented beyond a multiple of a number?

From Dev

How to check if a number (float or integer) is within a range (0 - 100)

From Dev

How can I check when integer is incremented beyond a multiple of a number?

From Dev

how to check if the number is not an integer in C without using isdigit?

From Dev

How to check if a number is within the range of the same number including float type

From Dev

Java - Check if input is a positive integer, negative integer, natural number and so on.

From Dev

C++ random number for arbitrary integer (of fundamental type)

From Dev

C++ random number for arbitrary integer (of fundamental type)

From Dev

Type declaration [[Integer] -> Integer]

From Dev

Input type=number : Firefox converts floating point number to integer when there are 3 digit after a point

Related Related

  1. 1

    Check if number is of Integer type

  2. 2

    Check if a number is an integer or non-integer rational

  3. 3

    Check if a number is an integer with basic arithmetic

  4. 4

    Check if variable is of type int/Integer

  5. 5

    How to check if number is integer number, with good precision?

  6. 6

    How do I check that a number is float or integer?

  7. 7

    Regular expression to check for integer or decimal number in iOS

  8. 8

    Check if variable is a number and positive integer in PHP?

  9. 9

    C - How to check if the number is integer or float?

  10. 10

    Twig - How to check if variable is a number / integer

  11. 11

    How to check if a big number is a square of an integer in R?

  12. 12

    Check integer number is between two values

  13. 13

    Android - Check the phone number type

  14. 14

    Check whether a factor variable is of type integer or float

  15. 15

    Large number failing validation as type xs:integer

  16. 16

    want to check the value of the input type number

  17. 17

    want to check the value of the input type number

  18. 18

    How to Check if type of Map extends Number

  19. 19

    How can I check, the number that scanner received is an integer certainly in java?

  20. 20

    How can I check when integer is incremented beyond a multiple of a number?

  21. 21

    How to check if a number (float or integer) is within a range (0 - 100)

  22. 22

    How can I check when integer is incremented beyond a multiple of a number?

  23. 23

    how to check if the number is not an integer in C without using isdigit?

  24. 24

    How to check if a number is within the range of the same number including float type

  25. 25

    Java - Check if input is a positive integer, negative integer, natural number and so on.

  26. 26

    C++ random number for arbitrary integer (of fundamental type)

  27. 27

    C++ random number for arbitrary integer (of fundamental type)

  28. 28

    Type declaration [[Integer] -> Integer]

  29. 29

    Input type=number : Firefox converts floating point number to integer when there are 3 digit after a point

HotTag

Archive