Dear people,

Thanks for your time and patience.

I wrote a program to calculate Cosine, Sin, Tangent.

However, by using the built in math() which are Math.Cos, Math.Sin and Math.Tan

It gives inaccurate readings. By default, the math() accepts and calculates in radian form.

I had properly converted user inputs into degree by

Math.PI * (userInput / 180)

The problem I faced are as follows :

When using Math.Cos(x)
Example : where x can be 90,270,450,630 'should give you a 0.
But, they gave me a funny value instead.

When using Math.Tan(x)
Example: where x can be 90,270,450 'should give you an error
Example: where x can be 180,360,540 'should give you 0
But, they gave me a different value

When using Math.Sin(x)
if example x is 180,360 'should give you 0
However, the function do not calculate it accurately.

I used Windows Calculator as a reference.

Can somehow suggest or help on this? Not by hard-coding but by logic?

## All 6 Replies

Hi

what is going on is that the functions round the numbers to a certain number of digits. pi will be a floating point value, and floating point numbers have a finite number of digits.

For instance:

- Math.Sin(180360 * System.Math.PI / 180) = -0.00000000000057034748018186532

which is a very small number almost zero, but not zero.

hope it helps

Oh yea, I understand what u mean... thanks.. I will try... maybe I shld define PI as 3.1415926?

Hi

what is going on is that the functions round the numbers to a certain number of digits. pi will be a floating point value, and floating point numbers have a finite number of digits.

For instance:

- Math.Sin(180360 * System.Math.PI / 180) = -0.00000000000057034748018186532

which is a very small number almost zero, but not zero.

hope it helps

Ok, Math.PI of VB 2005 is 3.14159265358979

However, I tried to define a variable called dblPI as simply 3.1415926

The answer I got was not much difference when for example I tried Sin360 in degree mode.

When using Math.PI : Sin360 gives -244921270764475E-16
Which is such an "absurb value".

However using dblPI as I defined. Sin360 gives -1.07179586340106E-07

I think its a bug inside VB2005... Any ideas?

Hi

It looks like you only have problems with the values where the answer is zero, one or undeterminded, right?

Why don't you make your own sine, cosine, etc functions and evaluate the vaue if the angle first; there you can determine if the result is zero, or 1, or any other problematic value.

Like for instance:

``````function sine(byval angle as double) as double

if angle = 360 or angle = 180 or angle = 0 then return 0

if angle = 90 then return 1

if angle = 270 then return -1

angle = angle*3.14159265358979/180

dim result as double = math.sin(angle)

return result

end function``````

it is a very basic function and probably needs some more work, but it can give you an idea of what to do if you need more presicion.

Hope it helps.

Hi,

Will you need E-16 precision at your result ? If not use standart routines (and Math.PI)but at the end round the number to E-14 (14 digits after decimal point) precision.

Loren Soth

can any one help me and give me some tips to write trigonometry calculator assembly code? please....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.