- Home /
Unity not giving correct solution to equation
I'm attempting to make a shmup wave spawner object that can spawn enemies in set patterns -- currently working on the "greater than" shape: > I came up with a formula that works in google sheets - the basic calculator that comes w/ windows - and everywhere else -- Unity, however, decides to give the wrong answer. The image attached - shows my google sheet - where I came up w/ the equation and tested a few values. On the right side, I simplify things and just Debug.Log the equation w/ preset values... and show the console result.
what should be 3.4, 2.3, 1.1 - is somehow 4, 4, 4
What gives?
Hi @dalessan9 - Try adding "f" after your numbers. Currently your numbers will be int numbers ins$$anonymous$$d of float number. So change 4 to 4f and so on. Otherwise you'll divide 4 by 7 and result will be whole numbers. I added this as answer + an example.
Answer by eses · Aug 19, 2018 at 04:43 PM
Hi @dalessan9
Try adding "f" after your numbers.
Currently your numbers will be int numbers instead of float number.
So change 4 to 4f and so on.
Otherwise you'll divide 4 by 7 and result will be whole numbers, see example below:
Debug.Log("Calc ints:" + 4 / 7);
// Calc ints:0
Debug.Log("Calc floats:" + 4f / 7f);
// Calc floats:0.5714286
kind of infuriating to have to specify that w/ generic numbers used in a formula - I'd call it a bug - but that's probably built in to C#
another one of those - assume it's stupid - and you're probably right - type of things thanks
No this is not a bug or anything weird. It's just your lack of understanding the program$$anonymous$$g language. Integer divisions existed in most CPUs before we got floating point numbers support.
Try switching your windows calculator into programmer mode and you actually get integer divisions as well. You should learn about datatypes.
btw:
this would only make any sense if you were expecting integer division as this would round down to the closest multiple of 7. Though in your case always to 0
(((0 + 1) / 7) * 7)
However if you expect this to perform a floating point division it just makes no sense as it's the same as just
(0 + 1)
So even the formula in your sheet makes no sense as you divide and multiply by the same value which has no effect.
sorry, I know you tried pretty hard, but I need more rep to down vote your comment
Your answer
Follow this Question
Related Questions
Any Good References for Game Math 1 Answer
Implement the equation as a code? 1 Answer
Random.seed Repeating 2 Answers
Normal distribution random 3 Answers