I need to preserve granularity in fuzzy work, so I made a function that receives as parameters:
- granularity (double between 0 and 1, 0 not included)
- value for conversion, if necessary (this double is greater than 0)
And returns a new value with the correct granularity.
This is the function that I have:
public static double preserveGranularity(double granul, double value){ double integerValue= (double)(int)value; if(granul == 1) return integerValue; double decimal = value - integerValue; long factor = Math.round(decimal/granul); return integerValue + granul*factor; }
Examples:
- preserveGranularity (0.25, 7.7) should return 7.75
- preserveGranularity (0.01, 0.009) should return 0.01
It works well for most values, but it does not work for everyone (for example: preserveGranularity (0.2, 0.57) = 0.6000000000000001, it should return 0.6)
Hi
PS Sorry for any mistake I made on this issue, this is my first here.
source share