Sha256: dd074a015b67368b54e7b22f41e6cb22975de34efd4281cfb417da7d74580b52

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

require File.join(File.dirname(__FILE__), "../spec_helper.rb")

ARITHMETIC_NUM_OF_CALCULATE = 1000

def check_mpc_arithmetic_op(arg1, arg2, error = 10e-10, &block)
  ruby_res = yield(Complex(*(arg1.map{ |a| a.to_f })), Complex(*(arg2.map{ |a| a.to_f })))
  mpc_res = yield(MPC.new(*arg1), MPC.new(*arg2))
  (MPFR.new(ruby_res.real) - mpc_res.real).abs.should < error
  (MPFR.new(ruby_res.imag) - mpc_res.imag).abs.should < error
end

describe MPC, "when calculating" do
  before(:all) do
    @prec_old = MPFR.get_default_prec
    MPFR.set_default_prec(256)
  end

  it "should be complex number" do
    args = GenerateComplex.float_arguments(ARITHMETIC_NUM_OF_CALCULATE)
    args.each_index do |i|
      if i > 0
        check_mpc_arithmetic_op(args[i-1], args[i]){ |a, b| a + b }
        check_mpc_arithmetic_op(args[i-1], args[i]){ |a, b| a - b }
        check_mpc_arithmetic_op(args[i-1], args[i]){ |a, b| a * b }
        check_mpc_arithmetic_op(args[i-1], args[i]){ |a, b| a / b }
      end
    end
  end

  after(:all) do
    MPFR.set_default_prec(@prec_old)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby-mpc-0.0.9 spec/mpc/arithmetic_op_spec.rb
ruby-mpc-0.0.8 spec/mpc/arithmetic_op_spec.rb
ruby-mpc-0.0.7 spec/mpc/arithmetic_op_spec.rb