473,491 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2371
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
8205
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
6101
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
2358
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
35437
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
3702
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
3959
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
4333
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
5772
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
5984
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
6978
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
7190
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...
1
6858
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...
0
7360
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...
0
5451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1392
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 ...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.