5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rustで多倍長演算

Posted at

Rugという多倍長演算ライブラリがあったのでRumpの例題をやってみます

use rug::{ops::Pow, Float};

fn f200(val: f64) -> Float {
    Float::with_val(200, val)
}

fn f(a: Float, b: Float) -> Float {
    let a2 = a.clone().pow(2);
    let b2 = b.clone().pow(2);
    let b4 = b.clone().pow(4);
    let b6 = b.clone().pow(6);
    let b8 = b.clone().pow(8);
    (f200(333.75) - &a2) * &b6
        + &a2 * (f200(11.0) * &a2 * &b2 - f200(121.0) * &b4 - f200(2.0))
        + f200(5.5) * &b8
        + a / (f200(2.0) * b)
}

fn main() {
    println!("{:e}", f(f200(77617.0), f200(33096.0)));
}

rug::FloatCopyが無いのでちょっと面倒になりましたが概ね雑にこんな感じだと思います。

Rug is a high-level interface to the following GNU libraries:

  • GMP for integers and rational numbers,
  • MPFR for floating-point numbers, and
  • MPC for complex numbers.

と言ってるのでGNU MPFRのラッパーですね。整数はnum-bigintがRust実装ですが、浮動小数点数は見当たらないですね(たいして探して無いのであったら教えてください)。

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?