-
Notifications
You must be signed in to change notification settings - Fork 5
/
CommonFormulas.swift
809 lines (739 loc) · 35.7 KB
/
CommonFormulas.swift
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
//
// File.swift
//
//
// Created by Arthur Guiot on 2020-01-23.
//
import Foundation
public extension Tables {
// MARK: Common Formulas + Trigonometry
/// Absolute value of a number
/// - Parameter number: A `BigDouble`
func ABS(_ number: BigNumber) -> BigNumber {
return abs(number)
}
/// Arc-Cosinus of a number.
/// - Parameter number: Any `BigDouble` less than 2pi
func ACOS(_ number: BigNumber) throws -> BigNumber {
guard let double = number.asDouble() else { throw TablesError.Overflow }
if BN.radians == true {
return BigDouble(acos(double))
}
return acos(double) * 180 / pi
}
/// Hyperbolic Arc-Cosinus of a number.
/// - Parameter number: Any `BigDouble`
func ACOSH(_ number: BigNumber) throws -> BigNumber {
guard let double = number.asDouble() else { throw TablesError.Overflow }
return BigDouble(acosh(double))
}
/// Arc-Cotangent of a number.
/// - Parameter number: Any `BigDouble` less than 2pi
func ACOT(_ number: BigNumber) throws -> BigNumber {
guard let double = number.asDouble() else { throw TablesError.Overflow }
if BN.radians == true {
return BigDouble(atan(1 / double))
}
return atan(1 / double) * 180 / pi
}
/// Hyperbolic Arc-Cotangent of a number.
/// - Parameter number: Any `BigDouble`
func ACOTH(_ number: BigNumber) throws -> BigNumber {
guard let double = number.asDouble() else { throw TablesError.Overflow }
guard double != 1 else { throw TablesError.DivisionByZero }
return BigDouble(0.5 * log((double + 1) / (double - 1)))
}
// MARK: TODO: AGGREGATE + ARABIC
/// Arc-Sinus of a number.
/// - Parameter number: Any `BigDouble` less than 2pi
func ASIN(_ number: BigNumber) throws -> BigNumber {
guard let double = number.asDouble() else { throw TablesError.Overflow }
if BN.radians == true {
return BigDouble(asin(double))
}
return asin(double) * 180 / pi
}
/// Hyperbolic Arc-Sinus of a number.
/// - Parameter number: Any `BigDouble`
func ASINH(_ number: BigNumber) throws -> BigNumber {
guard let double = number.asDouble() else { throw TablesError.Overflow }
return BigDouble(asinh(double))
}
/// Arc-Tangent of a number.
/// - Parameter number: Any `BigDouble`
func ATAN(_ number: BigNumber) throws -> BigNumber {
guard let double = number.asDouble() else { throw TablesError.Overflow }
if BN.radians == true {
return BigDouble(atan(double))
}
return atan(double) * 180 / pi
}
/// ATAN2(Y,X) returns the four-quadrant inverse tangent (tan-1) of Y and X, which must be real. The atan2 function follows the convention that atan2(x,x) returns 0 when x is mathematically zero (either 0 or -0).
/// - Parameters:
/// - n1: Y coordinate
/// - n2: X coordinate
func ATAN2(_ n1: BigNumber, _ n2: BigNumber) throws -> BigNumber {
guard let d1 = n1.asDouble() else { throw TablesError.Overflow }
guard let d2 = n2.asDouble() else { throw TablesError.Overflow }
if BN.radians == true {
return BigDouble(atan2(d1, d2))
}
return atan2(d1, d2) * 180 / pi
}
/// Hyperbolic Arc-Tangent of a number.
/// - Parameter number: Any `BigDouble`
func ATANH(_ number: BigNumber) throws -> BigNumber {
guard let double = number.asDouble() else { throw TablesError.Overflow }
return BigDouble(atanh(double))
}
/// Convert number to any base
/// - Parameters:
/// - number: The number you want to use
/// - radix: The base / radix
func BASE(_ number: BigInt, _ radix: BigInt) -> String {
return number.asString(radix: radix.asInt() ?? 10)
}
/// Returns number rounded up, away from zero, to the nearest multiple of significance.
///
/// For example, if you want to avoid using pennies in your prices and your product is priced at $4.42, use the formula `=CEILING(4.42,0.05)` to round prices up to the nearest nickel.
/// - Parameters:
/// - number: The number you want to use
/// - significance: The multiple to which you want to round.
/// - mode: Either 0 or 1 (0 by default, flooring). It will choose between flooring or ceiling the number if it's negative.
func CEILING(_ number: BigDouble, significance: BigDouble = 1, mode: Int = 0) throws -> BigDouble {
guard significance != 0 else { throw TablesError.DivisionByZero }
if number.isPositive() {
return ceil(number / significance) * significance
}
if mode == 0 {
return -1 * floor(abs(number) / significance) * significance
}
return -1 * ceil(abs(number) / significance) * significance
}
/// Returns the number of combinations for a given number of items. Use COMBIN to determine the total possible number of groups for a given number of items.
///
/// It uses combinations with repetitions: `n! / (k! * (n - k)!)`
/// - Parameters:
/// - n: The number of items.
/// - k: The number of items in each combination.
func COMBIN(_ n: BigInt, k: BigInt) throws -> BigInt {
guard let ni = n.asInt() else { throw TablesError.Overflow }
guard let ki = k.asInt() else { throw TablesError.Overflow }
return try combinations(ni, ki)
}
/// Returns the number of combinations (with repetitions) for a given number of items.
///
/// It uses combinations with repetitions: `(n + k - 1)! / (k! * (n - 1)!)`
/// - Parameters:
/// - n: The number of items.
/// - k: The number of items in each combination.
func COMBINA(_ n: BigInt, k: BigInt) throws -> BigInt {
guard let ni = n.asInt() else { throw TablesError.Overflow }
guard let ki = k.asInt() else { throw TablesError.Overflow }
return try combinationsWithRepetitions(ni, ki)
}
/// Cosinus of a number.
/// - Parameter number: Any `BigDouble` less than 2pi
func COS(_ number: BigNumber) throws -> BigNumber {
var number = number
if BN.radians == false {
number = number * pi / BN(180)
}
let mod = number % (2 * pi)
guard let double = mod.asDouble() else { throw TablesError.Overflow }
return BigDouble(cos(double))
}
/// Hyperbolic Cosinus of a number.
/// - Parameter number: Any `BigDouble`
func COSH(_ number: BigNumber) throws -> BigNumber {
guard let double = number.asDouble() else { throw TablesError.Overflow }
return BigDouble(cosh(double))
}
/// Cotangent of a number.
/// - Parameter number: Any `BigDouble`
func COT(_ number: BigNumber) throws -> BigNumber {
guard let double = number.asDouble() else { throw TablesError.Overflow }
guard double != 0 else { throw TablesError.DivisionByZero }
return BigDouble(1) / BigDouble(tan(double))
}
/// Hyperbolic Cotangent of a number.
/// - Parameter number: Any `BigDouble`
func COTH(_ number: BigNumber) throws -> BigNumber {
let e2 = exp(2 * number)
guard e2 != 1 else { throw TablesError.DivisionByZero }
return (e2 + 1) / (e2 - 1)
}
/// Cosecant of a number.
/// - Parameter number: Any `BigDouble`
func CSC(_ number: BigNumber) throws -> BigNumber {
guard let double = number.asDouble() else { throw TablesError.Overflow }
guard sin(double) != 0 else { throw TablesError.DivisionByZero }
return BigDouble(1) / BigDouble(sin(double))
}
/// Hyperbolic Cosecant of a number.
/// - Parameter number: Any `BigDouble`
func CSCH(_ number: BigNumber) throws -> BigNumber {
let e1 = exp(number)
let e2 = exp(-number)
guard e1 != e2 else { throw TablesError.DivisionByZero }
return BigDouble(2) / (e1 - e2)
}
/// Converts a text representation of a number in a given base into a decimal number.
/// - Parameters:
/// - str: Required
/// - radix: Radix must be an integer.
func DECIMAL(_ str: String, _ radix: Int) -> BigNumber {
return BigDouble(str, radix: radix) ?? 0
}
/// Converts radians into degrees.
/// - Parameter rad: The angle in radians that you want to convert.
func DEGREES(_ rad: BigDouble) -> BigNumber {
let abs = rad * BigInt(180) / pi
let mod = abs % BigDouble(360)
return mod
}
/// Returns number rounded up to the nearest even integer.
///
/// You can use this function for processing items that come in twos. For example, a packing crate accepts rows of one or two items. The crate is full when the number of items, rounded up to the nearest two, matches the crate's capacity.
/// - Parameter number: The value to round.
func EVEN(_ number: BigDouble) throws -> BigInt {
return try CEILING(number, significance: -2, mode: 1).rounded()
}
/// Returns e raised to the power of number.
/// The constant e equals 2.71828182845904, the base of the natural logarithm.
/// - Parameter number: The exponent applied to the base e.
func EXP(_ number: BigDouble) -> BigDouble {
return exp(number)
}
/// Returns the factorial of a number. The factorial of a number is equal to `1*2*3*...*` number.
///
///
/// - Parameter int: The nonnegative number for which you want the factorial. If number is not an integer, it is truncated.
func FACT(_ int: BigInt) throws -> BigInt {
return try factorial(int)
}
/// Returns the double factorial of a number. The factorial of a number is equal to `1*2*3*...*` number.
///
///
/// - Parameter int: The value for which to return the double factorial. If number is not an integer, it is truncated.
func FACTDOUBLE(_ int: BigInt) throws -> BigInt {
return try factorial(factorial(int))
}
/// Rounds number down, toward zero, to the nearest multiple of significance.
///
/// For example, if you want to avoid using pennies in your prices and your product is priced at $4.42, use the formula `=FLOOR(4.42,0.05)` to round prices up to the nearest nickel.
/// - Parameters:
/// - number: The numeric value you want to round.
/// - significance: The multiple to which you want to round.
/// - mode: Either 0 or 1 (0 by default, ceiling). It will choose between flooring or ceiling the number if it's negative.
func FLOOR(_ number: BigDouble, significance: BigDouble = 1, mode: Int = 0) throws -> BigDouble {
guard significance != .zero else { throw TablesError.DivisionByZero }
if number.isPositive() {
return floor(number / significance) * significance
}
if mode == 0 {
return -1 * ceil(abs(number) / significance) * significance
}
return -1 * floor(abs(number) / significance) * significance
}
/// Returns the greatest common divisor of two or more integers.
///
/// The greatest common divisor is the largest integer that divides both number1 and number2 without a remainder.
/// - Parameter n: Number1 and 2 are required, subsequent numbers are optional. 1 to 255 values. If any value is not an integer, it is truncated.
func GCD(_ n: BigInt...) throws -> BigInt {
guard n.count >= 2 else { throw TablesError.Arguments }
let r = n.reduce(n.first!) { (r, i) -> BigInt in
return gcd(r, i)
}
return r
}
/// Returns the greatest common divisor of two or more integers.
///
/// The greatest common divisor is the largest integer that divides both number1 and number2 without a remainder.
/// - Parameter n: Number1 and 2 are required, subsequent numbers are optional. 1 to 255 values. If any value is not an integer, it is truncated.
func GCD(array n: [BigInt]) throws -> BigInt {
guard n.count >= 2 else { throw TablesError.Arguments }
let r = n.reduce(n.first!) { (r, i) -> BigInt in
return gcd(r, i)
}
return r
}
/// Rounds a number down to the nearest integer.
/// - Parameter n: The real number you want to round down to an integer.
func INT(_ n: BigDouble) throws -> BigInt {
return try FLOOR(n).rounded()
}
/// Returns the least common multiple of integers.
/// The least common multiple is the smallest positive integer that is a multiple of all integer arguments number1, number2, and so on. Use LCM to add fractions with different denominators.
///
/// - Parameter n: Number1 and 2 are required, subsequent numbers are optional. 1 to 255 values. If any value is not an integer, it is truncated.
func LCM(_ n: BigInt...) throws -> BigInt {
guard n.count >= 2 else { throw TablesError.Arguments }
let r = n.reduce(n.first!) { (r, i) -> BigInt in
return lcm(r, i)
}
return r
}
/// Returns the least common multiple of integers.
/// The least common multiple is the smallest positive integer that is a multiple of all integer arguments number1, number2, and so on. Use LCM to add fractions with different denominators.
///
/// - Parameter n: Number1 and 2 are required, subsequent numbers are optional. 1 to 255 values. If any value is not an integer, it is truncated.
func LCM(array n: [BigInt]) throws -> BigInt {
guard n.count >= 2 else { throw TablesError.Arguments }
let r = n.reduce(n.first!) { (r, i) -> BigInt in
return lcm(r, i)
}
return r
}
/// Returns the natural logarithm of a number. Natural logarithms are based on the constant e (2.71828182845904)
///
/// LN is the inverse of the EXP function.
/// - Parameter n: The positive real number for which you want the natural logarithm.
func LN(_ n: BigDouble) -> BigDouble {
return ln(n)
}
/// Returns the logarithm of a number to the base you specify.
/// - Parameters:
/// - n: The positive real number for which you want the logarithm.
/// - base: The base of the logarithm. If base is omitted, it is assumed to be 10.
func LOG(_ n: BigDouble, base: BigDouble = 10) throws -> BigDouble {
let den = LN(base)
guard den != 0 else { throw TablesError.DivisionByZero }
return LN(n) / den
}
/// Returns the logarithm base 10
/// - Parameters:
/// - n: The positive real number for which you want the logarithm.
func LOG10(_ n: BigDouble) throws -> BigDouble {
return try LOG(n)
}
// MARK: TODO MATRICES
#if os(macOS)
/// Returns the matrix determinant
/// - Parameter lhs: The `Matrix` you want to use.
func MDETERM(_ lhs: Matrix) throws -> BigDouble {
guard let det = lhs.determinant() else { throw TablesError.NULL }
return det
}
/// The MINVERSE function returns the inverse matrix for a matrix stored in a `Matrix` object (Tables will convert that when using the parser).
/// - Parameter m: A numeric Matrix with an equal number of rows and columns.
func MINVERSE(_ m: Matrix) -> Matrix {
return m.inverse()
}
/// The MMULT function returns the matrix product of two `Matrix`. The result is an array with the same number of rows as lhs and the same number of columns as rhs.
/// - Parameters:
/// - lhs: The `Matrix` you want to multiply.
/// - rhs: The `Matrix` you want to multiply.
func MMULT(_ lhs: Matrix, _ rhs: Matrix) -> Matrix {
return lhs * rhs
}
#endif
/// MROUND returns a number rounded to the desired multiple.
/// - Parameters:
/// - n: The value to round.
/// - m: The multiple to which you want to round number.
func MROUND(_ n: BigDouble, _ m: BigDouble) throws -> BigDouble {
guard (n * m).isPositive() else { throw TablesError.Arguments }
return BigDouble((n / m).rounded()) * m
}
/// Returns the remainder after number is divided by divisor.
///
/// The result has the same sign as divisor.
/// - Parameters:
/// - a: The number for which you want to find the remainder.
/// - b: The number by which you want to divide number.
func MOD(_ a: BigDouble, _ b: BigDouble) -> BigDouble {
return a % b
}
/// Returns the ratio of the factorial of a sum of values to the product of factorials.
/// - Parameter numbers: Number1 is required, subsequent numbers are optional. 1 to 255 values for which you want the multinomial.
func MULTINOMIAL(_ numbers: BigInt...) throws -> BigDouble {
let sum = numbers.reduce(BigInt.zero) { $0 + $1 }
let upperFac = try BigDouble(factorial(sum))
let fac = try BigDouble(numbers.reduce(BigInt.zero) { try $0 + factorial($1) })
let div = upperFac / fac
return div
}
/// Returns the ratio of the factorial of a sum of values to the product of factorials.
/// - Parameter numbers: Number1 is required, subsequent numbers are optional. 1 to 255 values for which you want the multinomial.
func MULTINOMIAL(array numbers: [BigInt]) throws -> BigDouble {
let sum = numbers.reduce(BigInt.zero) { $0 + $1 }
let upperFac = try BigDouble(factorial(sum))
let fac = try BigDouble(numbers.reduce(BigInt.zero) { try $0 + factorial($1) })
let div = upperFac / fac
return div
}
/// Returns number rounded up to the nearest odd integer.
///
/// Regardless of the sign of number, a value is rounded up when adjusted away from zero. If number is an odd integer, no rounding occurs.
/// - Parameter n: The value to round.
func ODD(_ n: BigDouble) -> BigInt {
let r = ceil(abs(n))
guard !r.isOdd() else { return BigInt(sign: n.sign, limbs: (r).limbs) }
return BigInt(sign: n.sign, limbs: (r + 1).limbs)
}
/// Returns the mathematical constant
///
/// Originally defined as the ratio of a circle’s circumference to its diameter, it now has various equivalent definitions and appears in many formulas in all areas of mathematics and physics. It is approximately equal to 3.14159. It has been represented by the Greek letter “π” since the mid-18th century, though it is also sometimes spelled out as “pi”. It is also called Archimedes’ constant.
///
func PI() -> BigDouble {
return pi
}
/// Returns the result of a number raised to a power.
///
/// Let's say you want to calculate an extremely small tolerance level for a machined part or the vast distance between two galaxies. To raise a number to a power, use the POWER function.
///
/// > The "^" operator can be used instead of POWER to indicate to what power the base number is to be raised, such as in 5^2.
/// - Parameters:
/// - a: The base number. It can be any real number.
/// - b: The exponent to which the base number is raised.
func POWER(_ a: BigDouble, _ b: BigDouble) -> BigDouble {
return pow(a, b)
}
/// The PRODUCT function multiplies all the numbers given as arguments and returns the product. For example, if cells A1 and A2 contain numbers, you can use the formula =PRODUCT(A1, A2) to multiply those two numbers together. You can also perform the same operation by using the multiply (*) mathematical operator; for example, =A1 * A2.
/// The PRODUCT function is useful when you need to multiply many cells together. For example, the formula =PRODUCT(A1:A3, C1:C3) is equivalent to =A1 * A2 * A3 * C1 * C2 * C3.
/// - Parameter ns: The first number or range that you want to multiply. Continue by adding additional numbers or ranges that you want to multiply, up to a maximum of 255 arguments.
func PRODUCT(_ ns: BigDouble...) -> BigDouble {
return ns.reduce(BigDouble(1)) { $0 * $1 }
}
/// The PRODUCT function multiplies all the numbers given as arguments and returns the product. For example, if cells A1 and A2 contain numbers, you can use the formula =PRODUCT(A1, A2) to multiply those two numbers together. You can also perform the same operation by using the multiply (*) mathematical operator; for example, =A1 * A2.
/// The PRODUCT function is useful when you need to multiply many cells together. For example, the formula =PRODUCT(A1:A3, C1:C3) is equivalent to =A1 * A2 * A3 * C1 * C2 * C3.
/// - Parameter ns: The first number or range that you want to multiply. Continue by adding additional numbers or ranges that you want to multiply, up to a maximum of 255 arguments.
func PRODUCT(_ ns: [BigDouble]) -> BigDouble {
return ns.reduce(BigDouble(1)) { $0 * $1 }
}
/// Returns the integer portion of a division. Use this function when you want to discard the remainder of a division.
///
/// > Tip: If you want to divide numeric values, you should use the "/" operator as there isn't a DIVIDE function in `Euler.Tables`. For example, to divide 5 by 2, you would type =5/2 into a cell, which returns 2.5. The QUOTIENT function for these same numbers =QUOTIENT(5,2) returns 2, since QUOTIENT doesn't return a remainder.
/// - Parameters:
/// - numerator: The dividend.
/// - denominator: The divisor.
func QUOTIENT(_ numerator: BigDouble, _ denominator: BigDouble) throws -> BigInt {
guard denominator != 0 else { throw TablesError.DivisionByZero }
let div = numerator / denominator
return ceil(div)
}
/// Converts degrees to radians.
/// - Parameter angle: An angle in degrees that you want to convert.
func RADIANS(_ angle: BigDouble) -> BigDouble {
return angle * pi / BigDouble(180)
}
/// RAND returns an evenly distributed random real number greater than or equal to 0 and less than 1.
///
/// A new random real number is returned every time the worksheet is calculated.
func RAND() -> BigDouble {
return BigDouble(Double.random(in: 0..<1))
}
/// Returns a random integer number between the numbers you specify. A new random integer number is returned every time the worksheet is calculated.
/// - Parameters:
/// - a: The smallest integer RANDBETWEEN will return.
/// - b: The largest integer RANDBETWEEN will return.
func RANDBETWEEN(_ a: BigDouble, _ b: BigDouble) -> BigDouble {
guard let c = a.asDouble(), let d = b.asDouble() else { return RAND() * (b - a) + a }
guard c < d else { return RAND() * (b - a) + a }
return BigDouble(Double.random(in: c..<d))
}
// MARK: Todo Roman
/// The ROUND function rounds a number to a specified number of digits.
///
/// For example, if cell A1 contains 23.7825, and you want to round that value to two decimal places, you can use the following formula:
///
/// `=ROUND(A1, 2)`
/// The result of this function is 23.78.
///
/// - If `digits` is greater than 0 (zero), then number is rounded to the specified number of decimal places.
/// - If `digits` is 0, the number is rounded to the nearest integer.
/// - If `digits` is less than 0, the number is rounded to the left of the decimal point.
/// - To always round up (away from zero), use the ROUNDUP function.
/// - To always round down (toward zero), use the ROUNDDOWN function.
/// - To round a number to a specific multiple (for example, to round to the nearest 0.5), use the MROUND function.
/// - Parameters:
/// - n: The number that you want to round.
/// - digits: The number of digits to which you want to round the number argument.
func ROUND(_ n: BigDouble, digits: BigInt) -> BigDouble {
let powed = pow(10, digits)
let times = n * powed
return BigDouble(times.rounded()) / powed
}
/// Rounds a number down, toward zero.
/// - Parameters:
/// - n: Any real number that you want rounded down.
/// - digits: The number of digits to which you want to round number.
func ROUNDDOWN(_ n: BigDouble, digits: BigInt) -> BigDouble {
let sign: BigDouble = (n > 0) ? 1 : -1
let powed = pow(10, digits)
return sign * BigDouble(floor(abs(n) * powed)) / powed
}
/// Rounds a number up, away from zero.
/// - Parameters:
/// - n: Any real number that you want rounded up.
/// - digits: The number of digits to which you want to round number.
func ROUNDUP(_ n: BigDouble, digits: BigInt) -> BigDouble {
let sign: BigDouble = (n > 0) ? 1 : -1
let powed = pow(10, digits)
return sign * BigDouble(ceil(abs(n) * powed)) / powed
}
/// Returns the secant of an angle.
/// - Parameter n: Number is the angle in radians for which you want the secant.
func SEC(_ n: BigDouble) throws -> BigDouble {
let cos = try COS(n)
return BigDouble(1) / cos
}
/// Returns the hyperbolic secant of an angle.
/// - Parameter n: Number is the angle in radians for which you want the hyperbolic secant.
func SECH(_ n: BigDouble) -> BigDouble {
return 2 / (EXP(n) + EXP(-n))
}
/// Many functions can be approximated by a power series expansion.
/// Returns the sum of a power series based on the formula:
/// `$$ SERIES(x, n, m, a)=a_1x^n+a_2x^{n+m}+...+a_ix^{n+(i-1)m}$$`
/// - Parameters:
/// - x: The input value to the power series.
/// - n: The initial power to which you want to raise x.
/// - m: The step by which to increase n for each term in the series.
/// - coefficients: A set of coefficients by which each successive power of x is multiplied. The number of values in coefficients determines the number of terms in the power series. For example, if there are three values in coefficients, then there will be three terms in the power series.
func SERIESSUM(_ x: BigDouble, _ n: BigDouble, _ m: BigDouble, _ coefficients: BigDouble...) throws -> BigDouble {
guard coefficients.count >= 1 else { throw TablesError.Arguments }
var result = coefficients[0] * pow(x, n)
var i = 1
while i < coefficients.count {
result += coefficients[i] * pow(x, n + BigDouble(i) * m)
i += 1
}
return result
}
/// Many functions can be approximated by a power series expansion.
/// Returns the sum of a power series based on the formula:
/// `$$ SERIES(x, n, m, a)=a_1x^n+a_2x^{n+m}+...+a_ix^{n+(i-1)m}$$`
/// - Parameters:
/// - x: The input value to the power series.
/// - n: The initial power to which you want to raise x.
/// - m: The step by which to increase n for each term in the series.
/// - coefficients: A set of coefficients by which each successive power of x is multiplied. The number of values in coefficients determines the number of terms in the power series. For example, if there are three values in coefficients, then there will be three terms in the power series.
func SERIESSUM(_ x: BigDouble, _ n: BigDouble, _ m: BigDouble, _ coefficients: [BigDouble]) throws -> BigDouble {
guard coefficients.count >= 1 else { throw TablesError.Arguments }
var result = coefficients[0] * pow(x, n)
var i = 1
while i < coefficients.count {
result += coefficients[i] * pow(x, n + BigDouble(i) * m)
i += 1
}
return result
}
/// Determines the sign of a number. Returns 1 if the number is positive, zero (0) if the number is 0, and -1 if the number is negative.
/// - Parameter n: Any real number.
func SIGN(_ n: BigDouble) -> BigInt {
if (n.sign == true) {
return -1;
} else if (n == .zero) {
return 0;
} else {
return 1;
}
}
/// Sinus function
/// - Parameter n: Any real less than 2pi
func SIN(_ n: BigDouble) throws -> BigDouble {
var n = n
if BN.radians == false {
n = n * pi / BN(180)
}
let mod = n % (2 * pi)
guard let double = mod.asDouble() else { throw TablesError.Overflow }
return BigDouble(sin(double))
}
/// Hyperbolic sinus function
/// - Parameter n: Any real
func SINH(_ n: BigDouble) -> BigDouble {
return (exp(n) - exp(-n)) / 2
}
/// Returns a positive square root.
/// - Parameter n: The number for which you want the square root.
func SQRT(_ n: BigDouble) throws -> BigDouble {
guard let sqrt = n.squareRoot() else { throw TablesError.NULL }
return sqrt
}
/// Returns a positive nth root.
/// - Parameter n: The number for which you want the square root.
/// - Parameter base: The nth root (ex: 2, 3,...).
func ROOT(_ n: BigDouble, _ base: Int) throws -> BigDouble {
guard let rt = n.nthroot(base) else { throw TablesError.NULL }
return rt
}
/// Returns the square root of (number * pi).
/// - Parameter n: The number by which pi is multiplied.
func SQRTPI(_ n: BigDouble) throws -> BigDouble {
guard let sqrt = abs(n * pi).squareRoot() else { throw TablesError.NULL }
return sqrt
}
// MARK: Todo: SUBTOTAL
/// Add 2 numbers
/// - Parameters:
/// - a: Any real
/// - b: Any real
func ADD(_ a: BigDouble, _ b: BigDouble) -> BigDouble {
return a + b
}
/// Substract 2 numbers
/// - Parameters:
/// - a: Any real
/// - b: Any real
func MINUS(_ a: BigDouble, _ b: BigDouble) -> BigDouble {
return a - b
}
/// Divide 2 numbers
/// - Parameters:
/// - a: Any real
/// - b: Any real
func DIVIDE(_ a: BigDouble, _ b: BigDouble) throws -> BigDouble {
guard b != 0 else { throw TablesError.DivisionByZero }
return a / b
}
/// Multiply 2 numbers
/// - Parameters:
/// - a: Any real
/// - b: Any real
func MULTIPLY(_ a: BigDouble, _ b: BigDouble) -> BigDouble {
return a * b
}
/// Greater than or equal (`>=`)
func GTE(_ a: BigDouble, _ b: BigDouble) -> Bool {
return a >= b
}
/// Less than (`<`)
func LT(_ a: BigDouble, _ b: BigDouble) -> Bool {
return a < b
}
/// Less than or equal (`<=`)
func LTE(_ a: BigDouble, _ b: BigDouble) -> Bool {
return a <= b
}
/// Equal (`==`)
func EQ(_ a: BigDouble, _ b: BigDouble) -> Bool {
return a == b
}
/// Not equal (`==`)
func NE(_ a: BigDouble, _ b: BigDouble) -> Bool {
return a != b
}
/// Returns the result of a number raised to a power. Same as `POWER`
///
/// Let's say you want to calculate an extremely small tolerance level for a machined part or the vast distance between two galaxies. To raise a number to a power, use the POWER function.
///
/// > The "^" operator can be used instead of POWER to indicate to what power the base number is to be raised, such as in 5^2.
/// - Parameters:
/// - a: The base number. It can be any real number.
/// - b: The exponent to which the base number is raised.
func POW(_ a: BigDouble, _ b: BigDouble) -> BigDouble {
return POWER(a, b)
}
/// The SUM function adds values.
/// - Parameter n: The numbers you want to sum
func SUM(_ n: BigDouble...) -> BigDouble {
return n.reduce(BigDouble.zero) { $0 + $1 }
}
/// The SUM function adds values.
/// - Parameter n: The numbers you want to sum
func SUM(_ n: [BigDouble]) -> BigDouble {
return n.reduce(BigDouble.zero) { $0 + $1 }
}
// MARK: TODO: SUMIF(s)
#if os(macOS)
/// The SUMPRODUCT function returns the sum of the products of corresponding ranges or arrays.
///
/// Example: `SUMPRODUCT([1, 2, 3], [2, 3, 4]) //= 20`
/// - Parameter a: An array of numbers
func SUMPRODUCT(_ a: [BigDouble]...) throws -> BigDouble {
guard let f = a.first else { return .zero }
var vector = Matrix(rows: f.count, columns: 1, repeatedValue: 1)
for i in a {
guard i.count == f.count else { throw TablesError.Arguments }
let v1 = Matrix(i.map { $0.asDouble() ?? 0 }, isColumnVector: true)
vector = vector * v1
}
return vector.sum()
}
/// The SUMPRODUCT function returns the sum of the products of corresponding ranges or arrays.
///
/// Example: `SUMPRODUCT([1, 2, 3], [2, 3, 4]) //= 20`
/// - Parameter a: An array of numbers
func SUMPRODUCT(_ a: [[BigDouble]]) throws -> BigDouble {
guard let f = a.first else { return .zero }
var vector = Matrix(rows: f.count, columns: 1, repeatedValue: 1)
for i in a {
guard i.count == f.count else { throw TablesError.Arguments }
let v1 = Matrix(i.map { $0.asDouble() ?? 0 }, isColumnVector: true)
vector = vector * v1
}
return vector.sum()
}
#endif
/// Returns the sum of the squares of the arguments.
/// - Parameter n: The numbers you want to square and sum
func SUMSQ(_ n: BigDouble...) -> BigDouble {
return n.reduce(BigDouble.zero) { $0 + ($1 * $1) }
}
/// Returns the sum of the squares of the arguments.
/// - Parameter n: The numbers you want to square and sum
func SUMSQ(_ n: [BigDouble]) -> BigDouble {
return n.reduce(BigDouble.zero) { $0 + ($1 * $1) }
}
/// Returns the sum of the difference of squares of corresponding values in two arrays.
/// - Parameters:
/// - a1: List of number
/// - a2: List of number
func SUMX2MY2(_ a1: [BigDouble], _ a2: [BigDouble]) throws -> BigDouble {
guard a1.count == a2.count else { throw TablesError.Arguments }
let s1 = a1.reduce(BigDouble.zero) { $0 + ($1 * $1) }
let s2 = a2.reduce(BigDouble.zero) { $0 + ($1 * $1) }
return s1 - s2
}
/// Returns the sum of the sum of squares of corresponding values in two arrays. The sum of the sum of squares is a common term in many statistical calculations.
/// - Parameters:
/// - a1: List of number
/// - a2: List of number
func SUMX2PY2(_ a1: [BigDouble], _ a2: [BigDouble]) throws -> BigDouble {
guard a1.count == a2.count else { throw TablesError.Arguments }
let s1 = a1.reduce(BigDouble.zero) { $0 + ($1 * $1) }
let s2 = a2.reduce(BigDouble.zero) { $0 + ($1 * $1) }
return s1 + s2
}
/// Returns the sum of squares of differences of corresponding values in two arrays.
/// - Parameters:
/// - a1: List of number
/// - a2: List of number
func SUMXMY2(_ a1: [BigDouble], _ a2: [BigDouble]) throws -> BigDouble {
guard a1.count == a2.count else { throw TablesError.Arguments }
var r: BigDouble = 0
for i in 0..<a1.count {
let d = a1[i] - a2[i]
r += d * d
}
return r
}
/// Tangent of a number.
/// - Parameter number: Any `BigDouble` less than 2pi
func TAN(_ number: BigNumber) throws -> BigNumber {
var number = number
if BN.radians == false {
number = number * pi / BN(180)
}
let mod = number % (2 * pi)
guard let double = mod.asDouble() else { throw TablesError.Overflow }
return BigDouble(tan(double))
}
/// Hyperbolic Tangent of a number.
/// - Parameter number: Any `BigDouble`
func TANH(_ number: BigNumber) throws -> BigNumber {
guard let double = number.asDouble() else { throw TablesError.Overflow }
return BigDouble(tanh(double))
}
/// Truncates a number to an integer by removing the fractional part of the number.
/// - Parameters:
/// - number: The number you want to truncate.
/// - digits: Optional. A number specifying the precision of the truncation.
func TRUNC(_ number: BigDouble, _ digits: Int = 0) -> BigDouble {
let sign = (number > 0) ? 1 : -1;
return BigDouble(sign) * BigDouble(floor(abs(number) * pow(10, digits))) / pow(10, digits)
}
}