[R] seq(*,by=) rounding problem {was 'Strange behaviour of "image"'}

Martin Maechler maechler at stat.math.ethz.ch
Thu Oct 3 13:24:43 CEST 2002


>>>>> "Ernesto" == Ernesto Jardim <ernesto at ipimar.pt>
>>>>>     on 03 Oct 2002 12:11:11 +0100 writes:

    Ernesto> Hi I'm using "image" and got some strange
    Ernesto> results. When I define the color sequence as
    Ernesto> "col=gray(seq(0.95,0,-0.01))" or
    Ernesto> "col=gray(seq(0.94,0,-0.01))" I got an error

    Ernesto> Error in gray(level) : invalid gray level, must be
    Ernesto> in [0,1].

    Ernesto> If I use 0.96 or 0.93 it works ...

yes, this is just floating point arithmetic, or "rounding" :
The following reveals the problem :

 > range(seq(0.94,0, by = -0.01))
 [1] -1.110223e-16  9.400000e-01
 > range(seq(0.93,0, by = -0.01))
 [1] 0.00 0.93
 > range(seq(0.96,0, by = -0.01))
 [1] 0.00 0.96

BTW, I usually never use "by=" in seq() exactly for this reason
(and rather use "length=" instead).

Martin
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list