8000 * lib/cmath.rb: [DOC] Add docs [ci skip][Fix GH-909][Bug #11162] · sonots/ruby@41b6910 · GitHub
[go: up one dir, main page]

Skip to content

Commit 41b6910

Browse files
committed
* lib/cmath.rb: [DOC] Add docs [ci skip][Fix rubyGH-909][Bug ruby#11162]
Patch provided by @davydovanton git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50793 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 8538189 commit 41b6910

File tree

1 file changed

+56
-13
lines changed

1 file changed

+56
-13
lines changed

lib/cmath.rb

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
##
2+
# = Trigonometric and transcendental functions for complex numbers.
3+
#
24
# CMath is a library that provides trigonometric and transcendental
3-
# functions for complex numbers.
5+
# functions for complex numbers. The functions in this module accept
6+
# integers, floating-point numbers or complex numbers as arguments.
7+
#
8+
# Note that the selection of functions is similar, but not identical,
9+
# to that in module math. The reason for having two modules is that
10+
# some users aren’t interested in complex numbers, and perhaps don’t
11+
# even know what they are. They would rather have Math.sqrt(-1) raise
12+
# an exception than return a complex number.
413
#
514
# == Usage
615
#
7-
# To start using this library, simply:
16+
# To start using this library, simply require cmath library:
817
#
918
# require "cmath"
1019
#
11-
# Square root of a negative number is a complex number.
20+
# And after call any CMath function. For example:
21+
#
22+
# CMath.sqrt(-9) #=> 0+3.0i
23+
# CMath.exp(0 + 0i) #=> 1.0+0.0i
24+
# CMath.log10(-5.to_c) #=> (0.6989700043360187+1.3643763538418412i)
1225
#
13-
# CMath.sqrt(-9) #=> 0+3.0i
1426
#
27+
# For more information you can see Complec class.
1528

1629
module CMath
1730

@@ -44,9 +57,7 @@ module CMath
4457
##
4558
# Math::E raised to the +z+ power
4659
#
47-
# exp(Complex(0,0)) #=> 1.0+0.0i
48-
# exp(Complex(0,PI)) #=> -1.0+1.2246467991473532e-16i
49-
# exp(Complex(0,PI/2.0)) #=> 6.123233995736766e-17+1.0i
60+
# CMath.exp(2i) #=> (-0.4161468365471424+0.9092974268256817i)
5061
def exp(z)
5162
begin
5263
if z.real?
@@ -62,10 +73,11 @@ def exp(z)
6273
end
6374

6475
##
65-
# Returns the natural logarithm of Complex. If a second argument is given,
76+
# Returns the natural logarithm of Complex. If a second argument is given,
6677
# it will be the base of logarithm.
6778
#
68-
# log(Complex(0,0)) #=> -Infinity+0.0i
79+
# CMath.log(1 + 4i) #=> (1.416606672028108+1.3258176636680326i)
80+
# CMath.log(1 + 4i, 10) #=> (0.6152244606891369+0.5757952953408879i)
6981
def log(z, b=::Math::E)
7082
begin
7183
if z.real? && z >= 0 && b >= 0
@@ -80,6 +92,8 @@ def log(z, b=::Math::E)
8092

8193
##
8294
# returns the base 2 logarithm of +z+
95+
#
96+
# CMath.log2(-1) => (0.0+4.532360141827194i)
8397
def log2(z)
8498
begin
8599
if z.real? and z >= 0
@@ -94,6 +108,8 @@ def log2(z)
94108

95109
##
96110
# returns the base 10 logarithm of +z+
111+
#
112+
# CMath.log10(-1) #=> (0.0+1.3643763538418412i)
97113
def log10(z)
98114
begin
99115
if z.real? and z >= 0
@@ -108,9 +124,8 @@ def log10(z)
108124

109125
##
110126
# Returns the non-negative square root of Complex.
111-
# sqrt(-1) #=> 0+1.0i
112-
# sqrt(Complex(-1,0)) #=> 0.0+1.0i
113-
# sqrt(Complex(0,8)) #=> 2.0+2.0i
127+
#
128+
# CMath.sqrt(-1 + 0i) #=> 0.0+1.0i
114129
def sqrt(z)
115130
begin
116131
if z.real?
@@ -136,12 +151,16 @@ def sqrt(z)
136151

137152
##
138153
# returns the principal value of the cube root of +z+
154+
#
155+
# CMath.cbrt(1 + 4i) #=> (1.449461632813119+0.6858152562177092i)
139156
def cbrt(z)
140157
z ** (1.0/3)
141158
end
142159

143160
##
144161
# returns the sine of +z+, where +z+ is given in radians
162+
#
163+
# CMath.sin(1 + 1i) #=> (1.2984575814159773+0.6349639147847361i)
145164
def sin(z)
146165
begin
147166
if z.real?
@@ -157,6 +176,8 @@ def sin(z)
157176

158177
##
159178
# returns the cosine of +z+, where +z+ is given in radians
179+
#
180+
# CMath.cos(1 + 1i) #=> (0.8337300251311491-0.9888977057628651i)
160181
def cos(z)
161182
begin
162183
if z.real?
@@ -172,6 +193,8 @@ def cos(z)
172193

173194
##
174195
# returns the tangent of +z+, where +z+ is given in radians
196+
#
197+
# CMath.tan(1 + 1i) #=> (0.27175258531951174+1.0839233273386943i)
175198
def tan(z)
176199
begin
177200
if z.real?
@@ -186,6 +209,8 @@ def tan(z)
186209

187210
##
188211
# returns the hyperbolic sine of +z+, where +z+ is given in radians
212+
#
213+
# CMath.sinh(1 + 1i) #=> (0.6349639147847361+1.2984575814159773i)
189214
def sinh(z)
190215
begin
191216
if z.real?
@@ -201,6 +226,8 @@ def sinh(z)
201226

202227
##
203228
# returns the hyperbolic cosine of +z+, where +z+ is given in radians
229+
#
230+
# CMath.cosh(1 + 1i) #=> (0.8337300251311491+0.9888977057628651i)
204231
def cosh(z)
205232
begin
206233
if z.real?
@@ -216,6 +243,8 @@ def cosh(z)
216243

217244
##
218245
# returns the hyperbolic tangent of +z+, where +z+ is given in radians
246+
#
247+
# CMath.tanh(1 + 1i) #=> (1.0839233273386943+0.27175258531951174i)
219248
def tanh(z)
220249
begin
221250
if z.real?
@@ -230,6 +259,8 @@ def tanh(z)
230259

231260
##
232261
# returns the arc sine of +z+
262+
#
263+
# CMath.asin(1 + 1i) #=> (0.6662394324925153+1.0612750619050355i)
233264
def asin(z)
234265
begin
235266
if z.real? and z >= -1 and z <= 1
@@ -244,6 +275,8 @@ def asin(z)
244275

245276
##
246277
# returns the arc cosine of +z+
278+
#
279+
# CMath.acos(1 + 1i) #=> (0.9045568943023813-1.0612750619050357i)
247280
def acos(z)
248281
begin
249282
if z.real? and z >= -1 and z <= 1
@@ -258,6 +291,8 @@ def acos(z)
258291

259292
##
260293
# returns the arc tangent of +z+
294+
#
295+
# CMath.atan(1 + 1i) #=> (1.0172219678978514+0.4023594781085251i)
261296
def atan(z)
262297
begin
263298
if z.real?
@@ -273,6 +308,8 @@ def atan(z)
273308
##
274309
# returns the arc tangent of +y+ divided by +x+ using the signs of +y+ and
275310
# +x+ to determine the quadrant
311+
#
312+
# CMath.atan2(1 + 1i, 0) #=> (1.5707963267948966+0.0i)
276313
def atan2(y,x)
277314
begin
278315
if y.real? and x.real?
@@ -287,6 +324,8 @@ def atan2(y,x)
287324

288325
##
289326
# returns the inverse hyperbolic sine of +z+
327+
#
328+
# CMath.asinh(1 + 1i) #=> (1.0612750619050357+0.6662394324925153i)
290329
def asinh(z)
291330
begin
292331
if z.real?
@@ -301,6 +340,8 @@ def asinh(z)
301340

302341
##
303342
# returns the inverse hyperbolic cosine of +z+
343+
#
344+
# CMath.acosh(1 + 1i) #=> (1.0612750619050357+0.9045568943023813i)
304345
def acosh(z)
305346
begin
306347
if z.real? and z >= 1
@@ -315,6 +356,8 @@ def acosh(z)
315356

316357
##
317358
# returns the inverse hyperbolic tangent of +z+
359+
#
360+
# CMath.atanh(1 + 1i) #=> (0.4023594781085251+1.0172219678978514i)
318361
def atanh(z)
319362
begin
320363
if z.real? and z >= -1 and z <= 1
@@ -388,4 +431,4 @@ def handle_no_method_error # :nodoc:
388431
end
389432
module_function :handle_no_method_error
390433

391-
end
434+
end

0 commit comments

Comments
 (0)
0