@@ -54,6 +54,7 @@ module BigMath
54
54
# #=> "0.1414213562373095048801688724e1"
55
55
#
56
56
def sqrt ( x , prec )
57
+ x = BigMath . _coerce_to_bigdecimal ( x , :sqrt )
57
58
x . sqrt ( prec )
58
59
end
59
60
@@ -67,7 +68,8 @@ def sqrt(x, prec)
67
68
# #=> "0.125992104989487316476721060727822e1"
68
69
#
69
70
def cbrt ( x , prec )
70
- raise ArgumentError , "Zero or negative precision for cbrt" if prec <= 0
71
+ BigMath . _validate_prec ( prec , :cbrt )
72
+ x = BigMath . _coerce_to_bigdecimal ( x , :cbrt )
71
73
return x if x . zero? || x . infinite? || x . nan?
72
74
return -cbrt ( -x , prec ) if x < 0
73
75
@@ -92,6 +94,8 @@ def cbrt(x, prec)
92
94
# #=> "0.2236067977499789696409173668333333334e1"
93
95
#
94
96
def hypot ( x , y , prec )
97
+ BigMath . _validate_prec ( prec , :hypot )
98
+ x = BigMath . _coerce_to_bigdecimal ( x , :hypot )
95
99
return BigDecimal ::NAN if x . nan? || y . nan?
96
100
return BigDecimal ::INFINITY if x . infinite? || y . infinite?
97
101
prec2 = prec + BigDecimal . double_fig
@@ -110,7 +114,8 @@ def hypot(x, y, prec)
110
114
# #=> "0.70710678118654752440082036563292800375e0"
111
115
#
112
116
def sin ( x , prec )
113
- raise ArgumentError , "Zero or negative precision for sin" if prec <= 0
117
+ BigMath . _validate_prec ( prec , :sin )
118
+ x = BigMath . _coerce_to_bigdecimal ( x , :sin )
114
119
return BigDecimal ( "NaN" ) if x . infinite? || x . nan?
115
120
n = prec + BigDecimal . double_fig
116
121
one = BigDecimal ( "1" )
@@ -156,7 +161,8 @@ def sin(x, prec)
156
161
# #=> "-0.999999999999999999999999999999856613163740061349e0"
157
162
#
158
163
def cos ( x , prec )
159
- raise ArgumentError , "Zero or negative precision for cos" if prec <= 0
164
+ BigMath . _validate_prec ( prec , :cos )
165
+ x = BigMath . _coerce_to_bigdecimal ( x , :cos )
160
166
return BigDecimal ( "NaN" ) if x . infinite? || x . nan?
161
167
n = prec + BigDecimal . double_fig
162
168
one = BigDecimal ( "1" )
@@ -217,6 +223,8 @@ def cos(x, prec)
217
223
# #=> "0.17320508075688772935274463415059e1"
218
224
#
219
225
def tan ( x , prec )
226
+ BigMath . _validate_prec ( prec , :tan )
227
+ x = BigMath . _coerce_to_bigdecimal ( x , :tan )
220
228
return BigDecimal ( 0 ) if x . zero?
221
229
222
230
# BigMath calculates sin with relative precision only when x.abs is small
@@ -238,7 +246,8 @@ def tan(x, prec)
238
246
# #=> "0.52359877559829887307710723054659e0"
239
247
#
240
248
def asin ( x , prec )
241
- raise ArgumentError , "Zero or negative precision for asin" if prec <= 0
249
+ BigMath . _validate_prec ( prec , :asin )
250
+ x = BigMath . _coerce_to_bigdecimal ( x , :asin )
242
251
raise Math ::DomainError , "Out of domain argument for asin" if x < -1 || x > 1
243
252
return BigDecimal ::NAN if x . nan?
244
253
prec2 = prec + BigDecimal . double_fig
@@ -263,7 +272,8 @@ def asin(x, prec)
263
272
# #=> "0.10471975511965977461542144610932e1"
264
273
#
265
274
def acos ( x , prec )
266
- raise ArgumentError , "Zero or negative precision for acos" if prec <= 0
275
+ BigMath . _validate_prec ( prec , :acos )
276
+ x = BigMath . _coerce_to_bigdecimal ( x , :acos )
267
277
raise Math ::DomainError , "Out of domain argument for acos" if x < -1 || x > 1
268
278
269
279
return PI ( prec ) / 2 - asin ( x , prec ) if x < 0
@@ -287,7 +297,8 @@ def acos(x, prec)
287
297
# #=> "-0.785398163397448309615660845819878471907514682065e0"
288
298
#
289
299
def atan ( x , prec )
290
- raise ArgumentError , "Zero or negative precision for atan" if prec <= 0
300
+ BigMath . _validate_prec ( prec , :atan )
301
+ x = BigMath . _coerce_to_bigdecimal ( x , :atan )
291
302
return BigDecimal ( "NaN" ) if x . nan?
292
303
pi = PI ( prec )
293
304
x = -x if neg = x < 0
@@ -324,6 +335,9 @@ def atan(x, prec)
324
335
# #=> "-0.785398163397448309615660845819878471907514682065e0"
325
336
#
326
337
def atan2 ( y , x , prec )
338
+ BigMath . _validate_prec ( prec , :atan2 )
339
+ x = BigMath . _coerce_to_bigdecimal ( x , :atan2 )
340
+ y = BigMath . _coerce_to_bigdecimal ( y , :atan2 )
327
341
if x . infinite? || y . infinite?
328
342
one = BigDecimal ( 1 )
329
343
zero = BigDecimal ( 0 )
@@ -356,7 +370,8 @@ def atan2(y, x, prec)
356
370
# #=> "0.11752011936438014568823818505956e1"
357
371
#
358
372
def sinh ( x , prec )
359
- raise ArgumentError , "Zero or negative precision for sinh" if prec <= 0
373
+ BigMath . _validate_prec ( prec , :sinh )
374
+ x = BigMath . _coerce_to_bigdecimal ( x , :sinh )
360
375
return BigDecimal ::NAN if x . nan?
361
376
return x if x . infinite?
362
377
@@ -378,7 +393,8 @@ def sinh(x, prec)
378
393
# #=> "0.15430806348152437784779056207571e1"
379
394
#
380
395
def cosh ( x , prec )
381
- raise ArgumentError , "Zero or negative precision for cosh" if prec <= 0
396
+ BigMath . _validate_prec ( prec , :cosh )
397
+ x = BigMath . _coerce_to_bigdecimal ( x , :cosh )
382
398
return BigDecimal ::NAN if x . nan?
383
399
return BigDecimal ::INFINITY if x . infinite?
384
400
@@ -399,7 +415,8 @@ def cosh(x, prec)
399
415
# #=> "0.7615941559557648881194582826048e0"
400
416
#
401
417
def tanh ( x , prec )
402
- raise ArgumentError , "Zero or negative precision for tanh" if prec <= 0
418
+ BigMath . _validate_prec ( prec , :tanh )
419
+ x = BigMath . _coerce_to_bigdecimal ( x , :tanh )
403
420
return BigDecimal ::NAN if x . nan?
404
421
return BigDecimal ( x . infinite? ) if x . infinite?
405
422
@@ -422,7 +439,8 @@ def tanh(x, prec)
422
439
# #=> "0.881373587019543025232609324892919887466177636058e0"
423
440
#
424
441
def asinh ( x , prec )
425
- raise ArgumentError , "Zero or negative precision for tanh" if prec <= 0
442
+ BigMath . _validate_prec ( prec , :asinh )
443
+ x = BigMath . _coerce_to_bigdecimal ( x , :asinh )
426
444
return x if x . nan? || x . infinite?
427
445
return -asinh ( -x , prec ) if x < 0
428
446
@@ -442,7 +460,8 @@ def asinh(x, prec)
442
460
# #=> "0.1316957896924816708625046347239934461496535769096e1"
443
461
#
444
462
def acosh ( x , prec )
445
- raise ArgumentError , "Zero or negative precision for tanh" if prec <= 0
463
+ BigMath . _validate_prec ( prec , :acosh )
464
+ x = BigMath . _coerce_to_bigdecimal ( x , :acosh )
446
465
raise Math ::DomainError , "Out of domain argument for acosh" if x < 1
447
466
return BigDecimal ::INFINITY if x . infinite?
448
467
return BigDecimal ::NAN if x . nan?
@@ -462,7 +481,8 @@ def acosh(x, prec)
462
481
# #=> "0.54930614433405484569762261846126e0"
463
482
#
464
483
def atanh ( x , prec )
465
- raise ArgumentError , "Zero or negative precision for tanh" if prec <= 0
484
+ BigMath . _validate_prec ( prec , :atanh )
485
+ x = BigMath . _coerce_to_bigdecimal ( x , :atanh )
466
486
raise Math ::DomainError , "Out of domain argument for atanh" if x < -1 || x > 1
467
487
return BigDecimal ::NAN if x . nan?
468
488
return BigDecimal ::INFINITY if x == 1
@@ -488,7 +508,8 @@ def atanh(x, prec)
488
508
# #=> "0.158496250072115618145373894394782e1"
489
509
#
490
510
def log2 ( x , prec )
491
- raise ArgumentError , "Zero or negative precision for log2" if prec <= 0
511
+ BigMath . _validate_prec ( prec , :log2 )
512
+ x = BigMath . _coerce_to_bigdecimal ( x , :log2 , true )
492
513
return BigDecimal ::NAN if x . nan?
493
514
return BigDecimal ::INFINITY if x . infinite? == 1
494
515
@@ -513,7 +534,8 @@ def log2(x, prec)
513
534
# #=> "0.47712125471966243729502790325512e0"
514
535
#
515
536
def log10 ( x , prec )
516
- raise ArgumentError , "Zero or negative precision for log10" if prec <= 0
537
+ BigMath . _validate_prec ( prec , :log10 )
538
+ x = BigMath . _coerce_to_bigdecimal ( x , :log10 , true )
517
539
return BigDecimal ::NAN if x . nan?
518
540
return BigDecimal ::INFINITY if x . infinite? == 1
519
541
@@ -532,7 +554,7 @@ def log10(x, prec)
532
554
# #=> "0.3141592653589793238462643388813853786957412e1"
533
555
#
534
556
def PI ( prec )
535
- raise ArgumentError , "Zero or negative precision for PI" if prec <= 0
557
+ BigMath . _validate_prec ( prec , :PI )
536
558
n = prec + BigDecimal . double_fig
537
559
zero = BigDecimal ( "0" )
538
560
one = BigDecimal ( "1" )
@@ -577,7 +599,7 @@ def PI(prec)
577
599
# #=> "0.271828182845904523536028752390026306410273e1"
578
600
#
579
601
def E ( prec )
580
- raise ArgumentError , "Zero or negative precision for E" if prec <= 0
602
+ BigMath . _validate_prec ( prec , :E )
581
603
BigMath . exp ( 1 , prec )
582
604
end
583
605
end
0 commit comments