Sunday, August 26, 2012

Cosines and Sines

When I was at school, I learnt that the square of the cosine of an angle plus the square of the sine of that angle equals 1. I decided to try this out in Oracle 11.1.0.6.0 running on Windows XP. The results were quite close. The cosine and sine functions both work on angles in radians. To convert an angle in degrees into radians,  you must multiply it by pi/180:

SQL> set serveroutput on
SQL> declare
  2   cos_squared_i number;
  3   sin_squared_i number;
  4   csi_plus_ssi number;
  5  begin
  6   for i in 0..90 loop
  7    cos_squared_i := power(cos(i*3.14159/180),2);
  8    sin_squared_i := power(sin(i*3.14159/180),2);
  9    csi_plus_ssi := cos_squared_i + sin_squared_i;
 10    dbms_output.put_line('Angle = '||i);
 11    dbms_output.put_line('Result = '||csi_plus_ssi);
 12   end loop;
 13  end;
 14  /
Angle = 0
Result = 1
Angle = 1
Result = .9999999999999999999999999999999999999899
Angle = 2
Result = .9999999999999999999999999999999999999883
Angle = 3
Result = .9999999999999999999999999999999999999958
Angle = 4
Result = 1.00000000000000000000000000000000000002
Angle = 5
Result = 1.00000000000000000000000000000000000001
Angle = 6
Result = .9999999999999999999999999999999999999871
Angle = 7
Result = .9999999999999999999999999999999999999983
Angle = 8
Result = 1
Angle = 9
Result = .9999999999999999999999999999999999999975
Angle = 10
Result = 1
Angle = 11
Result = 1.00000000000000000000000000000000000001
Angle = 12
Result = .9999999999999999999999999999999999999964
Angle = 13
Result = .9999999999999999999999999999999999999963
Angle = 14
Result = 1.00000000000000000000000000000000000001
Angle = 15
Result = 1
Angle = 16
Result = 1.00000000000000000000000000000000000001
Angle = 17
Result = .9999999999999999999999999999999999999943
Angle = 18
Result = 1.00000000000000000000000000000000000001
Angle = 19
Result = .9999999999999999999999999999999999999953
Angle = 20
Result = 1.00000000000000000000000000000000000001
Angle = 21
Result = .9999999999999999999999999999999999999816
Angle = 22
Result = .9999999999999999999999999999999999999673
Angle = 23
Result = .9999999999999999999999999999999999999744
Angle = 24
Result = .9999999999999999999999999999999999999816
Angle = 25
Result = 1.00000000000000000000000000000000000002
Angle = 26
Result = 1
Angle = 27
Result = 1.00000000000000000000000000000000000001
Angle = 28
Result = 1.00000000000000000000000000000000000001
Angle = 29
Result = 1
Angle = 30
Result = 1
Angle = 31
Result = .9999999999999999999999999999999999999967
Angle = 32
Result = 1.00000000000000000000000000000000000002
Angle = 33
Result = .9999999999999999999999999999999999999992
Angle = 34
Result = .9999999999999999999999999999999999999992
Angle = 35
Result = 1.00000000000000000000000000000000000001
Angle = 36
Result = .9999999999999999999999999999999999999982
Angle = 37
Result = .9999999999999999999999999999999999999923
Angle = 38
Result = .9999999999999999999999999999999999999978
Angle = 39
Result = .9999999999999999999999999999999999999915
Angle = 40
Result = .9999999999999999999999999999999999999894
Angle = 41
Result = .9999999999999999999999999999999999999978
Angle = 42
Result = 1.00000000000000000000000000000000000002
Angle = 43
Result = 1.00000000000000000000000000000000000001
Angle = 44
Result = 1
Angle = 45
Result = 1
Angle = 46
Result = .9999999999999999999999999999999999999995
Angle = 47
Result = 1.00000000000000000000000000000000000003
Angle = 48
Result = 1
Angle = 49
Result = .9999999999999999999999999999999999999896
Angle = 50
Result = .9999999999999999999999999999999999999852
Angle = 51
Result = 1
Angle = 52
Result = .9999999999999999999999999999999999999984
Angle = 53
Result = 1.00000000000000000000000000000000000001
Angle = 54
Result = .9999999999999999999999999999999999999962
Angle = 55
Result = 1.00000000000000000000000000000000000001
Angle = 56
Result = 1.00000000000000000000000000000000000001
Angle = 57
Result = .9999999999999999999999999999999999999891
Angle = 58
Result = 1.00000000000000000000000000000000000001
Angle = 59
Result = .9999999999999999999999999999999999999965
Angle = 60
Result = 1.00000000000000000000000000000000000002
Angle = 61
Result = 1.00000000000000000000000000000000000001
Angle = 62
Result = 1.00000000000000000000000000000000000001
Angle = 63
Result = 1.00000000000000000000000000000000000001
Angle = 64
Result = .9999999999999999999999999999999999999912
Angle = 65
Result = .9999999999999999999999999999999999999862
Angle = 66
Result = .9999999999999999999999999999999999999832
Angle = 67
Result = 1.00000000000000000000000000000000000001
Angle = 68
Result = 1.00000000000000000000000000000000000004
Angle = 69
Result = 1
Angle = 70
Result = .9999999999999999999999999999999999999986
Angle = 71
Result = 1.00000000000000000000000000000000000002
Angle = 72
Result = 1.00000000000000000000000000000000000002
Angle = 73
Result = 1
Angle = 74
Result = 1.00000000000000000000000000000000000001
Angle = 75
Result = .9999999999999999999999999999999999999819
Angle = 76
Result = .9999999999999999999999999999999999999742
Angle = 77
Result = .9999999999999999999999999999999999999867
Angle = 78
Result = .999999999999999999999999999999999999991
Angle = 79
Result = .9999999999999999999999999999999999999907
Angle = 80
Result = 1
Angle = 81
Result = 1
Angle = 82
Result = 1
Angle = 83
Result = .9999999999999999999999999999999999999998
Angle = 84
Result = .9999999999999999999999999999999999999874
Angle = 85
Result = .9999999999999999999999999999999999999965
Angle = 86
Result = .9999999999999999999999999999999999999691
Angle = 87
Result = .9999999999999999999999999999999999999966
Angle = 88
Result = 1.00000000000000000000000000000000000001
Angle = 89
Result = 1.00000000000000000000000000000000000003
Angle = 90
Result = .9999999999999999999999999999999999999968

PL/SQL procedure successfully completed.

SQL>

No comments:

Post a Comment