Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
Javascript Function returning wrong calculated value

Javascript

function freq_rate(r) {
  // Step 1 - Equivalent Effective Rate Per Frequency, m, of the Annual Rate, r
  return ((1+r)^(1/12))-1; 
}

function discount_freq_rate(r) {
  // Step 2:  The Discount Version of Step 1
  return freq_rate(r)/(1+freq_rate(r));
}

function fv(r = 0.07,n = 30 ,m = 12) {
  //Apply to a Case Where r = annual interest rate, m = # months and n = # years
  return ((1 + freq_rate(r))^(m*n)-1)/discount_freq_rate(r);
}

I with fv(0.07,30), I am expecting a value of 1176.06485, but I am getting "Infinity" returned. Not sure what's wrong. My calculations are correct in excel with the following formula:

C9  = 1.07^(1/12)-1
C10 =C9/(1+C9)
1176.064858 =((1+C9)^(12*30)-1)/C10
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

because ^ in JS is not the "power" operator, try with this:

function freq_rate(r) {
  // Step 1 - Equivalent Effective Rate Per Frequency, m, of the Annual Rate, r
  return Math.pow(1+r , 1/12)-1; 
}
about 4 years ago · Juan Pablo Isaza Relatório

0

That's because ^ in javaScript and other languages is a logical operator not a mathematical operator . See here->XOR

In javascript you can use Math.pow

function freq_rate(r) {
  // Step 1 - Equivalent Effective Rate Per Frequency, m, of the Annual Rate, r
  return Math.pow(1+r,1/12)-1; 
}

function discount_freq_rate(r) {
  // Step 2:  The Discount Version of Step 1
  return freq_rate(r)/(1+freq_rate(r));
}

function fv(r = 0.07,n = 30 ,m = 12) {
  //Apply to a Case Where r = annual interest rate, m = # months and n = # years
  return Math.pow(1 + freq_rate(r),(m*n)-1)/discount_freq_rate(r);
}

console.log( fv(0.07,30))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda