Class: Xirr::RtSafeC

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/xirr/rtsafe_c.rb

Overview

C-backed rtsafe. Same algorithm and results as RtSafe, run in a native extension. Only usable when NATIVE is true (the extension was compiled); otherwise the gem runs the pure-Ruby RtSafe instead.

Instance Attribute Summary

Attributes included from Base

#cf

Instance Method Summary collapse

Methods included from Base

#initialize, #periods_from_start, #xnpv, #xnpv_derivative

Instance Method Details

#xirr(guess, options) ⇒ Float?

Parameters:

  • guess (Float, nil)
  • options (Hash)

    reads :iteration_limit

Returns:

  • (Float, nil)


21
22
23
24
25
26
27
28
29
30
# File 'lib/xirr/rtsafe_c.rb', line 21

def xirr(guess, options)
  limit = (options && options[:iteration_limit]) || Xirr.config.iteration_limit
  start = (guess || cf.irr_guess).to_f
  rate = Xirr::Native.rtsafe(flows, start, Xirr.config.eps.to_f, limit)
  return nil if rate.nil?

  # Round before the floor check: a rate just above -1 can round down to it.
  rounded = rate.round(Xirr.config.precision)
  rounded <= -1.0 ? nil : rounded
end