RHEL Engineering is moving the tracking of its product development work on RHEL 6 through RHEL 9 to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "RHEL project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs in the statuses "NEW", "ASSIGNED", and "POST" are being migrated throughout September 2023. Bugs of Red Hat partners with an assigned Engineering Partner Manager (EPM) are migrated in late September as per pre-agreed dates. Bugs against components "kernel", "kernel-rt", and "kpatch" are only migrated if still in "NEW" or "ASSIGNED". If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "RHEL project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/RHEL-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.
Bug 1261450 - wrong rounding of some numbers
Summary: wrong rounding of some numbers
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Enterprise Linux 7
Classification: Red Hat
Component: bash
Version: 7.1
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: rc
: ---
Assignee: Ondrej Oprala
QA Contact: BaseOS QE - Apps
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2015-09-09 11:03 UTC by Martin Kyral
Modified: 2016-02-01 02:10 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2015-09-09 11:57:22 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)

Description Martin Kyral 2015-09-09 11:03:43 UTC
Description of problem:
bash doesn't round numbers like 0.5, 2.5, 4.5... correctly. Interestingly, 1.5, 3.5 and so on are OK. See the snapshot from the test: 

:: [   LOG    ] :: printf is a shell builtin
:: [   PASS   ] :: Rounding 0.49 (Assert: '"0"' should equal '"0"')
:: [   FAIL   ] :: Rounding 0.5 (Assert: '"0"' should equal '"1"')
:: [   PASS   ] :: Rounding 0.51 (Assert: '"1"' should equal '"1"')
:: [   PASS   ] :: Rounding 0.9 (Assert: '"1"' should equal '"1"')
:: [   PASS   ] :: Rounding 1.01 (Assert: '"1"' should equal '"1"')
:: [   PASS   ] :: Rounding 1.5 (Assert: '"2"' should equal '"2"')
:: [   PASS   ] :: Rounding 1.9 (Assert: '"2"' should equal '"2"')
:: [   PASS   ] :: Rounding 4.49 (Assert: '"4"' should equal '"4"')
:: [   FAIL   ] :: Rounding 4.50 (Assert: '"4"' should equal '"5"')
:: [   PASS   ] :: Rounding 4.51 (Assert: '"5"' should equal '"5"')
:: [   PASS   ] :: Rounding 5.01 (Assert: '"5"' should equal '"5"')
:: [   PASS   ] :: Rounding 9.9999 (Assert: '"10"' should equal '"10"')
:: [   PASS   ] :: Rounding 10 (Assert: '"10"' should equal '"10"')
:: [   PASS   ] :: Rounding 10.0001 (Assert: '"10"' should equal '"10"')
:: [   PASS   ] :: Rounding 1.50 (Assert: '"2"' should equal '"2"')
:: [   FAIL   ] :: Rounding 2.50 (Assert: '"2"' should equal '"3"')
:: [   PASS   ] :: Rounding 3.50 (Assert: '"4"' should equal '"4"')
:: [   FAIL   ] :: Rounding 4.50 (Assert: '"4"' should equal '"5"')
:: [   PASS   ] :: Rounding 5.50 (Assert: '"6"' should equal '"6"')
:: [   FAIL   ] :: Rounding 6.50 (Assert: '"6"' should equal '"7"')
:: [   PASS   ] :: Rounding 7.50 (Assert: '"8"' should equal '"8"')
:: [   FAIL   ] :: Rounding 8.50 (Assert: '"8"' should equal '"9"')
:: [   PASS   ] :: Rounding 9.50 (Assert: '"10"' should equal '"10"')
:: [   FAIL   ] :: Rounding 10.50 (Assert: '"10"' should equal '"11"')

The reproducer is simple:

[0 root@qeos-27 test]# LANG=C bash -c 'printf "%0.f\n" 0.5'
0
[0 root@qeos-27 test]# LANG=C ksh -c 'printf "%0.f\n" 0.5'
1
[0 root@qeos-27 test]#



Version-Release number of selected component (if applicable):
bash-4.2.46-19.el7

How reproducible:


Steps to Reproduce:
1. LANG=C bash -c 'printf "%0.f\n" 0.5'
2.
3.

Actual results:
0


Expected results:
1


Additional info:

Comment 1 Ondrej Oprala 2015-09-09 11:57:22 UTC
Hi, this is called "rounding to even". It is extremely well explained here [1] (see the last example for a case similar to your reproducer) and
it's how the glibc printf (which bash calls) is implemented. Ksh usually handles math stuff with its own library and probably follows a different set of rules, you'll have to ping Michal about that. The standalone printf binary from coreutils also follows the same rules bash does.

[1] http://linuxgazette.net/144/misc/lg/a_question_of_rounding_in_issue_143.html


Note You need to log in before you can comment on or make changes to this bug.