整数或浮点在情况下,当两者都会做? [英] integers or floating point in situations when either would do?

查看:147
本文介绍了整数或浮点在情况下,当两者都会做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将整数和浮点数的相对优点的讨论移动到一个单独的问题。这里是:在整数类型或浮点类型之间的偏好是什么在既不是固有的整数也不是固有浮点的情况下?例如,当为一个良好控制范围的尺度开发几何引擎时,您将喜欢整数坐标在最小可行单位或浮点/双精度坐标?

Moving a discussion on relative merits of integers and floats into a separate question. Here it is: what is your preference between an integer type or a floating point type in situations that are neither inherently integral nor inherently floating point? For example, when developing geometric engine for a well-conntrolled range of scales would you prefer integer coordinates in the smallest feasible units or float/double coordinates?

推荐答案

更喜欢浮点数的一些原因是:

Some reasons to prefer floating-point are:


  • 当以定点格式乘法时,一个新的比例,所以它必须调整或代码必须写入改变比例的帐户。例如,如果采用缩放100的格式,那么.3用30表示,而.4用40表示,则30乘以40产生1200,但是相同尺度的正确答案应为12(表示.12 )。 Division需要类似的调整。

  • 当整数格式溢出时,许多机器和编程语言不能很好地支持获取结果的最重要部分。

  • 整数运算通常会舍去小数,但是浮点数舍入(除非另有要求)。

  • 一些计算涉及大范围的数字,包括非常大和非常小的数字。定点格式具有小范围,但是浮点格式具有大范围。

  • 许多机器和/或编程语言忽略整数溢出,但是可以使用定点格式手动跟踪比例,但是您只是使用整数实现自己的浮点。浮点可以妥善处理这些事件和/或在事件发生时提供通知。

  • 浮点运算是定义良好的, bug已经减少了(有时通过痛苦的经验)。

  • 对于某些函数,很难预先预测结果的规模,因此使用固定长度的代码是很尴尬的,点格式。例如,考虑正弦。每当输入接近π的倍数时,正弦接近零。因为π是不合理的(和超越的),整数或定点数的模式接近π的倍数是非常不规则的。一些定点数字不是π的接近倍数,它们的正弦约为0.1,.5,.9等。一些定点数是π的非常接近的倍数,并且它们的正弦接近于零。有些是非常接近π的倍数,他们的正弦很小。因此,没有合理精度的定点格式,它总是可以返回正弦的结果,而不会出现下溢或溢出。

  • When you multiply in a fixed-point format, the product has a new scale, so it must be adjusted or the code must be written to account for the changed scale. For example, if you adopt a format scaled by 100, so that .3 is represented with 30 and .4 is represented with 40, then multiplying 30 by 40 produces 1200, but correct answer at the same scale should be 12 (representing .12). Division needs similar adjustment.
  • When the integer format overflows, many machines and programming languages do not have good support for getting the most significant portion of the result. Floating-point automatically produces the most significant portion of the result and rounds the discarded bits.
  • Integer arithmetic usually truncates fractions, but floating-point rounds them (unless requested otherwise).
  • Some calculations involve a large range of numbers, including both numbers that are very large and very small. A fixed-point format has a small range, but a floating-point format has a large range. You could manually track the scale with a fixed-point format, but then you are merely implementing your own floating-point using integers.
  • Many machines and/or programming languages ignore integer overflow, but floating-point can handle these gracefully and/or provide notifications when they occur.
  • Floating-point arithmetic is well defined and generally well implemented; bugs in it have been reduced (sometimes by painful experience). Building new do-it-yourself arithmetic is prone to bugs.
  • For some functions, it is difficult to predict the scale of the result in advance, so it is awkward to use a fixed-point format. For example, consider sine. Whenever the input is near a multiple of π, sine is near zero. Because π is irrational (and transcendental), the pattern of which integers or fixed-point numbers are near multiples of π is very irregular. Some fixed-point numbers are not near multiples of π, and their sines are around .1, .5, .9, et cetera. Some fixed-point numbers are very near multiples of π, and their sines are close to zero. A few are very close to multiples of π, and their sines are tiny. Because of this, there is no fixed-point format of reasonable precision that can always return the result of sine without either underflowing or overflowing.


  • 整数算术可能更快,或者在特定硬件上具有更高的吞吐量。

  • 对于相同数量的位,整数算术提供更高的精度。

  • 在某些语言实现中,某些对整数算术的支持可能更好。例如,默认设置或具有高精度设置的低质量软件可能会错误地显示浮点值,但是软件很少会不正确地打印整数值。

我考虑了使用整数算术的某些特征作为使用它的原因,但是,经检查,它们不是实际的特征:

I considered ways to list certain "features" of integer arithmetic as reasons to use it, but, upon examination, they are not actual features:


  • 可能会说整数算术是精确的,直到它溢出。但这是错误的,因为整数算术或定点算术(具有尺度的整数算术)不是精确的。以年利率计算每月利息通常是不准确的。货币之间的转换不完全。物理计算不精确。

  • 在整数运算完全溢出之前,它不是一个特征。大多数机器允许整数算术溢出而没有警告。因此,当整数算术失败时,它会失败。 (使用IEEE 754浮点数,您可以设计精确的算术,如果发生不准确,请求陷阱或标记。)

这篇关于整数或浮点在情况下,当两者都会做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆