Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do you use multiple decimal place float division in C?

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},
};```

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading