My understanding is that the vast majority of western music uses equal temperament, i.e. all semitones have a frequency ratio of the 12th root of 2.
However I can hear in my piano that the notes at the low and high extremes of the keyboard are ever so slightly out of pitch. It seems that the lowest notes are slightly flat and the highest notes slightly sharp. This is a digital piano from Roland so they must be doing this on purpose. Why is this? I suppose normal pianos are like this too – is it done on purpose or is it a historical thing?
There are physical and psychoacoustics reasons behind it.
A vibrating string held by its two extremities can only vibrate at certain frequencies (cycles per second, expressed in Hertz, i.e. 440 Hz = 440 cycles/second), which relates to the characteristics of the string (e.g. its weight per unit of length, its flexibility) and how it is used (e.g. the vibrating length — which is fixed by the instrument and the player; how much force is used to tense it — which is how you tune a stringed instrument). These are called partials.
For an ideal string, that is a string which offers no resistance to being bent or rolled, there is a lowest frequency at which the string can vibrate, the fundamental; every other frequency at which the string can vibrate is a multiple of this frequency. These particular cases of partials are called harmonics.
For example, the string of a middle A in the piano can vibrate at 440Hz, 880Hz = 2*440Hz, 1320Hz = 3*440Hz, 1760Hz, … When you hit the string with the hammer, the string typically vibrates with a combination of several of these frequencies.
Now go an octave higher. The fundamental of that A is 880Hz, with harmonics at 1760Hz, 2640Hz, 3520Hz, … As you can see, the harmonics of the higher A all are harmonics of the lower A. Thus, they sound right together.
In the middle range of the piano, its strings can pretty accurately be considered perfect and partials tend to be true harmonics.
But if you go to the extreme range of the piano, it’s not the case any longer. The heavy string of the low notes can’t be rolled so easily (they are too large). This means that the partials are not harmonics any longer. For example (totally fictional and probably wildly inaccurate), the frequencies of a low A tuned to 55Hz could be 115Hz instead of 110Hz, 172Hz instead of 165Hz, … But this means that if you play a low 55Hz A with the A an octave higher, 110Hz, well, there is a 110Hz vibration (the higher A) and a 115 vibration (from the low A) at the same time. These conflict and you can hear that something is out of tune (you could hear a 5Hz beating, e.g.).
To avoid this, the low A is tuned flat, let’s say at 52Hz, with partials (still fictional) at 110Hz, 167Hz, … Now when you play the low A alone? Its fundamental is at 52Hz, which your trained ear might perceive as a bit flat. But when you play both As together? Now there is way less conflict between their partials. They sound good together.
Same with the high range on the piano: the very thin, highly constrained strings are far from an ideal string; their partials also are not true harmonics. So you tune them sharp (once again, to your trained ear) to ensure they sound good together with the lower strings.