Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7762855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:30:58+00:00 2026-06-01T14:30:58+00:00

I have this code: http://pastebin.com/Sd9WKZFr When i call something like rate(60, -6000, 120000) it

  • 0

I have this code: http://pastebin.com/Sd9WKZFr

When i call something like rate(60, -6000, 120000) it returns me a NAN result, but the same function on MS Excel returns me 0,04678.... I have the same problem trying -5000, -4000, -3000 and -2000.

When i debug the code, i see that about the 8/9 iteration, the line number 29 begins to return a NAN result, making all of other results to turn NAN too.

BUT, when i call something like rate(60, -1000, 120000) it returns me a float -0.02044..., exactly the same result of MS Excel.

I have already tryed to convert all of math calculations into BCMath functions, but this way the results of -6000 is wrong (-1.0427… instead of 0,04678…) but using -1000 the result is correct, matching excel’s result.

Is there a way to make it work correctly?

Thanks in advance for any useful sight about that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:31:00+00:00Added an answer on June 1, 2026 at 2:31 pm

    I’ll need to do some tests to ensure that there’s no adverse effects in other situations; but the following looks as though it might fix this problem, and certainly calculates the correct rate value for your arguments RATE(60, -6000, 120000) stabilises at 0.046781916422493 in iteration 15.

    define('FINANCIAL_MAX_ITERATIONS', 128); 
    define('FINANCIAL_PRECISION', 1.0e-08); 
    
    
    function RATE($nper, $pmt, $pv, $fv = 0.0, $type = 0, $guess = 0.1) { 
    
        $rate = $guess; 
        if (abs($rate) < FINANCIAL_PRECISION) { 
            $y = $pv * (1 + $nper * $rate) + $pmt * (1 + $rate * $type) * $nper + $fv; 
        } else { 
            $f = exp($nper * log(1 + $rate)); 
            $y = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv; 
        } 
        $y0 = $pv + $pmt * $nper + $fv; 
        $y1 = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv; 
    
        // find root by secant method 
        $i  = $x0 = 0.0; 
        $x1 = $rate; 
        while ((abs($y0 - $y1) > FINANCIAL_PRECISION) && ($i < FINANCIAL_MAX_ITERATIONS)) { 
            $rate = ($y1 * $x0 - $y0 * $x1) / ($y1 - $y0); 
            $x0 = $x1; 
            $x1 = $rate;
            if (($nper * abs($pmt)) > ($pv - $fv))
                $x1 = abs($x1);
    
            if (abs($rate) < FINANCIAL_PRECISION) { 
                $y = $pv * (1 + $nper * $rate) + $pmt * (1 + $rate * $type) * $nper + $fv; 
            } else { 
                $f = exp($nper * log(1 + $rate)); 
                $y = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv; 
            } 
    
            $y0 = $y1; 
            $y1 = $y; 
            ++$i; 
        } 
        return $rate; 
    }   //  function RATE() 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code require 'mechanize' @agent = Mechanize.new page = @agent.get('http://something.com/?page=1') next_page =
I have simple front controller plugin which contains this code: http://pastebin.com/m155c59b0 When session expire
I have this code http://pastebin.com/hCFk0FJg Server starts and listens normally, but then when I
I have this code: function submitTwice(f){ f.action = 'http://mydomain.com/threevideopage.php'; f.target='name'; f.submit(); } I left
Suppose we have this example: http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html with source code available here: http://code.google.com/p/myandroidwidgets/source/browse/trunk/Phonebook/src/com/abeanie/ How can
I have found this project on Codeplex. http://www.codeplex.com/ProjNET I need to integrate this code
I currently have this piece of code: NSString *strURL = [NSString stringWithFormat:@http://www.sample.com /phpFile.php?firstname=%@,txtfirstName.text]; NSString
I am trying to learn Flex and now i have the next code: http://pastebin.com/rZwxF7w1
I have a table called informations (http://pastebin.com/h3UiUhpx) and also have this associated tables educations,
I have read RSS and get html string like at http://pastebin.com/eWQ6gUe2 which i display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.