RSpec 3 has BigDecimal eq:
x.should eq(y) expect(x).to eq(y)
If you are comparing BigDecimal with Float, keep in mind that accuracy can affect the comparison.
You can use this:
x.should be_within(delta).of(y) expect x.to be_within(delta).of(y)
If you are comparing two BigDecimal numbers that have different prefixes, keep in mind that these numbers may show different results from inspect , as well as from hash , depending on your platform and which version of Ruby BigDecimal you are running.
For example, this could happen:
BigDecimal.new("2").hash == BigDecimal.new("2.0").hash => false
Your result shows that your views of BigDecimal strings are slightly different.
Here is what your lines mean:
- Part 1 is the address of the object.
- Part 2 is a numeric value represented as a string.
- Part 3 is the number of significant digits, and then the maximum number of significant digits.
Your conclusion shows that your lines have different addresses of objects, the same values ββof numbers (i.e., part 2), identical significant digits (the first number in part 3), but different numbers of maximum significant digits.
For your question, using RSpec 2 and comparing BigDecimal hashes, you will solve your problem by matching rspec be_within .
Note that the Ruby BigDecimal and Float numbers are floating point numbers:
You can see the BigDecimal floating point decimal by doing the following:
require 'bigdecimal' x=BigDecimal.new(100) =>
A significant part is β0.1β and the exponential part is βE3β.
Note that this is a typical Ruby MRI / KRI VM. Implementations may differ from other Ruby VMs, such as JRuby, because Java has its own bignum code.
When you compare two different types of floating point numbers, such as BigDecimal and Float, you may get results that may seem contradictory, because the types use different bases (decimal or binary), different prefixes and different Ruby classes.
Example:
BigDecimal.new("1.111111111111111") === 1.111111111111111 => true BigDecimal.new("1.1111111111111111") === 1.1111111111111111 => false