473,586 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When is min(a, b) != min(b, a)?

This issue may have been referred to in
news:<ma******* *************** *************** **@python.orgbu t I didn't
entirely understand the explanation. Basically I have this:
>>a = float(6)
b = float('nan')
min(a, b)
6.0
>>min(b, a)
nan
>>max(a, b)
6.0
>>max(b, a)
nan

Before I did not know what to expect, but I certainly didn't expect
this. So my question is what is the min/max of a number and NaN or is it
not defined (for which I would have expected either an exception to be
raised or NaN returned in each case).

As a corrollary would I be able to rely on the above behavior or is it
subject to change (to fix a bug in min/max perhaps :-)?

Jan 21 '08 #1
17 2151
'NaN' means "Not a number". according to Python semantics, if you try
to compare it with any other float numbers, it should return FALSE.
just like
>>>
1.0 'abc'
False
>>>
Since it always return FALSE, it is not a surprise for your question.

If you wish to get infinitive number, you'd use 'inf' or '-inf', from
IEEE 754 semantics:
>>>a=float(6)
b=float('inf ')
c=float('-inf')

Albert Hopkins wrote:
This issue may have been referred to in
news:<ma******* *************** *************** **@python.orgbu t I didn't
entirely understand the explanation. Basically I have this:
>>a = float(6)
>>b = float('nan')
>>min(a, b)
6.0
>>min(b, a)
nan
>>max(a, b)
6.0
>>max(b, a)
nan

Before I did not know what to expect, but I certainly didn't expect
this. So my question is what is the min/max of a number and NaN or is it
not defined (for which I would have expected either an exception to be
raised or NaN returned in each case).

As a corrollary would I be able to rely on the above behavior or is it
subject to change (to fix a bug in min/max perhaps :-)?
Jan 21 '08 #2
'NaN' means "Not a number". according to Python semantics, if you try
to compare it with any other float numbers, it should return FALSE.
just like:
>>>1.0 'abc'
False

Since it always return FALSE, it is not a surprise for your question.

If you wish to get infinitive number, you'd use 'inf' for positive
infinitive or '-inf' for negative infinitive, from
IEEE 754 semantics, just like:
>>>a=float(6)
b=float('inf ')
c=float('-inf')
For more information, PEP-0754 may be helpful.
Jan 21 '08 #3
On Jan 21, 3:15 am, Albert Hopkins <mar...@python. invalidwrote:
This issue may have been referred to in
<news:ma******* *************** *************** **@python.orgbu t I didn't
entirely understand the explanation. Basically I have this:
>>a = float(6)
>>b = float('nan')
>>min(a, b)
6.0
>>min(b, a)
nan
>>max(a, b)
6.0
>>max(b, a)
nan

Before I did not know what to expect, but I certainly didn't expect
this. So my question is what is the min/max of a number and NaN or is it
not defined (for which I would have expected either an exception to be
raised or NaN returned in each case).

As a corrollary would I be able to rely on the above behavior or is it
subject to change (to fix a bug in min/max perhaps :-)?
I am definitely NOT a floating point expert, but I did find this:
http://en.wikipedia.org/wiki/IEEE_754r#min_and_max

P.S. What platform /Compiler are you using for Python?

- Paddy.
Jan 21 '08 #4
On Sun, 20 Jan 2008 20:16:18 -0800, Paddy wrote:
I am definitely NOT a floating point expert, but I did find this:
http://en.wikipedia.org/wiki/IEEE_754r#min_and_max

P.S. What platform /Compiler are you using for Python?
Linux with GCC 4

-a
Jan 21 '08 #5
On Jan 21, 12:00 am, Albert Hopkins <mar...@python. invalidwrote:
On Sun, 20 Jan 2008 20:16:18 -0800, Paddy wrote:
I am definitely NOT a floating point expert, but I did find this:
http://en.wikipedia.org/wiki/IEEE_754r#min_and_max
P.S. What platform /Compiler are you using for Python?

Linux with GCC 4

-a
Please note that NaN's are very funky and platform dependent. They
depend on their underlying platform's C library for creation and
display. On windows, "float('nan ')" will cause an exception, as there
are no valid string representations of NAN that can be converted to
the special floating point value. Also, if you manage to create a nan
under Windows, it displays as "1.#QNAN".

Infinite values are also problematic. In almost all cases, it is far
better to avoid infinite and NaN values.

--Jason
Jan 21 '08 #6
On Jan 21, 9:55*am, Jason <tenax.racc...@ gmail.comwrote:
display. *On windows, "float('nan ')" will cause an exception, as there
are no valid string representations of NAN that can be converted to
the special floating point value. *Also, if you manage to create a nan
under Windows, it displays as "1.#QNAN".
I believe this will be fixed in Python 2.6 :)

Mark
Jan 21 '08 #7
On 2008-01-21, Jason <te***********@ gmail.comwrote:
Infinite values are also problematic. In almost all cases, it is far
better to avoid infinite and NaN values.
In many applications (e.g. process control) propogating NaN
values are way too useful to avoid. Avoiding NaN would make a
lot of code far more complicated than would using them.

--
Grant Edwards grante Yow! How's the wife?
at Is she at home enjoying
visi.com capitalism?
Jan 21 '08 #8
Jason wrote:
Please note that NaN's are very funky and platform dependent. They
depend on their underlying platform's C library for creation and
display. On windows, "float('nan ')" will cause an exception, as there
are no valid string representations of NAN that can be converted to
the special floating point value. Also, if you manage to create a nan
under Windows, it displays as "1.#QNAN".

Infinite values are also problematic. In almost all cases, it is far
better to avoid infinite and NaN values.
CC to Python Dev

I've fixed that and enhanced the support for NaN and inf for 2.6 and
3.0. I'm working together with Mark on more NaN and inf related fixes
and he has fixed some numerical issues in the cmath module. We both hope
to get Python's math more sound and stable across platforms.

So far I got float('nan'), float('inf') and float('-inf') working on all
platforms. The math module got three new methods: isinf, isnan, copysign.

Additionally the trunk-math branch contains code for inverse hyberbolic
functions (acosh), log1p, Mark's fixes for complex math and more stuff.
For example operations on NaNs now return a NaN on all platforms (except
1**NAN which is defined as 1 and 0**NAN which is defined as 0). In 2.5
it depends on the platform whether a function raises an exception or
returns NaN.

Mark had the nice idea to introduce a thread local or global flag for
NaN support. Depending on a flag Python turns a NaN into an exception.
The feature needs a proper PEP. Maybe Mark has time to write a PEP in time.

Christian

Jan 23 '08 #9
Grant Edwards wrote:
In many applications (e.g. process control) propogating NaN
values are way too useful to avoid. Avoiding NaN would make a
lot of code far more complicated than would using them.
NaNs are very useful for experienced power users but they are very
confusing for newbies or developers without a numerical background.

It's very easy to create an inf or nan in Python:

inf = 1E+5000
ninf = -inf
nan = inf * 0.

1E5000 creates a nan because it is *much* bigger than DBL_MAX (around
1E+308). In fact it is even larger than LDBL_MAX (around 1E+4932).

Christian

Jan 23 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
12507
by: Nuno | last post by:
Is there any SQL Error? Or I have to use Select case in VB code to control SQL instead. Thank you for any ans. Nuno
13
4998
by: elad | last post by:
Hi The Menu doesn't work properly when I have 2 frame and the Menu popup frame=document target frame, when I choose item in the menu the doc opened and the menu get stuck. Here is the code I'm using: FrameSet ------------------------------------------------------- <html>
7
19946
by: sql-db2-dba | last post by:
Does DB2 just fudge it when it is an empty table? Is there a "formula" for average row size when you have variable length records. Or you really have to know what your application is packing into those varchar columns. Bill Leung leungb@aptea.com
4
5917
by: Peter Row | last post by:
Hi, I have created a UserControl which is subsequently hosted on a standard form. My control has a TabControl on it but it has no TabPages configured. At runtime I create X pages and put a single RichTextBox on each page. In my controls loaded event I call my Render() method which configures everything appropriately. When setting up the...
5
555
by: AAguiar | last post by:
I have an asp.net project where the code behind the aspx page calls a c# class which makes calls to a managed static C++ class. The C# class works fine when the asp net worker process starts, when it is invoked by pressing "F5", or when the web.config file is modified. In all these cases the web.config file contains <identity...
8
2556
by: Galina | last post by:
Hello I have 6 dependent list boxes on my ASP page:  Faculty;  Lecturer;  Course;  Course occurrence;  Group;  Week commencing date. When faculty is selected, lists of lecturers and courses are populated. When course is selected, lists of occurrences, groups and
2
3938
by: Ramez T. Mina | last post by:
I tried to increment variable when I click on a button, but it didn't work. I use the VB.NET editor and not the HTML editor. any one can help??
22
4006
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to receive 5 bytes buffer , I call the BeginReceive and then wait on AsyncWaitHandle.WaitOne() but it is signald imidiatly , and the next call to...
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8336
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.