"how numbers are stored and used in computers"
Cosine distance is a measure of similarity between two strings by treating them as vectors, and computing the cosine of the angle between them. It is particularly useful for comparing documents or text strings where the order of words is not as important as their frequency.
The cosine distance
The vectors
Cosine similarity is a measure of similarity between two vectors.
code.ts1function cosineSimilarity(A: number[], B: number[]): number { 2 if (A.length !== B.length) { 3 throw new Error('Vectors must be of the same length'); 4 } 5 6 let dotProduct = 0; 7 let magnitudeA = 0; 8 let magnitudeB = 0; 9 10 for (let i = 0 ; i < A.length ; i++) { 11 dotProduct += A[i] * B[i]; 12 magnitudeA += A[i] * A[i]; 13 magnitudeB += B[i] * B[i]; 14 } 15 16 return dotProduct / (Math.sqrt(magnitudeA) * Math.sqrt(magnitudeB)); 17}