Is there any formula that directly relate density to altitude for a wide range of altitude?
There are the right way and the easy way.

Rigorous way would be to use the ICAO Standard Atmosphere (Doc 7488-CD in 1993) a link for document
Then your code looks for temperature gradient (Table D) and uses eq.12 or 13 for pressure calculation. It depends on the temperature gradient. Then you can easily compute the density eq.14.

Easy way would be calculate density for few altitudes and fit the result with some proper function.


Cheers Robert