How can you store floats in an array?

I’m trying to store float values in an array but it’s insisting that they must be stored as ‘int’.
I need to have the fractional parts utilized so even though I could convert the floats with int(float) it’s not what I’m looking for as the solution.

How can you store floats in such an array?

float dimx=800;
float dimy=850;
float[] exposure = new float[dimx*dimy]

returns error:
Type mismatch, “float” does not match with “int”

1 Like
static final int ROWS = 850, COLS = 800, GRID = ROWS * COLS;
final float[] exposures = new float[GRID];

void setup() {
  println(exposures.length); // 680000

Hi ProsExplorer,

the reason why you get an error is because the compiler prevents you to do something like this:

float[] exposure = new float[25.4];

You can’t do that and if dimx and dimy are float number, nothing can stop you from doing something like the line above.

1 Like

Thanks for letting me know.

Is there any other way to create an array that would store float or double numbers?

You are messing up the different elements. Let’s break it down:

  • float[] means that you want an array of float
  • exposure is the name of the array
  • new float[n] create an array of float containing n elements.

So you can store int, float, boolean, PVector… Anything your want.

The thing you can’t do is having n being a non integer because it makes no sense to have 2.46 elements in your array for example. Only integer values works here.

For example:

int[] intArray = new intArray[10] // create an array called intArray containing 10 integer values
float[] floatArray = new float[27] // create an array called floatArray containing 27 float values
PVector[] vectArray = new PVector[167] // create an array called vectArray containing 167 PVector

int[] intArray2 = new int[13.856] // will throw an error because 13.856 is not an integer
int[] intArray3 = new int[-56] // will throw an error because -56 is negative