-1

I am trying to do a division to get a decimal answer and every time I try it it returns either 1 or 0 which annoying as the actual answer is 0.017... as I am effectively doing 1/560 however I cannot just use this case as I want it to be variable, as in I want to be able to put in different values and do this function.

Here is my code:

float x = 1/width;
mat4 matrix =
{
    {x, 0.0f, 0.0f, 0.0f},
    {0.0f, 0.00416f, 0.0f, 0.0f},
    {0.0f, 0.0f, 1.0f, 0.0f},
    {0.0f, 0.0f, 0.0f, 1.0f},
};```
Electric fox
  • 35
  • 1
  • 8

1 Answers1

0

In code:

int width = 2;
float x = 1/width;

operation 1/width is integer division which is rounded to the nearest integer.

To perform floating point division do:

float x = 1.0f / width;

width will be automatically converted to float.

tstanisl
  • 13,520
  • 2
  • 25
  • 40