  We make a detailed study of matter density perturbations in both metric and
Palatini formalisms in theories whose Lagrangian density is a general function,
f(R), of the Ricci scalar. We derive these equations in a number of gauges. We
show that for viable models that satisfy cosmological and local gravity
constraints (LGC), matter perturbation equations derived under a sub-horizon
approximation are valid even for super-Hubble scales provided the oscillating
mode (scalaron) does not dominate over the matter-induced mode. Such
approximate equations are especially reliable in the Palatini formalism because
of the absence of scalarons.
  Using these equations we make a comparative study of the behaviour of density
perturbations as well as gravitational potentials for a number of classes of
theories. In the metric formalism the parameter m=Rf_{,RR}/f_{,R}
characterising the deviation from the Lambda CDM model is constrained to be
very small during the matter era in order to ensure compatibility with LGC, but
the models in which m grows to the order of 10^{-1} around the present epoch
can be allowed. These models also suffer from an additional fine tuning due to
the presence of scalaron modes which are absent in the Palatini case.
  In Palatini formalism LGC and background cosmological constraints provide
only weak bounds on |m| by constraining it to be smaller than ~ 0.1. This is in
contrast to matter density perturbations which, on galactic scales, place far
more stringent constraints on the present deviation parameter m of the order of
|m| < 10^{-5} - 10^{-4}. This is due to the peculiar evolution of matter
perturbations in the Palatini case which exhibits a rapid growth or a damped
oscillation depending on the sign of m.
