Overflow and underflow are two concepts that refer to the limits of numerical representation in computing. They can happen in various programming contexts, including in Python, especially when dealing with floating-point numbers or integers that exceed the maximum or minimum size that can be represented.
1. Overflow
Overflow occurs when a calculation produces a result that is larger than the maximum value that can be represented in the allocated space. In Python, integers(int) are arbitrary-precision, meaning they can grow to accommodate any number as long as your machine's memory can handle it. However, overflow is still relevant in the context of fixed-size data types such as floating-point numbers(float) and when using external libraries that rely on C or other languages where integers have fixed sizes.
import sys
import math
# Trying to represent an extremely large floating-point number
# Close to the maximum float
large_number = 1.79e308
# This will result in 'inf', indicating overflow
print(large_number * 10)
>>>> inf
2. Underflow
Underflow occurs when a calculation produces a result closer to zero than the smallest value that can be represented in the allocated space. In Python, this is most commonly seen with floating-point numbers when the result of a calculation is so close to zero that it cannot be represented accurately, leading to a loss of precision.
# Very small difference
small_number = 1e-323
# This will result in 0.0, indicating underflow
print(small_number / 10)
>>>> 0.0
[출처]
'개발' 카테고리의 다른 글
[websocket] (1) python websocket 서버 생성 & 클라이언트 테스트 (0) | 2024.02.28 |
---|---|
[ubuntu] mpi4py 설치 시 에러 (0) | 2024.02.23 |
[ubuntu] nohup 파일 생성 시 현재 날짜로 생기게 하기 (0) | 2024.02.15 |
[ubuntu] crontab 사용법 간단정리 (1) | 2024.02.10 |
[ubuntu] 좀비 프로세스 죽이기(defunct) (1) | 2024.01.13 |