Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9179797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:53:25+00:00 2026-06-17T17:53:25+00:00

Assume a simple 1-dimensional numpy array: >>> x = np.array([1,3,5,0,3,2]) Now assume I want

  • 0

Assume a simple 1-dimensional numpy array:

>>> x = np.array([1,3,5,0,3,2])

Now assume I want to perform the operation 1.0/x. I can do this with numpy:

>>> 1.0/x
array([ 1.        ,  0.33333333,  0.2       ,         inf,  0.33333333,
    0.5       ])

The problem here is the infinity (inf) result for the original element value 0, because 1.0/0 seems to return infinity in place of undefined behaviour.

Instead of infinity, I would like to provide my own custom value where these divide by 0 scenarios arise. While I know this can be accomplished using a loop, I would like to know whether there is any kind of idiomatic syntax for this kind of operation.

There’s a related question here, but it only deals with the if something: (do this) else: (do nothing) scenario whereas my question is a if something: (do this) else: (do that) scenario.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:53:26+00:00Added an answer on June 17, 2026 at 5:53 pm

    You can always patch it up later:

    a = 1.0/x
    inf_ind = np.isinf(a)
    a[inf_ind] = your_value
    

    or

    a[inf_ind] = f(x[inf_ind])
    

    Which has the advantage of not getting in the way of the nice optimized numpy methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be a really, really simple one, I would assume. I'm throwing together
Assume I have an enumerable object enum and now I want to get the
Assume a simple aspx data entry page in which admin user can upload an
I assume this should be pretty simple, but could not get it :(. In
I hope my problem has a very simple solution. I just can't find it:
Assume a group of data points, such as one plotted here (this graph isn't
Here is a simple example: Assume I have a customer table that has a
I want to do a simple image animation.Assume that there is small image of
Assume a simple JPA entity class like this: @Entity(name=TASK) public class Task implements Serializable
Lets assume a simple Spring MVC Controller that receives the ID of a domain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.