4

Is there something sys.minint in python similar to sys.maxint ?

masaya
  • 67
  • 4
  • 6
  • Possible duplicate of [Maximum and Minimum values for ints](https://stackoverflow.com/questions/7604966/maximum-and-minimum-values-for-ints) – roganjosh May 21 '18 at 10:04

3 Answers3

2

The old documentation specifies that it's -sys.maxint - 1, so could just use that, or make a constant of your own with that value. Do note that in the current version maxint has been removed and probably shouldn't be used in new code.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
2

You can use float('-inf') if that meets your criteria otherwise you can do -1*sys.maxsize

birajad
  • 482
  • 1
  • 8
  • 16
0

In Python 2, adding 1 to the maxint gives the highest possible long int and in Python 2.7, subtracting 1 from maxint gives the smallest possible value for an integer.

import sys
  
max_int = sys.maxint
min_int = sys.maxint-1
long_int = sys.maxint+1
  
print("maxint :"+str(max_int)+" - "+str(type(max_int)))
print("maxint - 1 :"+str(max_int)+" - "+str(type(min_int)))
print("maxint + 1 :"+str(max_int)+" - "+str(type(long_int)))

Output

maxint :9223372036854775807 - <type 'int'>
maxint - 1 :9223372036854775807 - <type 'int'>
maxint + 1 :9223372036854775807 - <type 'long'>

This constant was removed from Python 3, as integers in this version are considered to be of arbitrary length. If you use this constant in Python 3, then you will get the following error. Consider the same example where the minimum value element has to be found out from a list

import sys
  
# initializing the list
li = [1, -22, 43, 89, 2, 6, 3, 16]
  
# assigning a larger value with maxint constant
curr_min = sys.maxint
  
# loop to find minimum value
for i in range(0, len(li)):
  
    # update curr_min if a value lesser than it is found
    if li[i] < curr_min:
        curr_min = li[i]
  
print("The minimum value is " + str(curr_min))

Output

AttributeError: module 'sys' has no attribute 'maxint'

This constant was removed as there was no longer a limit for to the value of integers. In Python 3, a constant similar to this was introduced which is sys.maxsize. This returns the highest possible integer value of variable type Py_ssize_t and also, it denotes the pointer size of the platform. This maxsize is considered to limit the size of various data structures like Strings and lists. Another thing to be noted is, in Python 3 the int and long int are merged into one. Look at the example below for better understanding.


# import the module
import sys
  
# using sys.maxsize
max_int = sys.maxsize
min_int = sys.maxsize-1
long_int = sys.maxsize+1
  
print("maxint :"+str(max_int)+" - "+str(type(max_int)))
print("maxint - 1 :"+str(max_int)+" - "+str(type(min_int)))
  
# the data type is represented as int
print("maxint + 1 :"+str(max_int)+" - "+str(type(long_int)))

Output

maxint :9223372036854775807 - <class 'int'>
maxint - 1 :9223372036854775807 - <class 'int'>
maxint + 1 :9223372036854775807 - <class 'int'>
EdwardFunnyHands
  • 103
  • 2
  • 11