数值运算

  def squared(a, b)
    b = b.to_a
    a = a.to_a
    sum = 0
    (0...a.size).each { |i| sum += ((a[i].to_f - b[i].to_f) ** 2) }
    sum
  end

  def squared_root(x, y)
    Math.sqrt(squared(x, y))
  end

 

具体示例:

 

    p squared(15, 10) # => 25.0
    p squared_root(15, 10) # => 5.0

 

你可能感兴趣的:(F#)