Flutterby™! : Why display rounding can get you in

Next unread comment / Catchup all unread comments User Account Info | Logout | XML/Pilot/etc versions | Long version (with comments) | Weblog archives | Site Map | | Browse Topics

Why display rounding can get you in

2016-03-11 22:45:10.114636+01 by Dan Lyke 1 comments

Why display rounding can get you in trouble:

$ perl -le 'print (125.85/41.95); print int(125.85/41.95);'

3

2

$ python
    Python 2.7.5 (default, Nov 20 2015, 02:00:19) 
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print (125.85/41.95)
    3.0
    >>> print int(125.85/41.95)
    2

[ related topics: Perl Open Source hubris ]

comments in ascending chronological order (reverse):

#Comment Re: Why display rounding can get you in made: 2016-03-12 00:25:18.821903+01 by: Dan Lyke

Courtesy of William Holmes:

echo 'public class t { public static void main(String[] args) throws Exception { System.out.format("%.1f\n", 125.85/41.95); System.out.format("%d\n", java.lang.Double.valueOf(125.85/41.95).intValue()); System.out.format("%d\n", (int)java.lang.Math.round(125.85/41.95)); } }' > t.java ; javac t.java ; java -cp . t 3.0

echo 'int main(void){printf("%.1f\n", 125.85/41.95);printf("%d\n", (int) (125.85/41.95));printf("%d\n", (int)round(125.85/41.95));}' | gcc -w -std=c99 -xc -o t - ; ./t