0

when I want to init a number to infinitely small/big so that no other number is larger/smaller - there is C++ version - INT_MIN:

int global_min = INT_MIN #infinitely small number

what's the python equivalent version for super small, super big number?

Ðаn
  • 10,934
  • 11
  • 59
  • 95
ERJAN
  • 23,696
  • 23
  • 72
  • 146
  • I have to ask, do you really need such a thing? If you are computing a minimum, you can use the first element of the data set as your minimum value. – NathanOliver Mar 27 '20 at 14:21
  • @NathanOliver thx u Nathan - the question is for knowing it if i need in the future.. besides, i just used C++ and transitioning from it – ERJAN Mar 27 '20 at 14:22
  • [numpy](https://numpy.org/devdocs/reference/constants.html) has constants like `np.PINF` and `np.NINF` for positive/negative infinity – Lukas Thaler Mar 27 '20 at 14:25
  • Does this answer your question? [Maximum and Minimum values for ints](https://stackoverflow.com/questions/7604966/maximum-and-minimum-values-for-ints) – Yohann Mar 27 '20 at 14:26
  • Python can also express infinity, look here: [How can I represent an infinite number in Python?](https://stackoverflow.com/questions/7781260/how-can-i-represent-an-infinite-number-in-python) – etrnote Mar 27 '20 at 14:27
  • you have float('inf') and float('-inf') – rolf82 Mar 27 '20 at 14:27

2 Answers2

1

Python supports arbitrarily large integers - the only limit is your memory.
You can create an infinite float, though, and all integers will be less than that:

>>> 2**256
115792089237316195423570985008687907853269984665640564039457584007913129639936
>>> -2**256
-115792089237316195423570985008687907853269984665640564039457584007913129639936
>>> import math
>>> 2 ** 256 < math.inf
True
>>> - 2 ** 256 > -math.inf
True
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
1

For infinitely small or large numbers simply use math.inf after importing math.

import math

#x is infinitely small
x = float('-inf')

#y is infinitely large
y = float('+inf')
Viswa
  • 316
  • 1
  • 14
  • for clarification: `import math` is not required if `float('+inf')` is used. Alternatively (as described in the answer) one could use `import math; math.inf` (new in python 3.5) which resolves to the same constant (use `-math.inf` for negative numbers). – rosa b. Dec 30 '21 at 19:13