Floating points in a counter

Hey people!

I’m totally nubbie in Unity and i need some help

i’d like to know how i can make a counter using floating points

eg:

I have an array and i need to increment in the var numbers from 1.4 to 3.46

how can i do it?

thanks!

(script)

var frequencia = new Array();

for (i = 1.4; i <= 3.47; i+= 0.1){ frequencia = i;
if (Input.GetKey (“w”)){
print ( frequencia*);*
}
}
please, help me!

Arrays are by definition indexed by integers! Let me give you an example for why indexing an array by floating-point numbers doesn’t make any sense.

Say you have five houses along High Street. They are numbered 0, 1, 2, 3, 4. When the postman wants to deliver some mail to one of the houses, they get an address which looks like this-

highStreet[3];

for the fourth house along. Now, if the postman got an address like this-

highStreet[1.36]

which house would he deliver it to?

Of course, it’s impossible to deliver such a message. This is why normal arrays are always indexed with integers.

Now, what I don’t understand about your question is, what exactly are you trying to do here? Do you have a list of numbers, and you’re trying to find the numbers which are between certain values? If so, it’s not the indexes which you should be looking for, it’s the contents- the index is still an integer, even if it’s an array of floats.

One old trick, if you need to use non-integer slots in a regular pattern (in your case, 10th’s digit only.) is to map them to integers:

1.4 -> 0
1.5 -> 1
1.6 -> 2
 ...
3.4 -> 20

The pattern is “times 10” (to get rid of the fractions) and “minus 14” (to avoid wasting space in the array.) If N is the 1.4 to 3.4 number, then say something like frequency[ (int)(N*10+0.1)-14 ] = i.

The 0.1 is for rounding down problems – there’s a chance 2.1 is really stored as 2.099999 so will become 20.9 and round down to only 20. Usually requires a lot of testing.