Sha256: 11921b421f336dc60739b60300a7169be9d0d4707dc56954c499bce2d13a783c

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

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

describe MPFR, "when rounding number by prec_round" do
  before(:all) do
    MPFR.set_default_prec(200)
    @prec = 100
    @args = GenerateNumber.float(1000).map{ |a| MPFR.new(a.to_s) }
  end

  it "should be rounded up" do
    @args.each do |a|
      a.prec_round(MPFR::RNDU, @prec).should >= a
    end
  end

  it "should be rounded down" do
    @args.each do |a|
      a.prec_round(MPFR::RNDD, @prec).should <= a
    end
  end

  it "should be rounded to zero" do
    @args.each do |a|
      if a > 0
        a.prec_round(MPFR::RNDZ, @prec).should <= a
      else
        a.prec_round(MPFR::RNDZ, @prec).should >= a 
      end
    end
  end

  it "should be rounded to near number" do
    @args.each do |a|
      min = [(a - a.prec_round(MPFR::RNDU, @prec)).abs, (a - a.prec_round(MPFR::RNDD, @prec)).abs].min
      (a - a.prec_round(MPFR::RNDN, @prec)).abs.should eql min
    end
  end

  it "should be rounded by prec_round!" do
    @args.each do |a|
      [MPFR::RNDU, MPFR::RNDD, MPFR::RNDN, MPFR::RNDZ, MPFR::RNDA].each do |rnd|
        b = a.dup
        old_object_id = b.object_id
        b.prec_round!(rnd, @prec)
        b.should eql a.prec_round(rnd, @prec)
        b.object_id.should eql old_object_id
      end
    end
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby-mpfr-0.0.17 spec/mpfr/rounding_spec.rb
ruby-mpfr-0.0.16 spec/mpfr/rounding_spec.rb
ruby-mpfr-0.0.15 spec/mpfr/rounding_spec.rb