本帖最后由 slimay 于 2021-5-5 11:40 编辑
FRA.EXE
摘要:浮点数 化 渐进 分数工具。本工具非常适合用于工程近似计算,密码学,钢琴调音等领域。
版本:1.2
-----------------------------------------------------------------------------
用法:(控制台窗口输入命令)
fra.exe [待转化的浮点数]
-----------------------------------------------------------------------------
示例:
fra.exe "3.1415926535"
fra.exe "sqrt(pi)"
fra.exe "sin1+cos1"
fra.exe "lg5"
fra.exe sqrt(5)/2-0.5
-----------------------------------------------------------------------------
补充:
可使用的数学函数包括
常数类
pi 3.1415926535897932
e 2.7182818284590452
通用类
round 四舍五入
int 取整
ceil 向上舍入
floor 向下舍入
abs 绝对值
sqrt 开方
lg 常用对数,以10为底
ln 自然对数
exp e的次幂
torad 度转弧度
+ 加
- 减
* 乘
/ 除
% 取余数
^ 次方
三角函数类
sin、cos、tan
arcsin、arccos、arctan
双曲函数类
sinh、cosh、tanh
arcsinh、arccosh、arctanh
源码发行,请自助编译,不提供成品,以节省论坛空间- #include<stdio.h>
- #include<math.h>
- #include "fra.h"
-
- //逼近次数
- #define N 10
-
- //定义帮助信息
- #define HELP_INFORMATION "\
- Float to fraction tool, Copyright@2021~2023 by Slimay\n\
- usage: fra [Float]\n\
- example: fra lg(pi/2)\n\
- version: 1.2\n"
-
- int main(int argc, char** argv)
- {
- //开关检测
- if
- (
- ( argc == 1 ) ||
- ( argc > 2 ) ||
- ( stricmp(argv[1], "/?") == 0 ) ||
- ( stricmp(argv[1], "/h") == 0 ) ||
- ( stricmp(argv[1], "-h") == 0 ) ||
- ( stricmp(argv[1], "--h") == 0 ) ||
- ( stricmp(argv[1], "--help") == 0 )
- )
- {
- //抛出帮助信息
- fputs(HELP_INFORMATION, stdout);
- exit(1);
- }
-
- double c = atof(argv[1]);
-
- //存数分子
- double p0 = 0.0f;
- double p1 = 1.0f;
- double p;
- //存数分母
- double q0 = 1.0f;
- double q1 = 0.0f;
- double q;
-
- double t = c;
- for (int i = 0; i < N; i++)
- {
- int a = (int) t;
-
- p = a * p1 + p0;
- p0 = p1;
- p1 = p;
-
- q = a * q1 + q0;
- q0 = q1;
- q1 = q;
-
- t = 1.0/(t - a);
-
- //计算误差
- double re = fabs(p/q/c-1.0);
- //误差低于30%才显示
- if(re < 0.1)
- {
- printf
- (
- "%12.0f \n"
- " ------------- =%10.8lf ~=%s; Similarity rate [%10.5lf%%]\n"
- "%12.0f \n\n"
- ,
-
- p, p/q, argv[1], 100.0 * (1.0-re), q
- );
- }
-
- if(re < 1e-15)
- {
- break;
- }
- }
-
- return 0;
- }
复制代码 例如fra 3.1415926535 就得到了最能接近该超越数的分数比值 约率22 / 7,密率355 / 113, 以及 质精率 833719 / 265381 ~=3.14159265358
fra pi的输出结果- 3
- ------------- =3.00000000 ~=pi; Similarity rate [ 95.49297%]
- 1
-
- 22
- ------------- =3.14285714 ~=pi; Similarity rate [ 99.95975%]
- 7
-
- 333
- ------------- =3.14150943 ~=pi; Similarity rate [ 99.99735%]
- 106
-
- 355
- ------------- =3.14159292 ~=pi; Similarity rate [ 99.99999%]
- 113
-
- 103993
- ------------- =3.14159265 ~=pi; Similarity rate [ 100.00000%]
- 33102
-
- 104348
- ------------- =3.14159265 ~=pi; Similarity rate [ 100.00000%]
- 33215
-
- 208341
- ------------- =3.14159265 ~=pi; Similarity rate [ 100.00000%]
- 66317
-
- 312689
- ------------- =3.14159265 ~=pi; Similarity rate [ 100.00000%]
- 99532
-
- 833719
- ------------- =3.14159265 ~=pi; Similarity rate [ 100.00000%]
- 265381
复制代码
|