You need to read up on floating point arithmetic: this question doesn't cover your exact question, but gives useful references (more here). The difference between the two examples is that in the first case the addends are on very different scales, while in the second case they are of similar magnitudes. ?.Machine
will give you some of the relevant values, especially .Machine$double.eps
(which is the smallest number such that 1+x > x
) (although it won't explain floating point arithmetic for you).
(@BrianMontgomery's answer points out that technically this is only true if you use a <- 2^(-53)
(or smaller).)
(I don't think this answer is great, but it will do until someone wants to come along and explain the floating point issues underlying this example in more detail.)