home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!stanford.edu!kronos.arc.nasa.gov!iscnvx!enterprise!news
- From: gumerman@aspen.ops.lmsc.lockheed.com ()
- Subject: Rounding floating point numbers in C
- Message-ID: <1992Nov20.215133.3271@enterprise.rdd.lmsc.lockheed.com>
- Lines: 61
- Sender: news@enterprise.rdd.lmsc.lockheed.com
- Nntp-Posting-Host: aspen.ops.lmsc.lockheed.com
- Reply-To: gumerman@aspen.ops.lmsc.lockheed.com ()
- Organization: Lockheed Missles & Space Co.
- Date: Fri, 20 Nov 92 21:51:33 GMT
-
-
- Hi everybody,
-
- I have been writing come C code using VAX VMS C. Along the way I have run
- into a problem with the way the printf function rounds floating point numbers.
- The way numbers are rouned is totally inconsistent. Sometimes numbers are
- rounded up sometimes they are rounded down. What is going on here?
-
-
- Below is an example program and its output.
-
- #include "sourcedir:include_file.c"
- #include unixio
- #include file
- float one=0.0,two=0.0,three=0.0,four=0.0,five=0.0,six=0.0,seven=0.0,eight=0.0;
- float nine=0.0;
-
- main()
- {
- one = (float) 1.15;
- two = (float) 1.25;
- three = (float) 1.35;
- four = (float) 1.45;
- five = (float) 1.55;
- six = (float) 1.65;
- seven = (float) 1.75;
- eight = (float) 1.85;
- nine = (float) 1.95;
-
- printf("1.15 %1.1f\n",one);
- printf("1.25 %1.1f\n",two);
- printf("1.35 %1.1f\n",three);
- printf("1.45 %1.1f\n",four);
- printf("1.55 %1.1f\n",five);
- printf("1.65 %1.1f\n",six);
- printf("1.75 %1.1f\n",seven);
- printf("1.85 %1.1f\n",eight);
- printf("1.95 %1.1f\n",nine);
-
- }
-
-
- OUTPUT:
-
-
- 1.15 1.1 /* round down */
- 1.25 1.3 /* round up */
- 1.35 1.4 /* round up */
- 1.45 1.5 /* round up */
- 1.55 1.5 /* round down */
- 1.65 1.6 /* round down */
- 1.75 1.8 /* round up */
- 1.85 1.9 /* round up */
- 1.95 2.0 /* round up */
-
- Does anyone know what is going on here?
-
- Any help is appreciated,
-
- Signed the insane programmer.
-
-