# ryu_fortran
A Fortran implementation of [Ryu](https://dl.acm.org/doi/10.1145/3296979.3192369) algorithm which converts floating point numbers to decimal strings. It is more effective than internal file approach.
This implementation is based on the [Scala](https://github.com/scala-native/scala-native/tree/master/nativelib/src/main/scala/scala/scalanative/runtime/ieee754tostring/ryu) version of Ryu.
## Install
### Fortran package manager
Add fpm dependency declaration in the `fpm.toml` file of your project.
```toml
[dependencies]
ryu_fortran = { git = "https://github.com/St-Maxwell/ryu_fortran" }
```
## Usage
```fortran
use ryu, only: f2shortest, d2shortest, d2fixed, d2exp
use iso_fortran_env, only: real32, real64
write (*, "(A)") f2shortest(3.14159_real32)
write (*, "(A)") d2shortest(2.718281828_real64)
write (*, "(A)") d2fixed(1.2345678987654321_real64, 10)
write (*, "(A)") d2exp(299792458._real64, 5)
! 3.14159
! 2.718281828
! 1.2345678988
! 2.99792E+08
```
## Test
Ryu algorithm is meant to generate the shortest decimal representaion of a floating point number, and is able to preserve the information after conversion. That is, if we convert the produced string back to a floating point number, we should obtain the same binary representation comparing to the original number.
`f2shortest`, `d2shortest`, `d2fixed` and `d2exp` have been fully tested using the test cases from [ulfjack/ryu](https://github.com/ulfjack/ryu/tree/master/ryu/tests).
To perform the tests, run
```bash
fpm test [test-item]
```
where `test-item` can be `test-f2shortest`, `test-d2shortest`, `test-d2fixed` and `test-d2exp`.
## Benchmark
* Compiler: gfortran version 9.3.0 (Ubuntu-20.04 WSL2)
* Command: `fpm test benchmark --profile release`
Results
```
Benchmark for f2shortest
f2shortest Time (us): 0.0785894 Std Dev: 0.0118
internal IO Time (us): 0.7998928 Std Dev: 0.0778
Benchmark for d2shortest
d2shortest Time (us): 0.1047708 Std Dev: 0.0110
internal IO Time (us): 0.9942683 Std Dev: 0.1660
Benchmark for d2exp
d2exp Time (us): 0.1041636 Std Dev: 0.0167
internal IO Time (us): 0.9326320 Std Dev: 0.1252
Benchmark for d2fixed
d2fixed Time (us): 0.2314018 Std Dev: 0.2261
internal IO Time (us): 2.3488336 Std Dev: 2.3387
```
没有合适的资源?快使用搜索试试~ 我知道了~
fortran-使用fortran实现的快速浮点数到字符串转换.zip
共21个文件
f90:18个
txt:1个
toml:1个
需积分: 1 0 下载量 120 浏览量
2024-03-09
17:21:32
上传
评论
收藏 147KB ZIP 举报
温馨提示
fortran_使用fortran实现的快速浮点数到字符串转换
资源推荐
资源详情
资源评论
收起资源包目录
fortran_使用fortran实现的快速浮点数到字符串转换.zip (21个子文件)
fortran_使用fortran实现的快速浮点数到字符串转换
src
real64_to_shortest.f90 74KB
ryu_utils.f90 12KB
ryu.f90 189B
real64_to_fixed.f90 8KB
lookup_table.f90 346KB
real32_to_shortest.f90 18KB
real64_to_exp.f90 9KB
app
main.f90 3KB
test
benchmark
random.f90 2KB
benchmark.f90 7KB
bench_option.txt 56B
test_d2fixed
tester.f90 1KB
test_d2fixed.f90 48KB
test_d2shortest
tester.f90 1KB
test_d2shortest.f90 58KB
test_f2shortest
tester.f90 1KB
test_f2shortest.f90 27KB
test_d2exp
tester.f90 1KB
test_d2exp.f90 51KB
fpm.toml 552B
README.md 2KB
共 21 条
- 1
资源评论
DdddJMs__135
- 粉丝: 715
- 资源: 216
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功