angeloskath/php-nlp-tools

Division by zero

formigone opened this issue · 3 comments

NlpTools\Similarity\CosineSimilarity::similarity() can divide by zero if $v1_norm or $v2_norm (or both) are equal to zero.

That is true.

What do you suggest to remedy this? RuntimeException ? InvalidArgumentException? return 0?

It currently returns false and issues a warning.

It should throw a UnexpectedValueException. I will work on this next.

UnexpectedValueException php.net
Typically this happens when a function calls another function and expects the return value to be of a certain type or value not including arithmetic or buffer related errors

So I am guessing InvalidArgumentException.