首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果我知道EMI,本金(P)和时间(N),我如何从EMI公式计算利率(R)?

如果我知道EMI,本金(P)和时间(N),我如何从EMI公式计算利率(R)?
EN

Stack Overflow用户
提问于 2018-05-31 22:10:03
回答 1查看 288关注 0票数 0

大家好,如果我知道EMI,本金(P)和利率(R),我如何从EMI公式中计算任期(N)?

代码语言:javascript
运行
复制
Example:
P=50000;
EMI=4368;
N(month)=12

汇率(R)应该是多少?

EN

回答 1

Stack Overflow用户

发布于 2018-06-01 00:56:57

也许有一些方法可以用对数来做,不知道

代码语言:javascript
运行
复制
#include <iostream>
#include <cmath>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>

#define TOLERANCE 0.0000001

using namespace std;
using namespace boost::posix_time;

bool active(true);

// just adjust the calc if this isn't the right formula
//
//     EMI = [P x R x (1+R)^N] / [(1+R)^N-1]
//
double calc(double p, double r, double n) {
    return  p * r * pow(r + 1.0, n) / (pow(r + 1.0, n) - 1.0);
}

void timeout_loop() {
    ptime start = microsec_clock::local_time();
    time_duration timeout(seconds(10));

    while(active) {
        boost::this_thread::sleep(milliseconds(1));

        if((microsec_clock::local_time() - start).total_milliseconds() > timeout.total_milliseconds()) {
            cout << "timeout" << endl;
            active = false;
        }
    }
}

int main() {
    double p(50000.0);
    double target(4368.0);
    double n(12.0);

    double min(0.0);
    double max(1.0);
    double current = (max + min) / 2.0;

    boost::thread timeout_thread(timeout_loop);

    while(active) {
        double emi = calc(p, current, n);

        if((target - emi) > TOLERANCE) {
            min = current;
            current = (max + min) / 2.0;
        } else if((emi - target) > TOLERANCE) {
            max = current;
            current = (max + min) / 2.0;
        } else {
            cout << setprecision(12) << current << endl;
            break;
        }
    }

    active = false;

    timeout_thread.join();

    return 0;
}

0.00733556627529

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50626325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档