Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer conversion inconsistent #47274

Closed
helminthe mannequin opened this issue Jun 1, 2008 · 6 comments
Closed

Integer conversion inconsistent #47274

helminthe mannequin opened this issue Jun 1, 2008 · 6 comments

Comments

@helminthe
Copy link
Mannequin

helminthe mannequin commented Jun 1, 2008

BPO 3024
Nosy @loewis, @rhettinger

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2008-06-01.22:49:54.068>
created_at = <Date 2008-06-01.22:31:17.017>
labels = ['invalid']
title = 'Integer conversion inconsistent'
updated_at = <Date 2008-06-01.22:51:35.530>
user = 'https://bugs.python.org/helminthe'

bugs.python.org fields:

activity = <Date 2008-06-01.22:51:35.530>
actor = 'rhettinger'
assignee = 'none'
closed = True
closed_date = <Date 2008-06-01.22:49:54.068>
closer = 'loewis'
components = ['None']
creation = <Date 2008-06-01.22:31:17.017>
creator = 'helminthe'
dependencies = []
files = []
hgrepos = []
issue_num = 3024
keywords = []
message_count = 6.0
messages = ['67609', '67612', '67613', '67614', '67615', '67616']
nosy_count = 3.0
nosy_names = ['loewis', 'rhettinger', 'helminthe']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue3024'
versions = ['Python 2.5']

@helminthe
Copy link
Mannequin Author

helminthe mannequin commented Jun 1, 2008

This issue is probably older than I am, and was amazed to discover it in
python:
Python 2.5.2 (r252:60911, May  7 2008, 15:19:09)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
>>> int(float("-23.15"))
-23
>>> int(float("-23.65"))
-23
>>> int(float("24.9"))
24
>>> int(float("24.4"))
24
Is this by design? What is the python way of obtaining a correct result?

@loewis
Copy link
Mannequin

loewis mannequin commented Jun 1, 2008

I don't see any problem with that result? Why do you consider the result
incorrect, and what "correct" result would you have inspected instead?

Notice that int conversion of floats truncates them, by definition.

@helminthe
Copy link
Mannequin Author

helminthe mannequin commented Jun 1, 2008

Hello,
As far as I know, the correct conversion is to either round to the
nearest, or to the smaller integer, but not both.

@helminthe
Copy link
Mannequin Author

helminthe mannequin commented Jun 1, 2008

Sorry for not writing completely above - python does neither, it rounds
to the integer closest to zero

@loewis
Copy link
Mannequin

loewis mannequin commented Jun 1, 2008

I see. There is no such thing as a "correct" conversion from real
numbers to integer numbers. Instead, there are various approaches,
called "truncating", "rounding", "flooring", and "ceiling". Python's
default conversion is truncation, and it is consistent in doing so:
int(x) will return the nearest integer between 0 and x.

If you want rounding, use the round builtin.

@loewis loewis mannequin closed this as completed Jun 1, 2008
@loewis loewis mannequin added the invalid label Jun 1, 2008
@rhettinger
Copy link
Contributor

This is exactly what int() is supposed to do. For other kinds of
rounding, look at round(), math.floor(), math.ceil(), and many rounding
options in the decimal module:

from decimal import Decimal
>>> Decimal('-23.15').to_integral(ROUND_FLOOR)
Decimal("-24")
>>> Decimal('-23.15').to_integral(ROUND_CEILING)
Decimal("-23")
>>> Decimal('-23.15').to_integral(ROUND_DOWN)
Decimal("-23")
>>> Decimal('-23.15').to_integral(ROUND_HALF_EVEN)
Decimal("-23")

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant