-1

this is kind of a specific situation to a problem the problem is it supposed to produce 0 when numen/domin =1 domin output shows 6 but the result shows NaN

when numen = 4 and domin = sqrt of 4 to the power of 2 which leads to 4/4 it shows the right answer same with all positive integers like 9,16,25,36 but if i put 6 it gives NaN

if I put 7 it shows 1.490116E-08

if I put 8 it shows 2.107342E-08

i know can state

if(numen==domin) result = 0;

but i really want to know why this is not working properly

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ps
{
    class Program
    {
         static void Main(string[] args)
        {
           double numen = 6;
           double domin = Math.Pow(Math.Sqrt(6), 2);
           Console.WriteLine(domin);
           double result = Math.Acos(numen /(domin));
           Console.WriteLine(result);
        }
    }
}
  • 3
    Read [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Zohar Peled Oct 25 '20 at 09:51
  • `Sqrt(6)` isn't an integer. So, there are good odds that `Math.Pow(Math.Sqrt(6), 2);` will give an approximation instead of `1` (in that case, `5.999999999999999`). Then, `6/5.999999999999999` will be slightly higher than 1. Use `decimal` instead – Cid Oct 25 '20 at 09:55
  • thank u for help!! i understand why it's not working – user13674310 Oct 25 '20 at 10:00

1 Answers1

1

Well it's basic maths here...

Values of sinus and cosinus mathematical values vary between -1 and 1, so domain of inverse functions, such as arcus sinus and arcus cosinus is range between -1 and 1.

In your example, numen/domin turns out to be 1.0000000000000002 due to rounding errors in Math lib. So when you invoke Math.Acos which is arcus cosinus domin is OUTSIDE of function domain, so value returned is NaN.

Cid
  • 14,968
  • 4
  • 30
  • 45
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • "1.0000000000000002 due to rounding errors in Math lib" i am aware of the range of the arcos just didn't know about that the math library has rounding error didn't expect it to be honest my question wasn't around when it's NaN it was around why it's NaN – user13674310 Oct 25 '20 at 21:07
  • @user13674310 So, the reason is rounding errors, which caused value to be outside domain of arcus cosinus, which in turn, caused `NaN` value. If my answer has helped you, you should accept it (green check mark on the left). – Michał Turczyn Oct 25 '20 at 21:50