Skip to content

Instantly share code, notes, and snippets.

@marionette-of-u
Created December 29, 2012 07:04
Show Gist options
  • Select an option

  • Save marionette-of-u/4405135 to your computer and use it in GitHub Desktop.

Select an option

Save marionette-of-u/4405135 to your computer and use it in GitHub Desktop.
多倍精度浮動小数点数型 mpf_class において対数関数 ln(x) を求める
void mpf_ln(mpf_class &r, const mpf_class &x){
mp_bitcnt_t prec = x.get_prec();
if(x > 0.5){
mpf_class term = 1.0 - (1.0 / x), term_k = term, last = r;
r = term_k;
term_k *= term;
r += term_k / 2.0;
for(std::size_t i = 3; !mpf_eq(r.get_mpf_t(), last.get_mpf_t(), prec); ++i){
last = r;
term_k *= term;
r += term_k / i;
}
}else if(x > 0.0){
mpf_class term = x - 1.0, term_k = term, factor = 1.0, last = r;
r = term;
int sign = -1;
term_k *= term;
r += -term_k / 2.0;
for(std::size_t i = 3; !mpf_eq(r.get_mpf_t(), last.get_mpf_t(), prec); ++i){
last = r;
term_k *= term;
sign *= -1;
r += term_k * sign / i;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment