Ruby/Development/Math

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

Call to the class method sqrt from the Math module

puts Math.sqrt(16)



Convert polar coordinates to Cartesian coordinates

def cartesian(magnitude, angle)
  [magnitude*Math.cos(angle), magnitude*Math.sin(angle)]
end



Convert the Cartesian point (x,y) to polar (magnitude, angle) coordinates

def polar(x,y)
  return Math.hypot(y,x), Math.atan2(y,x)
end



do natural logarithms (base E or Euler) and base-10 logarithms.

Math.log(Math::E) # => 1.0
Math.log(1) # => 0.0
Math.log(0) # => -Infinity
Math.log10(100.0) # => 2.0



Math functions from the Math module

Ruby methods ending in ! mean that the method makes in-place, or destructive, changes to an object, not to a copy of it.
 
Method(s)                                             Description
Math.acos, Math.acos!                                 Arc cosine
Math.acosh, Math.acosh!                               Hyperbolic arc cosine
Math.asin, Math.asin!                                 Arc sine
Math.asinh, Math.asinh                                Hyperbolic arc sine
Math.atan, Math.atan!, Math.atan2, Math.atan2!        Arc tangent; atan takes an x argument; atan2 takes an x and a y argument
Math.atanh, Math.atanh!                               Hyperbolic arc tangent
Math.cos, Math.cos!                                   Cosine
Math.cosh, Math.cosh                                  Hyperbolic cosine
Math.sin, Math.sin!                                   Sine
Math.erf                                              Error function
Match.erfc                                            Complementary error function
Math.exp, Math.exp!                                   Base x of Euler
Math.frexp                                            Normalized fraction and exponent
Math.hypot                                            Hypotenuse
Math.ldexp                                            Floating-point value corresponding to a given mantissa and exponent
Math.sinh, Math.sinh!                                 Hyperbolic sine
Math.sqrt, Math.sqrt!                                 Square root
Math.tan, Math.tan!                                   Tangent
Math.tanh, Math.tanh!                                 Hyperbolic tangent



Random number

rand       # => 0.964395196505186
rand       # => 0.390523655919935
rand(100)  # => 81
rand(100)  # => 32



Random seeds

srand(0)                # Known seed
[rand(100),rand(100)]   # => [44,47]: pseudorandom sequence
srand(0)                # Reset the seed to repeat the sequence
[rand(100),rand(100)]   # => [44,47]



Taking Logarithms

puts Math.log(1)                                    # => 0.0
puts Math.log(Math::E)                              # => 1.0
puts Math.log(10)                                   # => 2.30258509299405
puts Math::E ** Math.log(25)                        # => 25.0
puts Math.log10(1)                                  # => 0.0
puts Math.log10(10)                                 # => 1.0
puts Math.log10(10.1)                               # => 1.00432137378264
puts Math.log10(1000)                               # => 3.0
puts 10 ** Math.log10(25)                           # => 25.0



The Math.exp function returns Euler to the power of x.

Math.exp(1) # => 2.71828182845905
Math.exp(11) # => 59874.1417151978



The Math Object

puts(Math::PI)
puts(Math::E)
puts(Math.cos(Math::PI/3))
puts(Math.tan(Math::PI/4))
puts(Math.log(Math::E**2))
puts((1 + Math.sqrt(5))/2)
# 3.14159265358979
# 2.71828182845905
# 0.5
# 1.0
# 2.0
# 1.61803398874989