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 3426394
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:42:38+00:00 2026-05-18T06:42:38+00:00

max(float(‘nan’), 1) evaluates to nan max(1, float(‘nan’)) evaluates to 1 Is it the intended

  • 0

max(float('nan'), 1) evaluates to nan

max(1, float('nan')) evaluates to 1

Is it the intended behavior?


Thanks for the answers.

max raises an exception when the iterable is empty. Why wouldn’t Python’s max raise an exception when nan is present? Or at least do something useful, like return nan or ignore nan. The current behavior is very unsafe and seems completely unreasonable.

I found an even more surprising consequence of this behavior, so I just posted a related question.

  • 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-05-18T06:42:38+00:00Added an answer on May 18, 2026 at 6:42 am
    In [19]: 1>float('nan')
    Out[19]: False
    
    In [20]: float('nan')>1
    Out[20]: False
    

    The float nan is neither bigger nor smaller than the integer 1.
    max starts by choosing the first element, and only replaces it when it finds an element which is strictly larger.

    In [31]: max(1,float('nan'))
    Out[31]: 1
    

    Since nan is not larger than 1, 1 is returned.

    In [32]: max(float('nan'),1)
    Out[32]: nan
    

    Since 1 is not larger than nan, nan is returned.


    PS. Note that np.max treats float('nan') differently:

    In [36]: import numpy as np
    In [91]: np.max([1,float('nan')])
    Out[91]: nan
    
    In [92]: np.max([float('nan'),1])
    Out[92]: nan
    

    but if you wish to ignore np.nans, you can use np.nanmax:

    In [93]: np.nanmax([1,float('nan')])
    Out[93]: 1.0
    
    In [94]: np.nanmax([float('nan'),1])
    Out[94]: 1.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What does the following expression return in Java? Math.max(Float.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); I saw this question
/// I can't do this let max = float n |> sqrt |> int64
select max(DELIVERY_TIMESTAMP) from DOCUMENTS; will return the time that the latest document was delivered.
The default 64 MB max heap memory can be small for a large Java
Is there a documented max to the length of the string data you can
What is the max size of the multi line text field? Can I set
An integer's max value in Java is 2147483647, since Java integers are signed, right?
The Java Collections.max method takes only a collection of a sortable ( Comparable )
How do you get the max value of an enum?
I have a xml which is max 3 levels deep. Now by using C#

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.