#浮点数 #十进制数 #大十进制 #十进制 #任意精度

无std dashu-float

支持任意精度、任意基数和任意舍入模式的巨型浮点数库

9个版本

0.4.3 2024年2月3日
0.4.1 2023年12月17日
0.4.0 2023年9月12日
0.3.2 2023年3月4日
0.3.0 2022年11月10日

#567 in 数学

Download history 1066/week @ 2024-04-08 807/week @ 2024-04-15 784/week @ 2024-04-22 892/week @ 2024-04-29 302/week @ 2024-05-06 343/week @ 2024-05-13 301/week @ 2024-05-20 347/week @ 2024-05-27 337/week @ 2024-06-03 305/week @ 2024-06-10 430/week @ 2024-06-17 337/week @ 2024-06-24 470/week @ 2024-07-01 455/week @ 2024-07-08 385/week @ 2024-07-15 431/week @ 2024-07-22

1,777 每月下载量
19 个crate中(直接使用4个) 使用

MIT/Apache

1MB
20K SLoC

dashu-float

作为dashu库的一部分,实现任意精度的浮点数。有关完整文档,请参阅Docs.rs

功能

  • 支持no_std,并使用纯Rust编写。
  • 支持任意基数任意舍入模式
  • 支持高效的基数转换
  • 小浮点数在栈上内联。
  • 以2~36为基数高效地进行浮点数的解析和打印。
  • 支持C++使用的十六进制浮点数格式。
  • 为浮点数提供开发者友好的调试打印。

可选依赖

  • std(默认):为依赖项启用std支持。

性能

将在内置基准测试中实现相关基准。

许可证

请参阅顶级README


lib.rs:

支持任意精度、任意基数和任意舍入模式的巨型浮点数库。

该库使用纯Rust实现高效的大浮点数算术。

主要类型是[FBig],表示任意精度的浮点数,[DBig]类型是支持十进制浮点数的别名。

要从字面量构建大浮点数,请使用dashu-macro crate以提高您的便利性。

示例

use core::str::FromStr;
use core::convert::TryFrom;
use dashu_float::DBig;

// due to the limit of rust generics, the default float type
// need to be instantiate explicitly
type FBig = dashu_float::FBig;

let a = FBig::try_from(-12.34_f32).unwrap();
let b = DBig::from_str("6.022e23")?;
let c = DBig::from_parts(271828.into(), -5);
let d: DBig = "-0.0123456789".parse()?;
let e = 2 * b.ln() + DBig::ONE;
let f = &c * d.powi(10.into()) / 7;

assert_eq!(a.precision(), 24); // IEEE 754 single has 24 significant bits
assert_eq!(b.precision(), 4); // 4 decimal digits

assert!(b > c); // comparison is limited in the same base
assert!(a.to_decimal().value() < d);
assert_eq!(c.to_string(), "2.71828");

// use associated functions of the context to get full result
use dashu_base::Approximation::*;
use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
let ctxt = Context::<HalfAway>::new(6);
assert_eq!(ctxt.exp(DBig::ONE.repr()), Inexact(c, NoOp));

可选依赖

  • std(默认):为依赖项启用std

依赖项

~0.3–1.7MB
~36K SLoC