Ruby Math.radians

I need the Math.radians () function and cannot find it.

radians=(angle/180)* Math::PI
+3
source share
3 answers

You probably want to create a file called "mymath.rb" in the lib / directory and a Math patch, like this:

require 'mathn'
module Math
  def self.to_rad angle
    angle/180 * Math::PI
  end
end

or you can do what @MBO said in your comment. The link seems inaccessible, but the Google archives give this little small suggestion that indicates a solution that may be cleaner than mine (although I prefer to keep the math material inside math):

The simplest solution is to define a conversion method in Numeric that converts the number of degrees to radians.

, Ruby 2.0 "Refinement", . ( :

module RadiansConversion
  refine Math do
    def to_rad angle
      angle/180 * Math::PI
    end
  end
end

... - .

module MyApp
  using RadiansConversion

  p Math.to_rad 180   #=> 3.14159265358979
  p Math.to_rad 235   #=> 4.10152374218667
end
+10

Float, 'to_rad':

class Float
 def to_rad
   self/180 * Math::PI
  end
end

radian=angle.to_rad
0

EDIT (- ): ! , , , .

, :

x * (Math::PI / 180)

, .

# Geocoder::Calculations.to_radians(@geocode)
def to_radians(*args)
  args = args.first if args.first.is_a?(Array)
  if args.size == 1
    args.first * (Math::PI / 180)
  else
    args.map{ |i| to_radians(i) }
  end
end

. ( ) , .

http://rubydoc.info/github/alexreisner/geocoder/master/Geocoder/Calculations#to_radians-instance_method

-1

Source: https://habr.com/ru/post/1734208/


All Articles