473,670 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Decimal vs Float comparasion

Hi,

It seems decimal object will always be larger than float in
comparasion, which goes against common sense:
>>from decimal import Decimal
a = Decimal('0.5')
a 99999
False
>>a 99999.0
True

It seems to me that rather than allowing this to happen, comparasion
between the two should either be made correct (by convertion decimal
to float e.g.) or forbidden, like arithmatic operations between the
two types.

--
Hong Yuan

´ó¹Ü¼ÒÍøÉϽ¨²Ä³ ¬ÊÐ
×°ÐÞ×°ä꽨²ÄÒ»Õ ¾Ê½¹ºÎï
http://www.homemaster.cn
Jun 27 '08 #1
4 1466
I still don't see why such a module exists.

On 5 mayo, 21:52, "Yuan HOng" <hongyuan1...@g mail.comwrote:
Hi,

It seems decimal object will always be larger than float in
comparasion, which goes against common sense:
>from decimal import Decimal
a = Decimal('0.5')
a 99999
False
>a 99999.0

True

It seems to me that rather than allowing this to happen, comparasion
between the two should either be made correct (by convertion decimal
to float e.g.) or forbidden, like arithmatic operations between the
two types.

--
Hong Yuan

´ó¹Ü¼ÒÍøÉϽ¨²Ä³ ¬ÊÐ
×°ÐÞ×°ä꽨²ÄÒ»Õ ¾Ê½¹ºÎïhttp://www.homemaster.cn
Jun 27 '08 #2
Gasto wrote:
I still don't see why such a module exists.
There are 2.0 types of programmers: those who always use floating point,
and those who know how to use them.
Jun 27 '08 #3
Dennis Lee Bieber wrote:
On Tue, 6 May 2008 11:52:10 +0800, "Yuan HOng" <ho**********@g mail.com>
declaimed the following in comp.lang.pytho n:
>It seems to me that rather than allowing this to happen, comparasion
between the two should either be made correct (by convertion decimal
to float e.g.) or forbidden, like arithmatic operations between the
two types.

Why should decimal be coerced to float? Maybe float should be
coerced to decimal?

Or... the programmer should explicitly specify what comparison is
wanted -- if any...

Or... Isn't Python 3.x supposed to forbid mixed type comparisons
unless the types implement suitable handling?
Bottom line is that it shouldn't silently return something insane.
99999.0 is surely exactly representable in any modern floating point
system, being a floating point representing of an integer, so silently
returning a completely invalid comparison is a tremendously bad idea.

It's a bug.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
Can I walk with you / 'Till the day that the world stops turning
-- India Arie
Jun 27 '08 #4
On May 6, 1:31 am, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
On Tue, 6 May 2008 11:52:10 +0800, "Yuan HOng" <hongyuan1...@g mail.com>
declaimed the following in comp.lang.pytho n:
It seems to me that rather than allowing this to happen, comparasion
between the two should either be made correct (by convertion decimal
to float e.g.) or forbidden, like arithmatic operations between the
two types.

Why should decimal be coerced to float? Maybe float should be
coerced to decimal?

Or... the programmer should explicitly specify what comparison is
wanted -- if any...

Or... Isn't Python 3.x supposed to forbid mixed type comparisons
unless the types implement suitable handling?
Yes, it is fixed in 3.0. Unfortunately it's well established
behaviour in 2.x, so it won't be changing there. Don't bother
reporting a bug about this unless it's about 3.0.
Jun 27 '08 #5

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

Similar topics

21
4519
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
0
1170
by: Batista, Facundo | last post by:
People: I'll post a reviewed version of the PEP. The only differences with the previous one will be the treatmen to float in both explicit and implicit construction: ------------ In Explicit construction:
3
2237
by: jbauer | last post by:
I was interested in playing around with Decimal and subclassing it. For example, if I wanted a special class to permit floats to be automatically converted to strings. from decimal import Decimal class MyDecimal(Decimal): def __init__(self, value): if isinstance(value, float):
15
12813
by: Kay Schluehr | last post by:
I wonder why this expression works: >>> decimal.Decimal("5.5")**1024 Decimal("1.353299876254915295189966576E+758") but this one causes an error 5.5**1024 Traceback (most recent call last):
18
12160
by: Kuljit | last post by:
I am doing Engineering(B.Tech) in Computer Science. I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the number of digits in a given number. I request to help me out in solving the below said question. Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case).
25
3022
by: Lennart Benschop | last post by:
Python has had the Decimal data type for some time now. The Decimal data type is ideal for financial calculations. Using this data type would be more intuitive to computer novices than float as its rounding behaviour matches more closely what humans expect. More to the point: 0.1 and 0.01 are exact in Decimal and not exact in float. Unfortunately it is not very easy to access the Decimal data type. To obtain the decimal number 12.34 one...
0
808
by: Wojciech Walczak | last post by:
2008/5/6, Yuan HOng <hongyuan1306@gmail.com>: Looks like a nasty bug. a 99999.0 returns True because NotImplemented 99999.0 returns True. a < 99999.0 returns False because NotImplemented < 99999.0 returns False. As you can see the real comparision has nothing to do with your Decimal number. I think you can report it at bugs.python.org.
17
6789
by: D'Arcy J.M. Cain | last post by:
I'm not sure I follow this logic. Can someone explain why float and integer can be compared with each other and decimal can be compared to integer but decimal can't be compared to float? True True False This seems to break the rule that if A is equal to B and B is equal to C then A is equal to C.
6
3698
by: Terry Reedy | last post by:
Gerhard Häring wrote: The new fractions module acts differently, which is to say, as most would want. True Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> F(1.0) File "C:\Program Files\Python30\lib\fractions.py", line 97, in __new__
0
8901
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8659
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7412
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6212
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4388
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
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
2
2037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1791
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.