it “should get right settlement percent” do
contract = Contract.new
contract.settlement_percent = 1.1 / 100.0
contract.settlement_percent.to_f.should == 0.011
contract.settlement_percent.to_s.should == “0.011”
end
it "should get right settlement percent" do
contract = Contract.new
contract.settlement_percent = 1.1 / 100.0
contract.settlement_percent.to_f.should == 0.011
contract.settlement_percent.to_s.should == "0.011"
end
Your model of floating point is incorrect. Exact equality with floating point
is rarely a good idea. Most modern computers use the IEEE floating point
format and arithmetic is done in base 2. There are many base 10 fractions
that do not have exact base 2 representations. And once you start doing
calculations, they diverge even further.
There are several ways around this. The simplest is:
If you have legal requirements (e.g. SEC regulations) on how arithmetic works,
you had better write your own class. Depending on floating point on arbitrary
architectures to behave in a certain way is inviting trouble.
Your model of floating point is incorrect. Exact equality with floating
point
is rarely a good idea. Most modern computers use the IEEE floating
point
format and arithmetic is done in base 2. There are many base 10
fractions
that do not have exact base 2 representations. And once you start doing
calculations, they diverge even further.
There are several ways around this. The simplest is: