gpt4 book ai didi

forth - GForth:将 float 转换为字符串

转载 作者:行者123 更新时间:2023-12-04 18:00:56 29 4
gpt4 key购买 nike

一个简单的问题,结果证明是相当复杂的:

如何在 GForth 中将浮点数转换为字符串?所需的行为看起来像这样:

1.2345e fToString \ takes 1.2345e from the float stack and pushes (addr n) onto the data stack

最佳答案

经过一番挖掘,我的一位同事发现了它:

f>str-rdp ( rf +nr +nd +np -- c-addr nr )

https://www.complang.tuwien.ac.at/forth/gforth/Docs-html-history/0.6.2/Formatted-numeric-output.html

Convert rf into a string at c-addr nr. The conversion rules and the meanings of nr +nd np are the same as for f.rdp.



来自 f.rdp :

f.rdp ( rf +nr +nd +np – )

https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Simple-numeric-output.html

Print float rf formatted. The total width of the output is nr. For fixed-point notation, the number of digits after the decimal point is +nd and the minimum number of significant digits is np. Set-precision has no effect on f.rdp. Fixed-point notation is used if the number of siginicant digits would be at least np and if the number of digits before the decimal point would fit. If fixed-point notation is not used, exponential notation is used, and if that does not fit, asterisks are printed. We recommend using nr>=7 to avoid the risk of numbers not fitting at all. We recommend nr>=np+5 to avoid cases where f.rdp switches to exponential notation because fixed-point notation would have too few significant digits, yet exponential notation offers fewer significant digits. We recommend nr>=nd+2, if you want to have fixed-point notation for some numbers. We recommend np>nr, if you want to have exponential notation for all numbers.



用人类可读的术语来说,这些函数需要浮点堆栈上的一个数字和数据堆栈上的三个数字。

第一个数字参数告诉它字符串应该有多长,第二个告诉它你想要多少小数,第三个告诉它最小的小数位数(大致转换为精度)。执行大量隐式数学运算以确定生成的最终 String 格式,因此几乎需要进行一些修补才能使其按照您想要的方式运行。

测试一下(我们不想重建 f. ,而是要生成一种格式,该格式将被再次接受为 EVALUATE 的浮点数,因此 1.2345E0 表示法是故意的):
PI 18 17 17 f>str-rdp type \ 3.14159265358979E0 ok
PI 18 17 17 f.rdp \ 3.14159265358979E0 ok
PI f. \ 3.14159265358979 ok

关于forth - GForth:将 float 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53642533/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com