Jump to content

log2


deoiub

Recommended Posts

I'm attempting to create a PHP function that will calculate a loan's interest rate based on knowing $total_loan_amount, $total_number_of_payments, and $payment_amount.

 

I have found a website with the a JS calculator for this, and includes the function I need to use.

 

https://www.easycalculation.com/mortgage/interest-rate.php

 

However, I'm having a difficult time trying to figure out how to translate this to PHP.   I'm getting hung up on calculating q, and specifically the 'divide by log2' portion.

 

Anyone have any suggestions on how to write the q = formula in PHP?

 

Cheers 

Link to comment
Share on other sites

  • 1 year later...

Looking at the JavaScript version of the calculation:

function calc()
{
  var de = document.getElementById("l").value ;
  var nu = document.getElementById("n").value ;
  var pay = document.getElementById("p").value ;
  
  var n = nu * 12;
  var q1 = Math.log(1 + parseFloat(1/n));
  var q2 = Math.log(2);
  var q = parseFloat(q1/q2);
  
  var amt;
  var amt1 = Math.pow((1 + parseFloat(pay/de)),1/q).toFixed(4);
  var amt2 = parseFloat( Math.pow(amt1-1,q)).toFixed(4);
  
  amt = parseFloat(1200 * (amt2 - 1)).toFixed(2); 
  
  document.getElementById("op").value  = amt;  
}

Try something like this to get started:

function calc($total_loan_amount, $total_number_of_payments, $payment_amount)
{
  $p = $payment_amount;
  $a = $total_loan_amount;
  $n = $total_number_of_payments;
  
  $q = log(1 + (1/$n)) / log(2);
  
  $i = pow(pow(1 + ($p/$a), 1/$q) - 1, $q) - 1;
  
  return $i;
}

Absolutely no attention paid to data types, decimals, etc for the numbers.

Edited by charlieholder
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.