Unity中的数值问题

来源:互联网 发布:珀莱雅淘宝店真假 编辑:程序博客网 时间:2024/05/21 17:43

1.Mathf.Epsilon
Mathf.Epsilon小正数

static var Epsilon : float

一个很小的浮点数值。(只读)

最小的浮点值,不同于0。

以下规则:

  • anyValue + Epsilon = anyValue

  • anyValue - Epsilon = anyValue

  • 0 + Epsilon = Epsilon

  • 0 - Epsilon = -Epsilon

一个在任意数和Epsilon的之间值将导致在任意数发生截断误差。

public class example : MonoBehaviour{            boolisEqual(float a, float b)    {                        if(a >= b - Mathf.Epsilon && a <= b + Mathf.Epsilon)                                    returntrue;                        else                                    returnfalse;            }}

由于浮点类数值的不精确,低于此的数值都不会有很大作用。所以当使用浮点数时可以将小于等于0的数和此数比较(例如某个数的平方小于Mathf.Epsilon,则可以认为该浮点数的平方为0),用于减少误差。
2.Vector2.sqrMagnitude
ar sqrMagnitude : float

Description描述

Returns the squared length of this vector (Read Only).

返回这个向量的长度的平方(只读)。

Calculating the squared magnitude instead of the magnitude is much faster. Often if you are comparing magnitudes of two vectors you can just compare their squared magnitudes.

计算长度的平方而不是magnitude是更快的。如果你是比较两个向量的长度差,你可以比较他们的平方长度。
3.velocity.magnitude
该返回值是速度在三维空间的模,即速度的大小

原创粉丝点击