Libraries generally don't significantly undercut sales because the bulk of sales occur during the early high demand period, when the library's limited number of copies force waiting lists so that those who want the item sooner rather than later buy it.
This was the basis of an interesting law review article published by Justice Breyer back in his academic days, called "The Uneasy Case for Copyright", where he argued that because such a high fraction of the profits from a work occurred from sales shortly after publication, before copiers had time to typeset and get into print unauthorized copies, there might not be a need for copyright law.
Since then, copying technology has gotten much better, and so Breyer's argument is no longer plausible.