Open In App

How To Fix – Python RuntimeWarning: overflow encountered in scalar

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

One such error that developers may encounter is the “Python RuntimeWarning: Overflow Encountered In Scalars”. In Python, numeric operations can sometimes trigger a “RuntimeWarning: overflow encountered in a scalar.” In this article, we will see what is Python “Python Runtimewarning: Overflow Encountered In Scalars” in Python and how to fix it.

What is Python RuntimeWarning: overflow encountered in scalar multiply?

In Python, numeric operations can sometimes trigger a “RuntimeWarning: overflow encountered in a scalar.” This warning occurs when the computed result is too large for the chosen data type.

Error Syntax

RuntimeWarning: overflow encountered in scalar multiply

Below are the reasons for the occurrence of Python Runtimewarning: Overflow Encountered In Scalars in Python:

  • Using Larger Data Types
  • Using NumPy’s np.seterr

Using Larger Data Types

Below, code uses NumPy to calculate the square of a large integer, anticipating a potential overflow error, and prints any encountered runtime warnings.

Python3
import numpy as np

large_integer = np.int64(10) ** 20
result = large_integer * large_integer
print(result)

Output

py:4: RuntimeWarning: overflow encountered in scalar multiply
  result = large_integer * large_integer
-5047021154770878464

Using NumPy’s np.seterr

Below, code uses a computation with NumPy, setting it to raise an overflow warning. It then tries to perform a calculation that triggers the warning and prints the warning message if it occurs.

Python3
import numpy as np

try:
    np.seterr(over='raise')
    result = np.array([1.0e308]) * 2  # This will trigger the warning
except RuntimeWarning as e:
    print(f"Output 3: {e}")

Output

Review\ppp\gfg\main.py", line 5, in <module>
    result = np.array([1.0e308]) * 2  # This will trigger the warning
             ~~~~~~~~~~~~~~~~~~~~^~~
Floatingpoint: Overflow Encountered In Scalars Multiply

Solution for Python “Python Runtimewarning: Overflow Encountered In Scalars”

Below, are the approaches to solve Python Runtimewarning: Overflow encountered in scalars:

  • Handle Large Integers with Object Data Type
  • Suppression of NumPy Warnings

Handle Large Integers with Object Data Type

The code uses NumPy to manage large integers efficiently by adopting an alternative method. It attempts a computation with large integers, specifying the data type to handle the result appropriately. If an overflow error arises during the process, it captures the exception and prints an error message to indicate the occurrence.

Python3
import numpy as np

# Use a different approach to handle large integers
large_integer = np.int64(10) ** 20

try:
    # Perform the computation with large integers
    result = np.multiply(large_integer, large_integer, dtype=object)
    print(result)

except OverflowError as e:
    print(f"Overflow error occurred: {e}")

Output
60315099313909970584365185794205286400

Suppression of NumPy Warnings

Below, code employs NumPy for numerical operations, aiming to calculate a result while disregarding overflow warnings. It handles potential runtime warnings, printing either the computed result or any warning messages encountered during the computation.

Python3
import numpy as np

try:
    # Set NumPy to ignore overflow warnings
    np.seterr(over='ignore')

    # Perform the computation
    result = np.array([1.0e308]) * 2

    # Print the result
    print("Result:", result)

except RuntimeWarning as e:
    # If a RuntimeWarning occurs, print the warning message
    print(f"RuntimeWarning: {e}")

Output
Result: [inf]

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads