It may be my lack of unerstanding, but round() seems to me to give inconsistent results when rounding 5s as in the following examples?> round(1.45, 1)[1] 1.4 # OK> round(2.45, 1)[1] 2.5 # shouldn't this be 2.4?> round(1.05, 1)[1] 1.1 # 1.0 ? and signif():> signif(2.445, 3)[1] 2.44 # OK> signif(3.445, 3)[1] 3.45 # 3.44 ?> version_ platform i586-pc-linux-gnu arch i586 os linux-gnu system i586, linux-gnu status major 1 minor 6.2 year 2003 month 01 day 10 language R Many thanks! Rashid Nassar
Consider the following: > tst <- .5-1e-9 > tst [1] 0.5 > round(tst) [1] 0 > tst1 <- .5+1e-9 > tst1 [1] 0.5 > round(tst1) [1] 1 Does this answer the question? Spencer Graves #################################################### Rashid Nassar wrote:> It may be my lack of unerstanding, but round() seems to me to give > inconsistent results when rounding 5s as in the following examples? > > >>round(1.45, 1) > > [1] 1.4 # OK > > >>round(2.45, 1) > > [1] 2.5 # shouldn't this be 2.4? > > >>round(1.05, 1) > > [1] 1.1 # 1.0 ? > > and signif(): > > >>signif(2.445, 3) > > [1] 2.44 # OK > > >>signif(3.445, 3) > > [1] 3.45 # 3.44 ? > > > >>version > > _ > platform i586-pc-linux-gnu > arch i586 > os linux-gnu > system i586, linux-gnu > status > major 1 > minor 6.2 > year 2003 > month 01 > day 10 > language R > > Many thanks! > > Rashid Nassar > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
At 01:53 PM 3/15/2003, Rashid Nassar wrote: >It may be my lack of unerstanding, but round() seems to me to give >inconsistent results when rounding 5s as in the following examples? Not really. Floating point numbers can't be precisely represented in binary and so the internal representation of 1.5 might be very different from what you think it is. If you've done any programming at all, this is one of the first lessons you learn about "real" numbers and computers. Dr. Marc R. Feldesman Professor and Chairman Emeritus Anthropology Department - Portland State University email: feldesmanm at pdx.edu email: feldesman at attglobal.net fax: 503-725-3905 "Sometimes the lights are all shining on me, other times I can barely see, lately it's occurred to me, what a long strange trip it's been..." Jerry & the boys
Actually, under the default IEEE rounding mode, decimal values ending in 5 are always rounded to the nearest even value. This intended to ensure that rounding does not systematically bias computations in one direction, as would happen if 5's were always rounded either up or down. A very useful review of the issues in floating point computations on computer and under IEEE standard arithmetic is the paper "What Every Computer Scientist Should Know About Floating-Point Arithmetic", by David Goldberg, published in the March, 1991 issue of Computing Surveys. This paper is reprinted on the Sun website at http://docs.sun.com/source/806-3568/ncg_goldberg.html. -Greg> -----Original Message----- > From: Marc R. Feldesman [mailto:feldesmanm at pdx.edu] > Sent: Saturday, March 15, 2003 5:49 PM > To: Rashid Nassar; r-help > Subject: Re: [R] round() seems inconsistent when rounding 5s > > > At 01:53 PM 3/15/2003, Rashid Nassar wrote: > >It may be my lack of unerstanding, but round() seems to me to give > >inconsistent results when rounding 5s as in the following examples? > > Not really. Floating point numbers can't be precisely represented in > binary and so the internal representation of 1.5 might be > very different > from what you think it is. If you've done any programming at > all, this is > one of the first lessons you learn about "real" numbers and computers. > > > Dr. Marc R. Feldesman > Professor and Chairman Emeritus > Anthropology Department - Portland State University > email: feldesmanm at pdx.edu > email: feldesman at attglobal.net > fax: 503-725-3905 > > > "Sometimes the lights are all shining on me, other times I > can barely see, > lately it's occurred to me, what a long strange trip it's > been..." Jerry & > the boys > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >LEGAL NOTICE\ Unless expressly stated otherwise, this message is... [[dropped]]