Skip to content

Exonio.rate never returns on certain input with BigDecimal #47

@btalayeminaei

Description

@btalayeminaei

Hello!

I'm not sure if this is a bug or if we are misusing the rate method. The method does not return when the number of payments (nper) is 1 and the amount per period (pmt) and the present value (pv) are passed in as BigDecimal (e.g. Exonio.rate(1, 100.0.to_d, -100.0.to_d)). I see the arguments are annotated as Floats but we have been we passing in BigDecimal for a few months and just now hit this edge-case.

Is there truly an issue with passing in BigDecimal? Could this also happen on other inputs since there is no circuit breaker on the number of times the method iterates to arrive at a value within the tolerance? We use this method in a financial context so we care about being precise and that's why we have opted to pass in BigDecimals.

I would appreciate your thoughts on this issue. Thanks!

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "exonio"
  gem "minitest"
end

require "exonio"
require "minitest/autorun"

class Test < Minitest::Test
  def test_rate_with_float
    assert_equal Exonio.rate(1, 100.0, -100.0), 2.80332e-15
  end

  def test_rate_with_big_deicmal
    assert_equal Exonio.rate(1, 100.0.to_d, -100.0.to_d), 2.80332e-15 # never returns
  end
end

ruby 3.3.7 (2025-01-15 revision be31f993d7) [arm64-darwin24]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions