[R] trunc/floor a number -- strange bahaviour

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Feb 12 16:35:16 CET 2009

That is not strange behaviour. It's a case of FAQ 7.31: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f 



ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be 

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Žroutík
Verzonden: donderdag 12 februari 2009 16:20
Aan: r-help at r-project.org
Onderwerp: [R] trunc/floor a number -- strange bahaviour

Hi everybody,

given a fresh rgui.exe load on winxp OS, I enter (a minimal exaple)

n <- 12.357531

Then the following command:
n <- (n - floor(n))*10; n

gives the following outputs:
[1] 3.57531
[1] 5.7531
[1] 7.531
[1] 5.31
[1] 3.1
[1] 1         === still as expected
[1] 10        === not expected, count with me: 1 - floor(1) is zero, times
10 gives 0, not 10!!!!
[1] 10        === should stay forever zero (0)
[1] 10
[1] 9.999998
[1] 9.999982
[1] 9.999821
[1] 9.998212

The sama happens with trunc().
e.g. (a minimal exaple)

n <- 0.245
n <- (n - trunc(n))*10; n
[1] 2.45
[1] 4.5
[1] 5
[1] 1.776357e-13 ===== zero expected!!!
[1] 1.776357e-12

And I'm asking "what the heck?!" and where is the bug in my examples? Any
suggestion well appreciated.

p.s. The expression with floor() and trunc() are to be implemented in a
function which gives a value equal precision order of the given number. e.g.
12.345 would have (-3), 12.1 would have (-1), 12 would have (0) and e.g.
12000 would have the order of the precision (4). Basically, it is the order
of the last given non-zero digit.

	[[alternative HTML version deleted]]

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.

More information about the R-help mailing list