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.
Octaves on a piano are not tuned pure.
Because of inharmonicity, the higher partials of a single piano string are slightly sharper than theory would predict.
Ideal harmonic series above 100Hz: 100 200 300 400 etc
Actual harmonic series above 100Hz (approximation): 100 200.05 300.2 400.6
The higher the partial; the sharper the pitch.
Shorter piano strings have a higher inharmonicity.
For an octave to sound in tune, all the partials of each note have to be as in tune with each other as possible.
If the 2nd partial of the top note equals the 4th partial of the bottom, it is called a 4:2 octave.
In tune octaves are wide 2:1's meaning the frequencies of the notes would be something like 100Hz and 200.2Hz. The culmination of this slightly sharper top note adds up to sharp treble notes, and flat bass notes, but this is relative to a tuner with no stretch. The octaves sound fine.
Any single string creates harmonics as it rings. Up to eight parts a C would generate C C G CEG Bb and C, essentially a dominant 7th chord. The lower partials are louder and than the upper partials due to string length influencing volume. A longer portion of the string vibrating will be louder. Tempered tuning requires that octaves and fifths sound good while the other weaker relationships can be slightly out. Our brains interpret wobbling major thirds as shimmer rather than being seriously out since that would be more noticeable at the octave for example. The upper partials sound sharp compared to what we define as proper tuning in tempered tuning. The sharpness is natural but would prevent us from playing in different keys. When an upper note is slightly flat your brain will interpret this as unnatural and object more than an upper note that is slightly sharp. Notes have a certain range of sharpness before they become objectionable. If you play a root and a fifth on a guitar you can bend the fifth ever so slightly sharp and it still sounds okay. The problem with the piano is that if you tune the low notes to standard pitch, the upper notes would sound slightly sour at standard pitch. Therefore the most used notes in the midrange should be at standard pitch allowing upper notes to be sweetened by being slightly sharp and the lower notes tuned slightly flat for the same reason. This would mimic the natural state of things. The length of string and thickness play a role as well as mentioned above. This is generally the reason why short scale upright pianos don't sound so great at the low and high range compared to a grand piano.
The technical term for this practice in pianos is called stretch tuning. Read the Wikipedia article here.
Edouard provided a perfect explanation. I don't have to add anything to that. Just a couple of comments: