如何在 Forth 中将两个整数相除并得到浮点数结果?
我正在寻找一种能够在单精度和双精度之间转换的方法。 一个示例是将 2 个整数相除并得到浮点结果。这怎么可能?
I am looking for a way to be able to translate between single precision and double precision.
One example would be to divide 2 integers and get a floating result. How is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另请参阅:http://forth.sourceforge.net/std/dpans/dpans12.htm
see also: http://forth.sourceforge.net/std/dpans/dpans12.htm
注意。 rvm 的答案依赖于一个单独的浮点堆栈,尽管标准没有保证,但这是非常常见的。在 kForth 上,一个具有统一浮点堆栈和两单元浮点的系统,您可以编写
,这仍然不是标准的:它依赖于有关堆栈上 FP 数字有多宽的知识。这里有两种可移植的方法:
尽管您最好的选择是仅声明环境依赖性并继续您的方式。一个单独的浮点堆栈确实很有帮助,几乎所有东西都是这样做的,如果您想将代码移植到为您提供统一 FP 的非常罕见的系统之一,那么一个简短的前奏可以在软件中实现 FP 堆栈堆。
除此之外,这是您问题的第二个答案:您不需要浮点数来处理小数值(在某些情况下,例如金钱,您不仅不需要它们,而且不想要它们)他们)。 Forth 使您可以轻松使用带有缩放运算符和自定义数字输出的固定精度数字。
自定义数字输出的示例:
缩放除法的示例:(
请注意,
n1 n2 n3 */
比n1 n2 * n3 /
更精确 - 请阅读规范。 )关于
$
这个词的两个注释。首先,它对接受带有 的双精度数字的系统具有环境依赖性。在其中的任何地方,而不仅仅是在最后。其次,它接受带有 的数字。 任何地方,即使在最后,但数字仍然被解释为有两个小数位 - 例如,$ 4235。
被解释为 42 美元和 35 美分,而不是四千二百三十五美元。所以,它很草率,但你可以写一个更严格、更可移植的解析词。您可能会说,“这听起来像是额外的工作,浮点数不需要那么多的程序员纪律。”好吧,在您让这种印象成为化石之前,请阅读每台计算机都有什么科学必须了解浮点算术:-)
(对于sheepez 的问题,没有一个严肃的 ANS Forth 系统不支持浮点数。您可能需要加载 FP 设施,但它们会在那里 - 并且“可加载”并不意味着“附加”或“效率较低”。)
NB. rvm's answer relies on a separate floating-point stack, which - although not guaranteed by the standard - is extremely common. On kForth, a system with a unified floating-point stack and two-cell floats, you might write
, which is still not standard: it relies on knowledge about how wide FP numbers are on the stack. Here are two portable methods:
Although your best option is to just declare the environmental dependency and go on your way. A separate floating-point stack really helps, just about everything does it that way, and a brief prelude can implement an FP stack in software should you ever want to port your code to one of the very rare systems that presents you with a unified FP stack.
All of that aside, here is a second answer to your question: you don't need floating-point numbers to deal in fractional values (and in some cases, like money, you not only don't need them but don't want them). Forth makes it very easy for you to use fixed-precision numbers with scaling operators and custom numerical output.
An example of custom numerical output:
An example of scaled division:
(Note that
n1 n2 n3 */
is more precise thann1 n2 * n3 /
-- read the spec.)Two notes about that
$
word. First, it has an environmental dependency on the system accepting as a double a number with a . anywhere in it, rather than just at the end. Second, it accepts numerals with a . anywhere in them, even at the end, but the numbers are still interpreted to have two fractional digits -- e.g.,$ 4235.
is interpreted as 42 dollars and 35 cents, and not four-thousand two-hundred thirty-five dollars. So, it's sloppy, but you can write a parsing word that's both stricter and more portable.You might say, "that sounds like extra work, floating-point numbers don't demand as much programmer discipline." Well, before you let that impression fossilize, read What Every Computer Science Must Know About Floating-Point Arithmetic :-)
(To sheepez's question, there's no remotely serious ANS Forth system that doesn't have floating-point number support. You may need to load the FP facilities, but they'll be there - and 'loadable' doesn't mean 'tacked on' or 'less efficient'. Consult your Forth system's docs.)