1. Aug 20, 2011

### sara_87

1. The problem statement, all variables and given/known data

I put the following code into MATLAB:

d=(0.2+0.1)-0.2

y=(asin((d)/(0.1)))

d=0.100000000000000

and i know that d/0.1 is equal to 1. so, i expect the answer of y to be
y=1.570796326794897 (which is pi/2)
but, MATLAB gives:

y= 1.570796326794897 - 0.000000021073424i

2. Relevant equations

3. The attempt at a solution

if i put:
d=0.1
then it would give:
y= 1.570796326794897

But, i want to know why the answer is complex valued when it uses 0.1000000000000000 ?

2. Aug 20, 2011

### gb7nash

The only reason I can think of is rounding error. The computer thinks .1000000000000 > .1, so when computing d/.1, you obtain a value greater than 1. Taking the arcsin of a value greater than 1, as you know, does not exist for real numbers. This is why there's a small complex term which contributes to the answer.

3. Aug 21, 2011

### uart

Hi Sara87. Floating point numbers on a computer generally cant be represented exactly, but this is usually hidden from the user by limiting the number of significant digits to which results are displayed. What's unusual in this case is that you are operating precisely on the boundary of the real domain (and the extended complex domain) of the asin() function.

BTW. Are you familiar with complex numbers?

Code (Text):

> format long

> d=0.3
d =  0.300000000000000
> ((d - 0.2)/0.1)*1e16
ans = 9999999999999998

> d = 0.2+0.1
d = 0.300000000000000
> ((d - 0.2)/0.1)*1e16
ans = 10000000000000002

Last edited: Aug 21, 2011
4. Aug 21, 2011

### sara_87

Thank you both.
Yes, i am familiar with the complex numbers.
So, how can i change my code so that is gives pi/2 instead of pi/2+ some complex number ?

5. Aug 21, 2011

### uart

There are several possible ways.

The simplest, if you're certain your result should be real, is: y = real(asin(...))

OR you could test if the arguement is in [-1-delta ... +1+delta], then throw an error if it isn't and clip it back to [-1...1] if it is.

6. Aug 21, 2011

### sara_87

Thank you. I will use y=real(asin(...))