相关疑难解决方法(0)

.NET框架中是否定义了FLT_EPSILON?

可能的重复:
Double.Epsilon 表示相等、大于、小于、小于或等于、大于或等于

我最近发现FLT_EPSILON中的定义与 .NET Framework 中的定义<cfloat>完全不同。Single.Epsilon

  • FLT_EPSILON被定义为 1.0 + epsilon != 1.0 的最小值。

  • Single.Epsilon被定义为大于零的最小可能数。

<cfloat>.NET Framework 中的某处是否定义了 -style epsilon?或者,如果我需要定义自己的值,是否应该在 .NET 中定义与我在 中看到的值相同的值<cfloat>

.net c# floating-point

5
推荐指数
1
解决办法
1755
查看次数

计算给定的总变化量

您好我现在正在使用C#和.NET Framework书进行计算,而我在其中一个练习中遇到了困难

编写C#程序进行更改.输入小于一美元的项目的成本.输出硬币作为更改,使用季度,硬币,镍币和硬币.尽可能使用最少的硬币.例如,如果该项目花费17美分,则变化将是四分之三,一个镍和三个便士

由于我还在尝试掌握c#编程,我提出的最佳方法是使用while循环.

while(costOfItem >= 0.50)
            {
                costOfItem -= 0.50;
                fiftyPence++;
            }
Run Code Online (Sandbox Code Playgroud)

我为每个笔20,10,5等都有这些.我正在检查金额是否大于或等于50便士,如果是这样,我从用户给出的金额减少50便士并加1 fiftypence变量.

然后它移动到下一个while循环,我对每个pences都有.问题是,沿着线路的某个环路消失,比如20便士,而costOfItem变成类似"0.1999999999999"的东西,然后它永远不会下降到0,它应该得到正确的变化量.

任何帮助表示赞赏,请不要建议我已经涵盖的复杂程序.

c# loops while-loop

2
推荐指数
1
解决办法
753
查看次数

Math.Abs​​()不会创建精确的绝对值

我正在研究一个模拟空间物理/重力的项目,其中一部分是预测轨道和显示某些信息,其中一个是近地点和顶点(轨道的最近和最远点).实现这一目标的最简单方法是找到轨道中物体相对于静止物体的接近速度为0的点,但由于每秒的计算量有限,因此几乎不会发生该值精确为0的情况 - 所以我的如果当前值的符号和之前的循环值已经改变,那么我们就想比较每个循环.

代码看起来类似于:

ArrayList orbit = new ArrayList();    
orbit.Add(newValue);

//In case the sign changes - to +

if(
   orbit.Count >= 2 &&
   physics.getVelocity(orbit[orbit.Count-1], otherObject) == Math.Abs(physics.getVelocity(orbit[orbit.Count-1], otherObject) &&
   physics.getVelocity(orbit[orbit.Count-2], otherObject) != Math.Abs(physics.getVelocity(orbit[orbit.Count-2], otherObject)
)

//In case the sign changes + to -

if(
   orbit.Count >= 2 &&
   physics.getVelocity(orbit[orbit.Count-1], otherObject) != Math.Abs(physics.getVelocity(orbit[orbit.Count-1], otherObject) &&
   physics.getVelocity(orbit[orbit.Count-2], otherObject) == Math.Abs(physics.getVelocity(orbit[orbit.Count-2], otherObject)
)
Run Code Online (Sandbox Code Playgroud)

但有时它似乎没有用,所以我对变量进行了调整,并且找不到任何错误,总是有这样的模式:

Value (1): -0.4523452
Value (2): 1.2435235
Run Code Online (Sandbox Code Playgroud)

要么

Value (1): 0.4523452
Value (2): -1.2435235
Run Code Online (Sandbox Code Playgroud)

但该算法忽略了一些事件,但仅在数字接近于0(如上所述)时我的观察结果,但如果它看起来像这样:

Value (1): 64.904534
Value (2): …
Run Code Online (Sandbox Code Playgroud)

c#

1
推荐指数
1
解决办法
1622
查看次数

if 语句未在 c# unity 中执行

我试图让一个角色移动一定距离,然后在他们移动一定距离时改变方向......我的代码中的逻辑可能是错误的我仍然需要解决这不是问题,问题是我的 if 语句不执行

public class EnemyControl : MonoBehaviour
{
    private int xMoveDirection=-1;
    private float x;
    void Start()
    {
        x=gameObject.transform.position.x;
    }
    void Update()
    {
        gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(xMoveDirection,0);
        if(x==0.00){
          Debug.Log("helloimhere");
          xMoveDirection=0;
          x=x+1;
        }
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine game-physics

-2
推荐指数
1
解决办法
50
查看次数