Java在计算过程中可能出现的问题

Java在计算过程中可能出现的问题,java,math,logic,Java,Math,Logic,我正在将这两个值传递给我的函数 float c = -80f; float m = 1.66f; 该函数采用两个浮点数,用于增强图像的对比度,但是在if结构期间,程序总是进入if else块,这是不正确的。因此,数组中的每个值都被指定为255,这是不正确的,有人能确定为什么会出现这种情况吗 //look up table for linear stretching public short[] linearStretchLut(float m,float c) { sh

我正在将这两个值传递给我的函数

    float c = -80f;
    float m = 1.66f;
该函数采用两个浮点数,用于增强图像的对比度,但是在if结构期间,程序总是进入if else块,这是不正确的。因此,数组中的每个值都被指定为255,这是不正确的,有人能确定为什么会出现这种情况吗

//look up table for linear stretching
public short[] linearStretchLut(float m,float c)
{
    short Lut[] = new short[256];

    for(int i =0; i<Lut.length;i++){            
        if(i < -c/m){
            Lut[i] = 0;
        } else if(i > (255 - c) / m) {
            Lut[i] = 255;
        } else {
            Lut[i] = (short) (m*i+c);
        }
        System.out.println(i + " " + Lut[i]);
    }
    return Lut;
}   
//查找线性拉伸的表格
公共短线[]线性短线(浮点数m、浮点数c)
{
短Lut[]=新短[256];
对于(int i=0;i(255-c)/m){
Lut[i]=255;
}否则{
Lut[i]=(短)(m*i+c);
}
System.out.println(i+“”+Lut[i]);
}
返回Lut;
}   
一个使条件为假的例子

i=10 10>(255-c)/m (最终值应为201.8)
因此,这应该是一个错误的评估,但程序不会复制这种行为。

问题是


我以错误的顺序将变量m和c传递到函数中,因此问题是我自己造成的:)

我创建了以下测试程序,您似乎只是以错误的顺序传递了值:

package test;

public class Test {

    //look up table for linear stretching
    public static short[] linearStretchLut(float m,float c)
    {
        short Lut[] = new short[256];

        for(int i =0; i<Lut.length;i++){ 
            float f1 = -c/m;
            float f2 = (float) (255 - c) / m;
            if(i < f1){
                Lut[i] = 0;
            } else if(i > f2) {
                Lut[i] = 255;
            } else {
                Lut[i] = (short) (m*i+c);
            }
            System.out.println("-c/m = " + f1 + " / " + " (255 - c) / m = " + f2 + "/ i = " +  i + " / Lut[i] = " + Lut[i]);
        }
        return Lut;
    } 

    public static void main(String[] args)
    {
        float c = -80f;
        float m = 1.66f;
        short Lut[] = Test.linearStretchLut(m,c);
    }
}
封装测试;
公开课考试{
//查找线性拉伸的表格
公共静态短线[]线性短线(浮点数m、浮点数c)
{
短Lut[]=新短[256];
对于(int i=0;i f2){
Lut[i]=255;
}否则{
Lut[i]=(短)(m*i+c);
}
System.out.println(“-c/m=“+f1+”/“+””(255-c)/m=“+f2+”/i=“+i+”/Lut[i]=”+Lut[i]);
}
返回Lut;
} 
公共静态void main(字符串[]args)
{
浮点数c=-80f;
浮球m=1.66f;
短Lut[]=测试线性拉伸(m,c);
}
}

您是否尝试过使用调试器?在
Lut[i]=255上设置断点行,然后查看i、c和m的值。呜呜,对不起,我发帖时你回答了。很抱歉