cost function returns a non-negative result in tests