Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何解决双模问题以获得;0“;在c中#_C#_.net_Casting_Double_Modulo - Fatal编程技术网

C# 如何解决双模问题以获得;0“;在c中#

C# 如何解决双模问题以获得;0“;在c中#,c#,.net,casting,double,modulo,C#,.net,Casting,Double,Modulo,在s的情况下,我发现模为2.0%0.1,这是双变量返回“0.09999”而不是“0”的结果 我的代码是: var distanceFactor = slider.Value % step; //This do not return 0 when Value=2.0 and step=0.1 if (distanceFactor != 0) { slider.Value -= distanceFactor; } elseif(dist

在s的情况下,我发现模为2.0%0.1,这是双变量返回“0.09999”而不是“0”的结果

我的代码是:

    var distanceFactor = slider.Value % step; //This do not return 0 when Value=2.0 and step=0.1
    if (distanceFactor != 0)
    {
        slider.Value -= distanceFactor;
    }    
    elseif(distanceFactor == 0)
    {
       //do something here
    }           
    txtblUnits.Text = Math.Round(slider.Value, 1).ToString();

当值=2.0,步长=0.1时,如何在
elseif
条件下获得控制?

%
对浮点数没有意义。在应用运算符之前,应该将其相乘并强制转换为int

var distanceFactor = ((int) (slider.Value * 10D)) % (int) (step * 10D);

%
对浮点数没有意义。在应用运算符之前,应该将其相乘并强制转换为int

var distanceFactor = ((int) (slider.Value * 10D)) % (int) (step * 10D);

在if条件下,可以将距离因子四舍五入为0小数,如下所示

if (Math.Round(distanceFactor,0) != 0)
{
    slider.Value -= distanceFactor;
}    
else
{
   //do something here
}

在if条件下,可以将距离因子四舍五入为0小数,如下所示

if (Math.Round(distanceFactor,0) != 0)
{
    slider.Value -= distanceFactor;
}    
else
{
   //do something here
}

Double
不适用于模运算符,因此使用带浮点数的模从来都不是一个好主意,因此最好使用
decimal

decimal slider = 2.0M, step = 0.1M;
var distanceFactor = slider % step; //This will return 0 when Value=2.0 and step=0.1
if (distanceFactor != 0)
{
    slider -= distanceFactor;
}
else if (distanceFactor == 0)
{
    //do something here
}
txtblUnits.Text = Math.Round(slider, 1).ToString();

txtblUnits.Text
将具有
2.0

Double
不适用于模数运算符,因此将模数与浮点数一起使用从来都不是一个好主意,因此最好使用
十进制

decimal slider = 2.0M, step = 0.1M;
var distanceFactor = slider % step; //This will return 0 when Value=2.0 and step=0.1
if (distanceFactor != 0)
{
    slider -= distanceFactor;
}
else if (distanceFactor == 0)
{
    //do something here
}
txtblUnits.Text = Math.Round(slider, 1).ToString();

txtblUnits.Text
将具有
2.0

正如我在您的帖子中评论的那样,为了正确使用模运算符,请尝试执行以下操作:

string valueDecimalsString = (slider.Value - Math.Floor(slider.Value)).ToString();

int valueDecimals = valueDecimalsString.Substring(valueDecimalsString.IndexOf('.') + 1).Length;

//The same with step

int decimals = (valueDecimals > stepDecimals) ? valueDecimals : stepDecimals;

int value = (int)(slider.Value * Math.Pow(10, decimals))

//the same with step

var distanceFactor = value % stepValue; 

正如我在您的帖子中所评论的,为了正确使用模运算符,请尝试执行以下操作:

string valueDecimalsString = (slider.Value - Math.Floor(slider.Value)).ToString();

int valueDecimals = valueDecimalsString.Substring(valueDecimalsString.IndexOf('.') + 1).Length;

//The same with step

int decimals = (valueDecimals > stepDecimals) ? valueDecimals : stepDecimals;

int value = (int)(slider.Value * Math.Pow(10, decimals))

//the same with step

var distanceFactor = value % stepValue; 


您可以检查距离因子步长是否小于预定义的ε值。顺便说一下,双精度模不是一个好主意……模不应该与非整数值一起使用,因为在数学中,它是一个整数运算符。你试着做mod(2,0.1),这和做mod(20,1)是一样的。看看提供的“重复问题”链接,有一个关于模运算符发生什么的等式。。。就这么做吧:<代码>双a、b、r;a=120;b=.05;r=a-数学楼层(a/b)*b您可以检查距离因子步长是否小于预定义的ε值。顺便说一下,双精度模不是一个好主意……模不应该与非整数值一起使用,因为在数学中,它是一个整数运算符。你试着做mod(2,0.1),这和做mod(20,1)是一样的。看看提供的“重复问题”链接,有一个关于模运算符发生什么的等式。。。就这么做吧:<代码>双a、b、r;a=120;b=.05;r=a-数学楼层(a/b)*bdecimal slider=(decimal)slider.Value我不能这样做,因为这个slider.Value是通过移动slider获得的,它是双精度的,如果我将它设为十进制,那么它将避免打印在textblock.slider id ui元素上的slider progree的小阻力,所以它的值总是双精度的。我们不是手动赋值,它是通过拖动滑块获得的。然后简单地将其转换为十进制,即
decimal slider=(decimal)slider.Value我不能这样做,因为这个滑块。值是通过移动滑块获得的,它是双精度的,如果我将其设置为十进制,那么它将避免打印在textblock上的滑块程序的小阻力。我不能这样做,因为这个滑块。值是通过移动滑块获得的,它是双精度的,如果我将其设置为十进制,那么它将避免小阻力拖动打印在textblock上的slider progree。我不能这样做,因为此slider.Value是通过移动滑块获得的,它是双精度的,如果我将其设置为十进制,则将避免打印在textblock上的slider progree的小拖动。