C - division doesnt work [duplicate] - c

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Why do we separately cast to "float" in an integer division?
(4 answers)
Closed 5 years ago.
I'm actually totally stuck I don't understand the behavior of this function:
#include <stdio.h>
typedef struct s_resolution
{
int x;
int y;
float fx;
float fy;
} t_resolution;
typedef struct s_pixel_info
{
int tablen;
t_resolution resolution;
char name;
int line;
int pos_suite[6];
int suite[6];
} t_pixel_info;
void get_proportion_ratio(t_pixel_info DataB, t_pixel_info pix_info,
t_resolution *proportion)
{
proportion->fx = (float)(DataB.resolution.x / pix_info.resolution.x);
//I also tried without the cast
proportion->y = (int)(DataB.resolution.y / pix_info.resolution.y * 100);
//if (proportion->y != 100)
printf("%d | %d | %d\n",proportion->y, DataB.resolution.y, pix_info.resolution.y);
}
int main()
{
t_pixel_info DataB;
t_pixel_info pix_info;
t_resolution proportion;
DataB.resolution.x = 5;
pix_info.resolution.x = 10;
DataB.resolution.y = 5;
pix_info.resolution.y = 10;
get_proportion_ratio(DataB, pix_info, &proportion);
}
DataB.resolution.y, pix_info.resolution.y and proportion->y are all Int type.
My problem is that I have this result:
0 | 5 | 10
The operation only works when the result is 100... I must be missing something obvious but I've no idea what.

All of your divisions are being done on integers. Take this expression:
5 / 10 * 100
This groups as:
(5 / 10) * 100
This evaluates to 0: 5 / 10 is 0, and 0 * 100 is still 0. Casting the result after the fact doesn't change it.
If you multiply by 100 before you divide, you will obtain two more digits of precision:
100 * 5 / 10
This evaluates to 50: 100 * 5 is 500, and 500 / 10 is 50.
You could also perform the arithmetic in floating point, e.g.
(double) 5 / (double) 10 * (double) 100
Or you could just cast the first operand and let the standard arithmetic conversions handle the rest; the result is equivalent:
(double) 5 / 10 * 100
In either case, the result will be 50.0

Since resolution.x and resolution.y are ints, the division between them will done as integer division, which keeps only the "whole" part of the division. If you want to perform a floating point ("regular") division, you should cast to a floating point type before performing the division. E.g.:
proportion->fx = ((float) DataB.resolution.x) / ((float) pix_info.resolution.x);

Related

Tax variable wont assign to new value (C) [duplicate]

This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 5 years ago.
I got this C code.
#include <stdio.h>
int main(void)
{
int n, d, i;
double t=0, k;
scanf("%d %d", &n, &d);
t = (1/100) * d;
k = n / 3;
printf("%.2lf\t%.2lf\n", t, k);
return 0;
}
I want to know why my variable 't' is always zero (in the printf function) ?
because in this expression
t = (1/100) * d;
1 and 100 are integer values, integer division truncates, so this It's the same as this
t = (0) * d;
you need make that a float constant like this
t = (1.0/100.0) * d;
you may also want to do the same with this
k = n / 3.0;
You are using integer division, and 1/100 is always going to round down to zero in integer division.
If you wanted to do floating point division and simply truncate the result, you can ensure that you are using floating pointer literals instead, and d will be implicitly converted for you:
t = (int)((1.0 / 100.0) * d);
I think its because of
t = (1/100) * d;
1/100 as integer division = 0
then 0 * d always equals 0
if you do 1.0/100.0 i think it will work correctly
t = (1/100) * d;
That is always equals 0,you can do this
t=(1%100)*d
and add it to 0

fahrenheit to celsius, very curious why it didnt compile [duplicate]

This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 5 years ago.
I got this C code.
#include <stdio.h>
int main(void)
{
int n, d, i;
double t=0, k;
scanf("%d %d", &n, &d);
t = (1/100) * d;
k = n / 3;
printf("%.2lf\t%.2lf\n", t, k);
return 0;
}
I want to know why my variable 't' is always zero (in the printf function) ?
because in this expression
t = (1/100) * d;
1 and 100 are integer values, integer division truncates, so this It's the same as this
t = (0) * d;
you need make that a float constant like this
t = (1.0/100.0) * d;
you may also want to do the same with this
k = n / 3.0;
You are using integer division, and 1/100 is always going to round down to zero in integer division.
If you wanted to do floating point division and simply truncate the result, you can ensure that you are using floating pointer literals instead, and d will be implicitly converted for you:
t = (int)((1.0 / 100.0) * d);
I think its because of
t = (1/100) * d;
1/100 as integer division = 0
then 0 * d always equals 0
if you do 1.0/100.0 i think it will work correctly
t = (1/100) * d;
That is always equals 0,you can do this
t=(1%100)*d
and add it to 0

Failure to normalize the dynamic range of an image [duplicate]

This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 5 years ago.
I got this C code.
#include <stdio.h>
int main(void)
{
int n, d, i;
double t=0, k;
scanf("%d %d", &n, &d);
t = (1/100) * d;
k = n / 3;
printf("%.2lf\t%.2lf\n", t, k);
return 0;
}
I want to know why my variable 't' is always zero (in the printf function) ?
because in this expression
t = (1/100) * d;
1 and 100 are integer values, integer division truncates, so this It's the same as this
t = (0) * d;
you need make that a float constant like this
t = (1.0/100.0) * d;
you may also want to do the same with this
k = n / 3.0;
You are using integer division, and 1/100 is always going to round down to zero in integer division.
If you wanted to do floating point division and simply truncate the result, you can ensure that you are using floating pointer literals instead, and d will be implicitly converted for you:
t = (int)((1.0 / 100.0) * d);
I think its because of
t = (1/100) * d;
1/100 as integer division = 0
then 0 * d always equals 0
if you do 1.0/100.0 i think it will work correctly
t = (1/100) * d;
That is always equals 0,you can do this
t=(1%100)*d
and add it to 0

C Floating Point Division Result Rounded to Zero [duplicate]

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Closed 7 years ago.
Why does this expression give me an output of zero?
float x = ((1000)/(24 * 60 * 60));
Breaking this into two parts, gives the correct result:
float x = (1000);
x /= (24 * 60 * 60);
The statement
float x = ((1000)/(24 * 60 * 60));
does the following:
Declares a variable x of type float.
Evaluates ((1000)/(24 * 60 * 60)).
Evaluates 24*60*60 which is 86400.
Evaluates 1000/86400 which is 0.
Assigns the result of that (which is 0) to x.
In the second step, ((1000)/(24 * 60 * 60)) is zero - the division is integer division, because both operands are integers. The fact that the result gets assigned to a floating point variable later makes no difference.
The simplest fix is to make sure either side of the division is a floating-point number, so it will use floating-point division. For example, you could change 1000 to 1000.0f.
See this answer, it will give you the correct output
#include <stdio.h>
int main(void) {
float x = (float)1000/(24 * 60 * 60);
printf("%f",x);
return 0;
}
Output: 0.011574
Output can also be seen: http://ideone.com/6bMp9r

Why does 1 / 10 equal zero unless I use variables? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why can't I return a double from two ints being divided
This statement in C with gcc:
float result = 1 / 10;
Produces the result 0.
But if I define variables a and b with values 1 and 10 respectively and then do:
float result = a / b;
I get the expected answer of 0.1
What gives?
When the / operator is applied to two integers, it's an integer division. So, the result of 1 / 10 is 0.
When the / operator is applied to at least one float variable, it's a float division. The result will be 0.1 as you intend.
Example :
printf("%f\n", 1.0f / 10); /* output : 0.1 (the 'f' means that 1.0 is a float, not a double)*/
printf("%d\n", 1 / 10); /* output : 0 */
Example with variables :
int a = 1, b = 10;
printf("%f\n", (float)a / b); /* output : 0.1 */
That happens because 1 and 10 are integer constants, so the division is done using integer arithmetic.
If at least one of your variables a and b is a float, it will be done using floating-point arithmetic.
If you want to do it with number literals, use the notation to make at least one of them a float literal, for example:
float result = 1.0f / 10;
Or cast one of them to float, that would be a bit more elaborate:
float result = 1 / (float)10;
1 and 10 are both integers and will return an integer, when you define a and b you're defining as a float. If you use 1.0 and 10.0 it will return the correct result
If you want float than just cast it as follow.
float result = (float)a/b;

Resources