Implement a function to calculate cosine similarity for high-dimensional embeddings efficiently. The function receives two equal-length numeric lists and returns their cosine similarity as a float. Return 0.0 if either vector has zero magnitude. Use the signature cosine_similarity(a, b). For example, [1, 0] and [0, 1] return 0.0, while [1, 2] and [1, 2] return 1.0. Inputs contain between 1 and 2,000 finite numeric values.
def cosine_similarity(a, b):