Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dynamic rate step bracketing interval #7

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
## 1.0.2

- Fixed: The `find_bracketing_interval` function now supports extremely high rates, improving precision for very high XIRRs (over 1E300%). Dynamic step sizing ensures accurate results even at extreme rates.

## 1.0.1

- Fix: Resolved an issue where calculating XIRR with an empty cash flow array returned 10. It now correctly returns 0.
- Fixed: Resolved an issue where calculating XIRR with an empty cash flow array returned 10. It now correctly returns 0.

## 1.0.0 / 2024-07-03

### Initial Release
### Initial Release

5 changes: 2 additions & 3 deletions ext/fast_xirr/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@ int find_bracketing_interval(CashFlow *cashflows, long long count, double *low,
}

// Extend the search range if no interval is found within the initial range
step = 10.0;
for (double rate = max_rate + step; rate <= 10000.0; rate += step) {
for (double rate = max_rate; rate <= 1e300; rate *= 1.5) {
double npv_rate = npv(rate, cashflows, count, min_date);

// Check if the function values at consecutive rates have opposite signs
if (npv_min_rate * npv_rate < 0) {
*low = rate - step;
*low = rate / 1.5;
*high = rate;
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions spec/use_cases_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
expect(result).to be_within(1e-6).of(22.35220616417055)
end

it 'returns NaN for a good investment that cannot be annualized' do
it 'calculates xirr for an extremely good and unrealistic investment' do
cashflows = [
[-1000, Date.new(1985, 1, 1)],
[6000, Date.new(1985, 1, 2)]
]

result = FastXirr.calculate(cashflows: cashflows)
expect(result.nan?).to be true
expect(result).to be_within(1e-6).of(1.0597571969623571e+284)
end

it 'returns NaN for all negative cashflows' do
Expand Down
Loading