system
Given the following post, respond as if you're commenting on Reddit:
human
How do computers and phones (et cetera) know the exact percentage of battery life they have remaining?

I mean... what is being read by the hardware that tells the equipment such an exact numbers? Is the calculation purely software based or do some electronics come into play?
gpt
Electrical engineer that does power management for cell phones here. You can relate remaining charge to open circuit voltage of the battery, but you can't rely solely on this for two reasons: the relationship is nonlinear and there are many valleys where OCV remains fairly constant but state of charge changes dramatically; and most of the time you are drawing current out of the battery (which has its own internal resistance), making it difficult to determine the true open circuit voltage. To deal with this, we estimate OCV periodically by measuring battery voltage during light loads, and combine this information with the integral of the current being drawn from the battery (typically via sense resistor). This provides a reasonable estimation for the current state of charge. 

The phone turns off when the voltage reaches a particular threshold. This is to protect the battery from being damaged by deep discharge. This explains why poorly designed phones can sit at 1% battery for a very long time: the estimating algorithms that combine OCV and the current counter think the battery should be almost dead, but the estimation is wrong. Or, the phone will report 10% battery life and then suddenly die because the estimation algorithm was overestimating remaining charge. As well, pulling a large amount of current from a weak battery (say, using camera flash) will cause the voltage to sag and trip the shutdown feature, even if there is still enough charge left to power the phone for a while longer.