Piecewise functions and calculations