- Home /
Differance in formula results in Open Office spreadsheet and Unity
I'm getting different results in Unity(using C#), than my formula in OpenOffice Calc.
(Formula with inputs and results above.)
public static int Formula(int i, int j)
{
return i - ((int)((float)(j / 2) * ((j - 1) * 6) + 1) - ((j - 1) * 6)) + 1;
}
(Formula in Unity)
For example if I have i = 9 and j = 3 returns 9 in Unity while I am expecting it to return 3, while i = 2, j = 2 or j=24, j=4 returns the correct value.
Is there something difference about how the formula is calculated in C# or do I have to restructure the formula.
Answer by saschandroid · Aug 22, 2016 at 07:14 AM
That's because you divide integers: 3/2 = 1 ... You have to cast to float like this ( (float)j/2 )
.
Futhermore the foruma can be simplified quite a bit:
return (int)(i- (j*0.5f-1f)*((j-1f)*6f);
The term "((j - 1) * 6)" is redundant. It can be moved into the first bracket as "-1". Further more the two "+1" cancel each other out. By using float constants you don't need to explicitly cast to float.
Note: you might want to use $$anonymous$$athf.RoundToInt
ins$$anonymous$$d of the int cast at the end. This ensures that if the result is slighly below the closest integer it isn't one off since 3.9999 casted to int is "3" not "4"
Thanks saschandroid. I thought I'd had the (float) there at one point trying things out, looks like I didn't. And thanks Bunny83 I knew that formula was bit bigger than it needed to be.