473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Re: negative numbers are not equal...



Christian Heimes wrote:
You are getting the result because Python optimized small integers.
See http://svn.python.org/projects/pytho...ts/intobject.c
Integers between -5 and +256 are singletons as are some other objects
like strings with one element or empty tuples. You must not rely on
the optimization.
i see now, so i guess that's also why id() returns the same address for
them as well...
i'll dive into the implementation file, it may be a bit out of my league
but i'll see what i can gather, and hey, that's how it works, right? :-)

thanks

ariel
Aug 14 '08 #1
5 2366
On Thu, 14 Aug 2008 18:23:21 -0300, ariel ledesma wrote:
i see now, so i guess that's also why id() returns the same address for
them as well...
It just have to work like this.

a is b

is actually equal to:

id(a) == id(b)

so there is no other way for id() in such case.

Hope this helps.

--
Regards,
Wojtek Walczak,
http://www.stud.umk.pl/~wojtekwa/
Aug 14 '08 #2
On Aug 14, 4:31*pm, Wojtek Walczak <gmin...@bzt.bztwrote:
On Thu, 14 Aug 2008 18:23:21 -0300, ariel ledesma wrote:
i see now, so i guess that's also why id() returns the same address for
them as well...

It just have to work like this.

a is b

is actually equal to:

id(a) == id(b)

so there is no other way for id() in such case.

Hope this helps.

--
Regards,
Wojtek Walczak,http://www.stud.umk.pl/~wojtekwa/
For

a= 6
b= a

the test

a is b

should clearly return true. Python distinguishes what mathematics
does not, between identity and equality. Clearly 5+4 and 6+3 -
evaluate- to the same, but math doesn't define whether they are the
same, and in some sense the question isn't asked ordinarily, or isn't
debated. I want to infer that math doesn't define the 'is' relation
as Python knows it.

I feel the documentation should state, 'the interpreter is free to
return a -new- equivalent non-identical object in the case of
immutables.'

My tests:
>>a= -6
a is -6
False
>>-6 is -6
True

I don't know a convincing argument for the truth of Is( -6, -6 ).
Perhaps you could make one, or one for the permissibility of Is( a,
b ) & ~Equal( a, b )... identical non-equivalent.
Aug 15 '08 #3
On Thu, 14 Aug 2008 20:44:32 -0700, castironpi wrote:
For

a= 6
b= a

the test

a is b

should clearly return true.
Since Python promises not to make a copy of a when you execute "b = a",
then I think that such behaviour is guaranteed by the language.

Python distinguishes what mathematics does
not, between identity and equality. Clearly 5+4 and 6+3 - evaluate- to
the same, but math doesn't define whether they are the same, and in some
sense the question isn't asked ordinarily, or isn't debated. I want to
infer that math doesn't define the 'is' relation as Python knows it.
Mathematicians often *define* equality as identity. That certainly makes
sense when dealing with numbers -- what would it mean to say that there
are (say) three different instances of the abstract integer 42, all equal
yet not identical? I suggest that this simply doesn't make sense -- it is
"not even wrong".

Equality-as-identity may not hold in all areas of mathematics, but I
think it is safe to say it holds for ideal (abstract) numbers, as opposed
to implementations of numbers as bit patterns or objects in memory.

http://en.wikipedia.org/wiki/Equality_(mathematics)

--
Steven
Aug 15 '08 #4
well, i'm glad i stumbled upon this detail early on (i only had to fix
about one page of code)... i'll just stick to 'is' when it concerns
checking if it is the *same* object (memorywise) instead of an
*equivalent* one...
just before wrapping up, the special methods __eq__ and __ne__ are
called upon == and !=, right? not for 'is', 'is not'...

Steven D'Aprano wrote:
>
Mathematicians often *define* equality as identity. That certainly makes
sense when dealing with numbers -- what would it mean to say that there
are (say) three different instances of the abstract integer 42, all equal
yet not identical? I suggest that this simply doesn't make sense -- it is
"not even wrong".

Equality-as-identity may not hold in all areas of mathematics, but I
think it is safe to say it holds for ideal (abstract) numbers, as opposed
to implementations of numbers as bit patterns or objects in memory.

http://en.wikipedia.org/wiki/Equality_(mathematics)

but who knows? maybe abstract numbers -5 to 256 are optimized as well! :-)
Aug 15 '08 #5
ariel ledesma wrote:
well, i'm glad i stumbled upon this detail early on (i only had to fix
about one page of code)... i'll just stick to 'is' when it concerns
checking if it is the *same* object (memorywise) instead of an
*equivalent* one...
just before wrapping up, the special methods __eq__ and __ne__ are
called upon == and !=, right? not for 'is', 'is not'...
Correct! You can't change the identity operator 'is' with a magic method.

Christian

Aug 15 '08 #6

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

Similar topics

7
by: pj | last post by:
Why does M$ Query Analyzer display all numbers as positive, no matter whether they are truly positive or negative ? I am having to cast each column to varchar to find out if there are any...
5
by: Subrahmanyam Arya | last post by:
Hi Folks , I am trying to solve the problem of reading the numbers correctly from a serial line into an intel pentium processor machine . I am reading 1 byte and 2byte data both positive...
11
by: John | last post by:
Hi, I encountered a strange problem while debugging C code for a Windows-based application in LabWindows CVI V5.5, which led me to write the test code below. I tried this code with a different...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
11
by: drtimhill | last post by:
I'm just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled. For example, to get the last character in a string, I can enter "x". To get the...
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
23
by: Hallvard B Furuseth | last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is negative zero. So for signed types, x == 0 does not guarantee (x & foo) == 0. Is that right? (Not that I expect to ever encounter a...
20
by: Casey | last post by:
Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including...
6
by: ariel ledesma | last post by:
hello guys i just ran into this when comparing negative numbers, they start returning False from -6 down, but only when comparing with 'is' True False True i read that 'is' compares if...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...

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.