> as.character(seq(-.25,.95,.1))[1] "-0.25" "-0.15" "-0.05" "0.05" "0.15" "0.25" "0.35" "0.45" "0.55" "0.65" "0.75" "0.85" "0.95" > as.character(seq(-.35,.95,.1)) [1] "-0.35" "-0.25" "-0.15" "-0.0499999999999999" "0.05" [6] "0.15" "0.25" "0.35" "0.45" "0.55" [11] "0.65" "0.75" "0.85" "0.95" Not a big deal, just curiosity: Why do I obtain this "ugly" "-0.0499999999999999" instead of the expected "-0.05" ? Regards K.
see FAQ 7.31 On Nov 20, 2007 10:50 AM, Ken Fullish <ken.mailinglist at mac.com> wrote:> > > as.character(seq(-.25,.95,.1)) > [1] "-0.25" "-0.15" "-0.05" "0.05" "0.15" "0.25" "0.35" "0.45" > "0.55" "0.65" "0.75" "0.85" "0.95" > > > as.character(seq(-.35,.95,.1)) > [1] "-0.35" "-0.25" > "-0.15" "-0.0499999999999999" "0.05" > [6] "0.15" "0.25" > "0.35" "0.45" "0.55" > [11] "0.65" "0.75" > "0.85" "0.95" > > Not a big deal, just curiosity: > Why do I obtain this "ugly" "-0.0499999999999999" instead of the > expected "-0.05" ? > > Regards > K. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
On 11/20/2007 10:50 AM, Ken Fullish wrote:> > as.character(seq(-.25,.95,.1)) > [1] "-0.25" "-0.15" "-0.05" "0.05" "0.15" "0.25" "0.35" "0.45" > "0.55" "0.65" "0.75" "0.85" "0.95" > > > as.character(seq(-.35,.95,.1)) > [1] "-0.35" "-0.25" > "-0.15" "-0.0499999999999999" "0.05" > [6] "0.15" "0.25" > "0.35" "0.45" "0.55" > [11] "0.65" "0.75" > "0.85" "0.95" > > Not a big deal, just curiosity: > Why do I obtain this "ugly" "-0.0499999999999999" instead of the > expected "-0.05" ?Because as.character() tries to do an accurate conversion, and the number in your vector is closer to -0.0499999999999999 than to -0.05. You could get the "-0.05" by something like round( seq(...), 2). The reason seq() doesn't give you exactly -0.05 is that the starting values and step size you've chosen are not exactly representable in R's floating point format. It can only store fractions exactly when the denominator is a power of 2. Duncan Murdoch
Seemingly Similar Threads
- Puzzled with contour()
- Question about subsetting S4 object in ROCR
- Difficulties with dataframe filter using elements from an array created using a for loop or seq()
- problem with heatmap.2 in package gplots generating non-finite breaks
- Problem having tick marks aligned when plotting three graphs on top of one another.