-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
AngleValue.cs
991 lines (810 loc) · 33.3 KB
/
AngleValue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace xFunc.Maths.Expressions.Units.AngleUnits;
/// <summary>
/// Represents a number with angle measurement unit.
/// </summary>
public readonly struct AngleValue : IEquatable<AngleValue>, IComparable<AngleValue>, IComparable
{
/// <summary>
/// Initializes a new instance of the <see cref="AngleValue"/> struct.
/// </summary>
/// <param name="angle">The value.</param>
/// <param name="unit">The unit of number.</param>
public AngleValue(NumberValue angle, AngleUnit unit)
{
Angle = angle;
Unit = unit;
}
/// <summary>
/// Creates the <see cref="AngleValue"/> struct with <c>Degree</c> unit.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The angle.</returns>
public static AngleValue Degree(double value)
=> Degree(new NumberValue(value));
/// <summary>
/// Creates the <see cref="AngleValue"/> struct with <c>Degree</c> unit.
/// </summary>
/// <param name="numberValue">The value.</param>
/// <returns>The angle.</returns>
public static AngleValue Degree(NumberValue numberValue)
=> new AngleValue(numberValue, AngleUnit.Degree);
/// <summary>
/// Creates the <see cref="AngleValue"/> struct with <c>Radian</c> unit.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The angle.</returns>
public static AngleValue Radian(double value)
=> Radian(new NumberValue(value));
/// <summary>
/// Creates the <see cref="AngleValue"/> struct with <c>Radian</c> unit.
/// </summary>
/// <param name="numberValue">The value.</param>
/// <returns>The angle.</returns>
public static AngleValue Radian(NumberValue numberValue)
=> new AngleValue(numberValue, AngleUnit.Radian);
/// <summary>
/// Creates the <see cref="AngleValue"/> struct with <c>Gradian</c> unit.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The angle.</returns>
public static AngleValue Gradian(double value)
=> Gradian(new NumberValue(value));
/// <summary>
/// Creates the <see cref="AngleValue"/> struct with <c>Gradian</c> unit.
/// </summary>
/// <param name="numberValue">The value.</param>
/// <returns>The angle.</returns>
public static AngleValue Gradian(NumberValue numberValue)
=> new AngleValue(numberValue, AngleUnit.Gradian);
/// <inheritdoc />
public bool Equals(AngleValue other)
{
var inBase = ToBase();
var otherInBase = other.ToBase();
return inBase.Value.Equals(otherInBase.Value);
}
/// <inheritdoc />
public override bool Equals(object? obj)
=> obj is AngleValue other && Equals(other);
/// <inheritdoc />
public int CompareTo(AngleValue other)
{
var inBase = ToBase();
var otherInBase = other.ToBase();
return inBase.Value.CompareTo(otherInBase.Value);
}
/// <inheritdoc />
public int CompareTo(object? obj)
=> obj switch
{
null => 1,
AngleValue other => CompareTo(other),
_ => throw new ArgumentException($"Object must be of type {nameof(AngleValue)}"),
};
/// <inheritdoc />
[ExcludeFromCodeCoverage]
public override int GetHashCode()
=> HashCode.Combine(Angle, Unit);
/// <inheritdoc />
public override string ToString()
=> $"{Angle} '{Unit}'";
/// <summary>
/// Determines whether two specified instances of <see cref="AngleValue"/> are equal.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns><c>true</c> if <paramref name="left"/> is equal to <paramref name="right"/>; otherwise, <c>false</c>.</returns>
public static bool operator ==(AngleValue left, AngleValue right)
=> left.Equals(right);
/// <summary>
/// Determines whether two specified instances of <see cref="AngleValue"/> are not equal.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns><c>true</c> if <paramref name="left"/> is not equal to <paramref name="right"/>; otherwise, <c>false</c>.</returns>
public static bool operator !=(AngleValue left, AngleValue right)
=> !left.Equals(right);
/// <summary>
/// Indicates whether <paramref name="left"/> parameter is less than the <paramref name="right"/> parameter.
/// </summary>
/// <param name="left">The left angle.</param>
/// <param name="right">The right angle.</param>
/// <returns><c>true</c> if the <paramref name="left"/> parameter is less than the <paramref name="right"/> parameter; otherwise, <c>false</c>.</returns>
public static bool operator <(AngleValue left, AngleValue right)
=> left.CompareTo(right) < 0;
/// <summary>
/// Indicates whether <paramref name="left"/> parameter is greater than the <paramref name="right"/> parameter.
/// </summary>
/// <param name="left">The left angle.</param>
/// <param name="right">The right angle.</param>
/// <returns><c>true</c> if the <paramref name="left"/> parameter is greater than the <paramref name="right"/> parameter; otherwise, <c>false</c>.</returns>
public static bool operator >(AngleValue left, AngleValue right)
=> left.CompareTo(right) > 0;
/// <summary>
/// Indicates whether <paramref name="left"/> parameter is less than or equal to the <paramref name="right"/> parameter.
/// </summary>
/// <param name="left">The left angle.</param>
/// <param name="right">The right angle.</param>
/// <returns><c>true</c> if the <paramref name="left"/> parameter is less than or equal to the <paramref name="right"/> parameter; otherwise, <c>false</c>.</returns>
public static bool operator <=(AngleValue left, AngleValue right)
=> left.CompareTo(right) <= 0;
/// <summary>
/// Indicates whether <paramref name="left"/> parameter is greater than or equal to the <paramref name="right"/> parameter.
/// </summary>
/// <param name="left">The left angle.</param>
/// <param name="right">The right angle.</param>
/// <returns><c>true</c> if the <paramref name="left"/> parameter is greater than or equal to the <paramref name="right"/> parameter; otherwise, <c>false</c>.</returns>
public static bool operator >=(AngleValue left, AngleValue right)
=> left.CompareTo(right) >= 0;
/// <summary>
/// Adds two objects of <see cref="AngleValue"/>.
/// </summary>
/// <param name="left">The first object to add.</param>
/// <param name="right">The second object to add.</param>
/// <returns>An object that is the sum of <paramref name="left"/> and <paramref name="right"/>.</returns>
public static AngleValue operator +(AngleValue left, AngleValue right)
{
right = right.To(left.Unit);
return new AngleValue(left.Angle + right.Angle, left.Unit);
}
/// <summary>
/// Subtracts two objects of <see cref="AngleValue"/>.
/// </summary>
/// <param name="left">The first object to sub.</param>
/// <param name="right">The second object to sub.</param>
/// <returns>An object that is the difference of <paramref name="left"/> and <paramref name="right"/>.</returns>
public static AngleValue operator -(AngleValue left, AngleValue right)
{
right = right.To(left.Unit);
return new AngleValue(left.Angle - right.Angle, left.Unit);
}
/// <summary>
/// Produces the negative of <see cref="AngleValue"/>.
/// </summary>
/// <param name="angleValue">The angle.</param>
/// <returns>The negative of <paramref name="angleValue"/>.</returns>
public static AngleValue operator -(AngleValue angleValue)
=> new AngleValue(-angleValue.Angle, angleValue.Unit);
private AreaValue ToBase()
=> new AreaValue(Angle * Unit.Factor, AreaUnit.Meter);
/// <summary>
/// Converts the current object to the specified <paramref name="newUnit"/>.
/// </summary>
/// <param name="newUnit">The unit to convert to.</param>
/// <returns>The angle which is converted to the specified <paramref name="newUnit"/>.</returns>
public AngleValue To(AngleUnit newUnit)
{
var inBase = Angle * Unit.Factor;
var converted = inBase / newUnit.Factor;
return new AngleValue(converted, newUnit);
}
/// <summary>
/// Converts the current object to degrees.
/// </summary>
/// <returns>The angle which is converted to degrees.</returns>
public AngleValue ToDegree()
=> To(AngleUnit.Degree);
/// <summary>
/// Converts the current object to radians.
/// </summary>
/// <returns>The angle which is converted to radians.</returns>
public AngleValue ToRadian()
=> To(AngleUnit.Radian);
/// <summary>
/// Converts the current object to gradians.
/// </summary>
/// <returns>The angle which is converted to gradians.</returns>
public AngleValue ToGradian()
=> To(AngleUnit.Gradian);
/// <summary>
/// Normalizes the current angle between [0, 2pi).
/// </summary>
/// <returns>The normalized angle.</returns>
public AngleValue Normalize()
{
const double degreeFullCircle = 360.0;
const double radianFullCircle = 2 * Math.PI;
const double gradianFullCircle = 400.0;
static double NormalizeInternal(double value, double circle)
{
value %= circle;
if (value < 0)
value += circle;
return value;
}
if (Unit == AngleUnit.Degree)
return Degree(NormalizeInternal(Angle.Number, degreeFullCircle));
if (Unit == AngleUnit.Radian)
return Radian(NormalizeInternal(Angle.Number, radianFullCircle));
// if (Unit == AngleUnit.Gradian)
Debug.Assert(Unit == AngleUnit.Gradian, "Should be Gradian");
return Gradian(NormalizeInternal(Angle.Number, gradianFullCircle));
}
/// <summary>
/// Returns the absolute value of a specified angle.
/// </summary>
/// <param name="angleValue">The angle.</param>
/// <returns>The angle, <c>x</c>, that such that 0 ≤ <c>x</c> ≤ <c>MaxValue</c>.</returns>
public static AngleValue Abs(AngleValue angleValue)
=> new AngleValue(NumberValue.Abs(angleValue.Angle), angleValue.Unit);
/// <summary>
/// Returns the smallest integral value that is greater than or equal to the specified angle number.
/// </summary>
/// <param name="angleValue">The angle.</param>
/// <returns>The smallest integral value.</returns>
public static AngleValue Ceiling(AngleValue angleValue)
=> new AngleValue(NumberValue.Ceiling(angleValue.Angle), angleValue.Unit);
/// <summary>
/// Returns the largest integral value less than or equal to the specified angle number.
/// </summary>
/// <param name="angleValue">The angle.</param>
/// <returns>The largest integral value.</returns>
public static AngleValue Floor(AngleValue angleValue)
=> new AngleValue(NumberValue.Floor(angleValue.Angle), angleValue.Unit);
/// <summary>
/// Calculates the integral part of a specified angle number.
/// </summary>
/// <param name="angleValue">An angle to truncate.</param>
/// <returns>The integral part of angle number.</returns>
public static AngleValue Truncate(AngleValue angleValue)
=> new AngleValue(NumberValue.Truncate(angleValue.Angle), angleValue.Unit);
/// <summary>
/// Returns the fractional part of the angle number.
/// </summary>
/// <param name="angleValue">The angle number.</param>
/// <returns>The fractional part.</returns>
public static AngleValue Frac(AngleValue angleValue)
=> new AngleValue(NumberValue.Frac(angleValue.Angle), angleValue.Unit);
/// <summary>
/// Rounds a double-precision floating-point value to a specified number of fractional digits,
/// and uses the specified rounding convention for midpoint values.
/// </summary>
/// <param name="angleValue">The angle number.</param>
/// <param name="digits">The number of fractional digits in the return value.</param>
/// <returns>The number nearest to <paramref name="angleValue"/> that has a number of fractional digits equal to <paramref name="digits"/>. If value has fewer fractional digits than <paramref name="digits"/>, <paramref name="angleValue"/> is returned unchanged.</returns>
public static AngleValue Round(AngleValue angleValue, NumberValue digits)
=> new AngleValue(NumberValue.Round(angleValue.Angle, digits), angleValue.Unit);
/// <summary>
/// The 'sin' function.
/// </summary>
/// <param name="angleValue">The angle number.</param>
/// <returns>The result of sine function.</returns>
public static NumberValue Sin(AngleValue angleValue)
{
var angle = angleValue.Normalize().Angle;
// 0
if (angle == 0)
return NumberValue.Zero;
// 30
if (angle == Math.PI / 6)
return NumberValue.Half;
// 45
if (angle == Math.PI / 4)
return NumberValue.Sqrt2By2;
// 60
if (angle == Math.PI / 3)
return NumberValue.Sqrt3By2;
// 90
if (angle == Math.PI / 2)
return NumberValue.One;
// 120
if (angle == 2 * Math.PI / 3)
return NumberValue.Sqrt3By2;
// 135
if (angle == 3 * Math.PI / 4)
return NumberValue.Sqrt2By2;
// 150
if (angle == 5 * Math.PI / 6)
return NumberValue.Half;
// 180
if (angle == Math.PI)
return NumberValue.Zero;
// 210
if (angle == 7 * Math.PI / 6)
return -NumberValue.Half;
// 225
if (angle == 5 * Math.PI / 4)
return -NumberValue.Sqrt2By2;
// 240
if (angle == 4 * Math.PI / 3)
return -NumberValue.Sqrt3By2;
// 270
if (angle == 3 * Math.PI / 2)
return -NumberValue.One;
// 300
if (angle == 5 * Math.PI / 3)
return -NumberValue.Sqrt3By2;
// 315
if (angle == 7 * Math.PI / 4)
return -NumberValue.Sqrt2By2;
// 330
if (angle == 11 * Math.PI / 6)
return -NumberValue.Half;
return new NumberValue(Math.Sin(angleValue.Angle.Number));
}
/// <summary>
/// The 'cos' function.
/// </summary>
/// <param name="angleValue">The angle number.</param>
/// <returns>The result of cosine function.</returns>
public static NumberValue Cos(AngleValue angleValue)
{
var angle = angleValue.Normalize().Angle;
// 0
if (angle == 0)
return NumberValue.One;
// 30
if (angle == Math.PI / 6)
return NumberValue.Sqrt3By2;
// 45
if (angle == Math.PI / 4)
return NumberValue.Sqrt2By2;
// 60
if (angle == Math.PI / 3)
return NumberValue.Half;
// 90
if (angle == Math.PI / 2)
return NumberValue.Zero;
// 120
if (angle == 2 * Math.PI / 3)
return -NumberValue.Half;
// 135
if (angle == 3 * Math.PI / 4)
return -NumberValue.Sqrt2By2;
// 150
if (angle == 5 * Math.PI / 6)
return -NumberValue.Sqrt3By2;
// 180
if (angle == Math.PI)
return -NumberValue.One;
// 210
if (angle == 7 * Math.PI / 6)
return -NumberValue.Sqrt3By2;
// 225
if (angle == 5 * Math.PI / 4)
return -NumberValue.Sqrt2By2;
// 240
if (angle == 4 * Math.PI / 3)
return -NumberValue.Half;
// 270
if (angle == 3 * Math.PI / 2)
return NumberValue.Zero;
// 300
if (angle == 5 * Math.PI / 3)
return NumberValue.Half;
// 315
if (angle == 7 * Math.PI / 4)
return NumberValue.Sqrt2By2;
// 330
if (angle == 11 * Math.PI / 6)
return NumberValue.Sqrt3By2;
return new NumberValue(Math.Cos(angleValue.Angle.Number));
}
/// <summary>
/// The 'tan' function.
/// </summary>
/// <param name="angleValue">The angle number.</param>
/// <returns>The result of tangent function.</returns>
public static NumberValue Tan(AngleValue angleValue)
{
var angle = angleValue.Normalize().Angle;
// 0
if (angle == 0)
return NumberValue.Zero;
// 30
if (angle == Math.PI / 6)
return NumberValue.Sqrt3By3;
// 45
if (angle == Math.PI / 4)
return NumberValue.One;
// 60
if (angle == Math.PI / 3)
return NumberValue.Sqrt3;
// 90
if (angle == Math.PI / 2)
return NumberValue.PositiveInfinity;
// 120
if (angle == 2 * Math.PI / 3)
return -NumberValue.Sqrt3;
// 135
if (angle == 3 * Math.PI / 4)
return -NumberValue.One;
// 150
if (angle == 5 * Math.PI / 6)
return -NumberValue.Sqrt3By3;
// 180
if (angle == Math.PI)
return NumberValue.Zero;
// 210
if (angle == 7 * Math.PI / 6)
return NumberValue.Sqrt3By3;
// 225
if (angle == 5 * Math.PI / 4)
return NumberValue.One;
// 240
if (angle == 4 * Math.PI / 3)
return NumberValue.Sqrt3;
// 270
if (angle == 3 * Math.PI / 2)
return NumberValue.PositiveInfinity;
// 300
if (angle == 5 * Math.PI / 3)
return -NumberValue.Sqrt3;
// 315
if (angle == 7 * Math.PI / 4)
return -NumberValue.One;
// 330
if (angle == 11 * Math.PI / 6)
return -NumberValue.Sqrt3By3;
return new NumberValue(Math.Tan(angleValue.Angle.Number));
}
/// <summary>
/// The 'cot' function.
/// </summary>
/// <param name="angleValue">The angle number.</param>
/// <returns>The result of cotangent function.</returns>
public static NumberValue Cot(AngleValue angleValue)
{
var angle = angleValue.Normalize().Angle;
// 0
if (angle == 0)
return NumberValue.PositiveInfinity;
// 30
if (angle == Math.PI / 6)
return NumberValue.Sqrt3;
// 45
if (angle == Math.PI / 4)
return NumberValue.One;
// 60
if (angle == Math.PI / 3)
return NumberValue.Sqrt3By3;
// 90
if (angle == Math.PI / 2)
return NumberValue.Zero;
// 120
if (angle == 2 * Math.PI / 3)
return -NumberValue.Sqrt3By3;
// 135
if (angle == 3 * Math.PI / 4)
return -NumberValue.One;
// 150
if (angle == 5 * Math.PI / 6)
return -NumberValue.Sqrt3;
// 180
if (angle == Math.PI)
return NumberValue.PositiveInfinity;
// 210
if (angle == 7 * Math.PI / 6)
return NumberValue.Sqrt3;
// 225
if (angle == 5 * Math.PI / 4)
return NumberValue.One;
// 240
if (angle == 4 * Math.PI / 3)
return NumberValue.Sqrt3By3;
// 270
if (angle == 3 * Math.PI / 2)
return NumberValue.Zero;
// 300
if (angle == 5 * Math.PI / 3)
return -NumberValue.Sqrt3;
// 315
if (angle == 7 * Math.PI / 4)
return -NumberValue.One;
// 330
if (angle == 11 * Math.PI / 6)
return -NumberValue.Sqrt3By3;
return new NumberValue(Math.Cos(angleValue.Angle.Number) / Math.Sin(angleValue.Angle.Number));
}
/// <summary>
/// The 'cot' function.
/// </summary>
/// <param name="angleValue">The angle number.</param>
/// <returns>The result of secant function.</returns>
public static NumberValue Sec(AngleValue angleValue)
{
var angle = angleValue.Normalize().Angle;
// 0
if (angle == 0)
return NumberValue.One;
// 30
if (angle == Math.PI / 6)
return NumberValue.Sqrt3By3By2;
// 45
if (angle == Math.PI / 4)
return NumberValue.Sqrt2;
// 60
if (angle == Math.PI / 3)
return NumberValue.Two;
// 90
if (angle == Math.PI / 2)
return NumberValue.PositiveInfinity;
// 120
if (angle == 2 * Math.PI / 3)
return -NumberValue.Two;
// 135
if (angle == 3 * Math.PI / 4)
return -NumberValue.Sqrt2;
// 150
if (angle == 5 * Math.PI / 6)
return -NumberValue.Sqrt3By3By2;
// 180
if (angle == Math.PI)
return -NumberValue.One;
// 210
if (angle == 7 * Math.PI / 6)
return -NumberValue.Sqrt3By3By2;
// 225
if (angle == 5 * Math.PI / 4)
return -NumberValue.Sqrt2;
// 240
if (angle == 4 * Math.PI / 3)
return -NumberValue.Two;
// 270
if (angle == 3 * Math.PI / 2)
return NumberValue.PositiveInfinity;
// 300
if (angle == 5 * Math.PI / 3)
return -NumberValue.Two;
// 315
if (angle == 7 * Math.PI / 4)
return NumberValue.Sqrt2;
// 330
if (angle == 11 * Math.PI / 6)
return NumberValue.Sqrt3By3By2;
return new NumberValue(1 / Math.Cos(angleValue.Angle.Number));
}
/// <summary>
/// The 'csc' function.
/// </summary>
/// <param name="angleValue">The angle number.</param>
/// <returns>The result of cosecant function.</returns>
public static NumberValue Csc(AngleValue angleValue)
{
var angle = angleValue.Normalize().Angle;
// 0
if (angle == 0)
return NumberValue.PositiveInfinity;
// 30
if (angle == Math.PI / 6)
return NumberValue.Two;
// 45
if (angle == Math.PI / 4)
return NumberValue.Sqrt2;
// 60
if (angle == Math.PI / 3)
return NumberValue.Sqrt3By3By2;
// 90
if (angle == Math.PI / 2)
return NumberValue.One;
// 120
if (angle == 2 * Math.PI / 3)
return NumberValue.Sqrt3By3By2;
// 135
if (angle == 3 * Math.PI / 4)
return NumberValue.Sqrt2;
// 150
if (angle == 5 * Math.PI / 6)
return NumberValue.Two;
// 180
if (angle == Math.PI)
return NumberValue.PositiveInfinity;
// 210
if (angle == 7 * Math.PI / 6)
return -NumberValue.Two;
// 225
if (angle == 5 * Math.PI / 4)
return -NumberValue.Sqrt2;
// 240
if (angle == 4 * Math.PI / 3)
return -NumberValue.Sqrt3By3By2;
// 270
if (angle == 3 * Math.PI / 2)
return -NumberValue.One;
// 300
if (angle == 5 * Math.PI / 3)
return NumberValue.Sqrt3By3By2;
// 315
if (angle == 7 * Math.PI / 4)
return NumberValue.Sqrt2;
// 330
if (angle == 11 * Math.PI / 6)
return NumberValue.Two;
return new NumberValue(1 / Math.Sin(angleValue.Angle.Number));
}
/// <summary>
/// Returns the angle whose sine is the specified number.
/// </summary>
/// <param name="number">A number representing a sine, where d must be greater than or equal to -1, but less than or equal to 1.</param>
/// <returns>
/// An angle, θ, measured in radians, such that -π/2 ≤ θ ≤ π/2.
/// -or-
/// <see cref="double.NaN"/> if <paramref name="number"/> < -1 or <paramref name="number"/> > 1 or <paramref name="number"/> equals <see cref="double.NaN"/>.
/// </returns>
public static AngleValue Asin(NumberValue number)
=> Radian(Math.Asin(number.Number));
/// <summary>
/// Returns the angle whose cosine is the specified number.
/// </summary>
/// <param name="number">A number representing a cosine, where d must be greater than or equal to -1, but less than or equal to 1.</param>
/// <returns>
/// An angle, θ, measured in radians, such that 0 ≤ θ ≤ π.
/// -or-
/// <see cref="double.NaN"/> if <paramref name="number"/> < -1 or <paramref name="number"/> > 1 or <paramref name="number"/> equals <see cref="double.NaN"/>.
/// </returns>
public static AngleValue Acos(NumberValue number)
=> Radian(Math.Acos(number.Number));
/// <summary>
/// Returns the angle whose tangent is the specified number.
/// </summary>
/// <param name="number">A number representing a tangent.</param>
/// <returns>
/// An angle, θ, measured in radians, such that -π/2 ≤ θ ≤ π/2.
/// -or-
/// <see cref="double.NaN"/> if <paramref name="number"/> equals <see cref="double.NaN"/>,
/// -π/2 rounded to double precision (-1.5707963267949) if <paramref name="number"/> equals <see cref="double.NegativeInfinity"/>,
/// or π/2 rounded to double precision (1.5707963267949) if <paramref name="number"/> equals <see cref="double.PositiveInfinity"/>.
/// </returns>
public static AngleValue Atan(NumberValue number)
=> Radian(Math.Atan(number.Number));
/// <summary>
/// Returns the angle whose cotangent is the specified number.
/// </summary>
/// <param name="number">A number representing a cotangent.</param>
/// <returns>An angle, measured in radians.</returns>
public static AngleValue Acot(NumberValue number)
=> Radian(Math.PI / 2 - Math.Atan(number.Number));
/// <summary>
/// Returns the angle whose secant is the specified number.
/// </summary>
/// <param name="number">A number representing a secant.</param>
/// <returns>An angle, measured in radians.</returns>
public static AngleValue Asec(NumberValue number)
=> Radian(Math.Acos(1 / number.Number));
/// <summary>
/// Returns the angle whose cosecant is the specified number.
/// </summary>
/// <param name="number">A number representing a hyperbolic cosecant.</param>
/// <returns>An angle, measured in radians.</returns>
public static AngleValue Acsc(NumberValue number)
=> Radian(Math.Asin(1 / number.Number));
/// <summary>
/// Returns the hyperbolic sine of the specified angle.
/// </summary>
/// <param name="angle">An angle.</param>
/// <returns>The hyperbolic sine of <paramref name="angle"/>. If <paramref name="angle"/> is equal to <see cref="double.NegativeInfinity"/>, <see cref="double.PositiveInfinity"/>, or <see cref="double.NaN"/>.</returns>
public static NumberValue Sinh(AngleValue angle)
=> new NumberValue(Math.Sinh(angle.ToRadian().Angle.Number));
/// <summary>
/// Returns the hyperbolic cosine of the specified angle.
/// </summary>
/// <param name="angle">An angle.</param>
/// <returns>
/// The hyperbolic cosine of <paramref name="angle"/>.
/// If <paramref name="angle"/> is equal to <see cref="double.NegativeInfinity"/> or <see cref="double.PositiveInfinity"/>, <see cref="double.PositiveInfinity"/> is returned.
/// If <paramref name="angle"/> is equal to <see cref="double.NaN"/>, <see cref="double.NaN"/> is returned.
/// </returns>
public static NumberValue Cosh(AngleValue angle)
=> new NumberValue(Math.Cosh(angle.ToRadian().Angle.Number));
/// <summary>
/// Returns the hyperbolic tangent of the specified angle.
/// </summary>
/// <param name="angle">An angle.</param>
/// <returns>
/// The hyperbolic tangent of <paramref name="angle"/>.
/// If <paramref name="angle"/> is equal to <see cref="double.NegativeInfinity"/>, this method returns -1.
/// If <paramref name="angle"/> is equal to <see cref="double.PositiveInfinity"/>, this method returns 1.
/// If <paramref name="angle"/> is equal to <see cref="double.NaN"/>, this method returns <see cref="double.NaN"/>.
/// </returns>
public static NumberValue Tanh(AngleValue angle)
=> new NumberValue(Math.Tanh(angle.ToRadian().Angle.Number));
/// <summary>
/// Returns the hyperbolic cotangent of the specified angle.
/// </summary>
/// <param name="angle">An angle, measured in radians.</param>
/// <returns>The hyperbolic cotangent of value.</returns>
public static NumberValue Coth(AngleValue angle)
{
var d = angle.ToRadian().Angle.Number;
return new NumberValue((Math.Exp(d) + Math.Exp(-d)) / (Math.Exp(d) - Math.Exp(-d)));
}
/// <summary>
/// Returns the hyperbolic secant of the specified angle.
/// </summary>
/// <param name="angle">An angle.</param>
/// <returns>The hyperbolic secant of value.</returns>
public static NumberValue Sech(AngleValue angle)
{
var d = angle.ToRadian().Angle.Number;
return new NumberValue(2 / (Math.Exp(d) + Math.Exp(-d)));
}
/// <summary>
/// Returns the hyperbolic cosecant of the specified angle.
/// </summary>
/// <param name="angle">An angle.</param>
/// <returns>The hyperbolic cosecant of value.</returns>
public static NumberValue Csch(AngleValue angle)
{
var d = angle.ToRadian().Angle.Number;
return new NumberValue(2 / (Math.Exp(d) - Math.Exp(-d)));
}
/// <summary>
/// Returns the angle whose hyperbolic sine is the specified number.
/// </summary>
/// <param name="number">A number representing a hyperbolic sine, where <paramref name="number"/> must be greater than or equal to <see cref="double.NegativeInfinity"/>, but less than or equal to <see cref="double.PositiveInfinity"/>.</param>
/// <returns>
/// An angle, θ, measured in radians, such that -∞ < θ ≤ -1, or 1 ≤ θ < ∞.
/// -or-
/// <see cref="double.NaN"/> if <paramref name="number"/> equals <see cref="double.NaN"/>.
/// </returns>
public static AngleValue Asinh(NumberValue number)
=> Radian(Math.Asinh(number.Number));
/// <summary>
/// Returns the angle whose hyperbolic cosine is the specified number.
/// </summary>
/// <param name="number">A number representing a hyperbolic cosine, where <paramref name="number"/> must be greater than or equal to 1, but less than or equal to <see cref="double.PositiveInfinity"/>.</param>
/// <returns>
/// An angle, θ, measured in radians, such that 0 ≤ θ ≤ ∞.
/// -or-
/// <see cref="double.NaN"/> if <paramref name="number"/> < 1 or <paramref name="number"/> equals <see cref="double.NaN"/>.
/// </returns>
public static AngleValue Acosh(NumberValue number)
=> Radian(Math.Acosh(number.Number));
/// <summary>
/// Returns the angle whose hyperbolic tangent is the specified number.
/// </summary>
/// <param name="number">A number representing a hyperbolic tangent, where <paramref name="number"/> must be greater than or equal to -1, but less than or equal to 1.</param>
/// <returns>
/// An angle, θ, measured in radians, such that -∞ < θ < -1, or 1 < θ < ∞.
/// -or-
/// <see cref="double.NaN"/> if <paramref name="number"/> < -1 or <paramref name="number"/> > 1 or <paramref name="number"/> equals <see cref="double.NaN"/>.
/// </returns>
public static AngleValue Atanh(NumberValue number)
=> Radian(Math.Atanh(number.Number));
/// <summary>
/// Returns the angle whose hyperbolic cotangent is the specified number.
/// </summary>
/// <param name="number">A number representing a hyperbolic cotangent.</param>
/// <returns>An angle, measured in radians.</returns>
public static AngleValue Acoth(NumberValue number)
=> Radian(Math.Log((number.Number + 1) / (number.Number - 1)) / 2);
/// <summary>
/// Returns the angle whose hyperbolic secant is the specified number.
/// </summary>
/// <param name="number">A number representing a hyperbolic secant.</param>
/// <returns>An angle, measured in radians.</returns>
public static AngleValue Asech(NumberValue number)
{
var z = 1 / number.Number;
return Radian(Math.Log(z + Math.Sqrt(z + 1) * Math.Sqrt(z - 1)));
}
/// <summary>
/// Returns the angle whose hyperbolic cosecant is the specified number.
/// </summary>
/// <param name="number">A number representing a hyperbolic cosecant.</param>
/// <returns>An angle, measured in radians.</returns>
public static AngleValue Acsch(NumberValue number)
=> Radian(Math.Log(1 / number.Number + Math.Sqrt(1 / number.Number * number.Number + 1)));
/// <summary>
/// Converts <see cref="AngleValue"/> to <see cref="Angle"/>.
/// </summary>
/// <returns>The angle number.</returns>
public Angle AsExpression()
=> new Angle(this);
/// <summary>
/// Gets a value.
/// </summary>
public NumberValue Angle { get; }
/// <summary>
/// Gets a unit.
/// </summary>
public AngleUnit Unit { get; }
/// <summary>
/// Gets an integer that indicates the sign of a double-precision floating-point number.
/// </summary>
public double Sign => Angle.Sign;
}