03-27-2003, 07:45 PM
Wel, one more time, some will not learn anything and some will, but this can be helpful :
The SIN and COs standard functions of QB are "exact" only in a given range. For example, if you type
Pi = ATN(1)*4 (or Pi = 3.141593, that's equal)
then
Print COS(Pi/2)
You will get -4.371139 E-8 instead of zero. This is sufficient most of the time, but unsufficient if you, for example, build a Hi-Res raytracer capable of plotting quadric shapes such as a torus or any twisted 4th degree shape on a 1024x768 screen. Those who guessed I'm buiding such a raytracer are right :rotfl: )
That simply comes from the fact that SIN and COS are correct only for a given range of angles (0 to Pi/4 for sinus).
If you need "exact" values, you may follow this code, and adapt it to your habits:
CONST Pi = 3.141592653589793# ' In the main module
FUNCTION CosD (A)
A = A - INT(A / 360 + .5) * 360
IF A >= 0 AND A <= 90 THEN
x# = A / 180 * Pi
IF COS(x#) < .5 THEN
CosD = SIN(Pi / 2 - x#)
ELSE
CosD = COS(x#)
END IF
EXIT FUNCTION
END IF
IF A < 0 AND A >= -90 THEN
x# = -A / 180 * Pi
IF COS(x#) < .5 THEN
CosD = SIN(Pi / 2 - x#)
ELSE
CosD = COS(x#)
END IF
EXIT FUNCTION
END IF
IF A > 90 AND A <= 180 THEN
x# = (180 - A) / 180 * Pi
IF COS(x#) < .5 THEN
CosD = -SIN(Pi / 2 - x#)
ELSE
CosD = -COS(x#)
END IF
EXIT FUNCTION
END IF
IF A < -90 AND A >= -180 THEN
x# = (A - 180) / 180 * Pi
IF COS(x#) < .5 THEN
CosD = -SIN(Pi / 2 - x#)
ELSE
CosD = -COS(x#)
END IF
EXIT FUNCTION
END IF
END FUNCTION
Now for an "exact" Sinus :
FUNCTION SinD (A)
SinD = CosD(90 - A)
END FUNCTION
Much easier after the first one...
The SIN and COs standard functions of QB are "exact" only in a given range. For example, if you type
Pi = ATN(1)*4 (or Pi = 3.141593, that's equal)
then
Print COS(Pi/2)
You will get -4.371139 E-8 instead of zero. This is sufficient most of the time, but unsufficient if you, for example, build a Hi-Res raytracer capable of plotting quadric shapes such as a torus or any twisted 4th degree shape on a 1024x768 screen. Those who guessed I'm buiding such a raytracer are right :rotfl: )
That simply comes from the fact that SIN and COS are correct only for a given range of angles (0 to Pi/4 for sinus).
If you need "exact" values, you may follow this code, and adapt it to your habits:
CONST Pi = 3.141592653589793# ' In the main module
FUNCTION CosD (A)
A = A - INT(A / 360 + .5) * 360
IF A >= 0 AND A <= 90 THEN
x# = A / 180 * Pi
IF COS(x#) < .5 THEN
CosD = SIN(Pi / 2 - x#)
ELSE
CosD = COS(x#)
END IF
EXIT FUNCTION
END IF
IF A < 0 AND A >= -90 THEN
x# = -A / 180 * Pi
IF COS(x#) < .5 THEN
CosD = SIN(Pi / 2 - x#)
ELSE
CosD = COS(x#)
END IF
EXIT FUNCTION
END IF
IF A > 90 AND A <= 180 THEN
x# = (180 - A) / 180 * Pi
IF COS(x#) < .5 THEN
CosD = -SIN(Pi / 2 - x#)
ELSE
CosD = -COS(x#)
END IF
EXIT FUNCTION
END IF
IF A < -90 AND A >= -180 THEN
x# = (A - 180) / 180 * Pi
IF COS(x#) < .5 THEN
CosD = -SIN(Pi / 2 - x#)
ELSE
CosD = -COS(x#)
END IF
EXIT FUNCTION
END IF
END FUNCTION
Now for an "exact" Sinus :
FUNCTION SinD (A)
SinD = CosD(90 - A)
END FUNCTION
Much easier after the first one...