I use the code below to calculate log base 2.
Math.log(x)/Math.log(2);
With this function, i get nan if x=4/5.
With calc, excel this problem don’t exist.
How to avoid to get nan and get the real value?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I have a strong feeling that your
4/5is integers. Which would mean that4/5will evaluate to0due to integer division.log(0)is negative infinity, that could be the source of yourNaN.To fix this, cast your numbers to floating-point before you do the division: