Red Hat Bugzilla – Bug 1261450

wrong rounding of some numbers

Last modified: 2016-01-31 21:10:54 EST

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:

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