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
Related
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
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
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);
This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 7 years ago.
I've written a code for the following program but the output seems to be wrong.
Question:
https://www.hackerrank.com/challenges/plus-minus
Code:
#include <stdio.h>
int main() {
int N,num,i,cp=0,cn=0,cz=0;
double fp,fn,fz;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d",&num);
if(num>0)
cp=cp+1;
else if(num==0)
cz=cz+1;
else
cn=cn+1;
}
fp=cp/N;
fn=cn/N;
fz=cz/N;
printf("%lf\n%lf\n%lf",fp,fn,fz);
return 0;
}
The Output comes as:
0.000000
0.000000
0.000000
Istructions:
fp=cp/N;
fn=cn/N;
fz=cz/N;
Are performed as integer division.
Change your code to:
fp=(double)(cp)/(double)(N);
fn=(double)(cn)/(double)(N);
fz=(double)(cz)/(double)(N);
you are doing an integer division which creates only integer results. If you want to calculate floating point results you need to perform floating point divisions.
int a = 1;
int b = 3;
int c = a / b;
// c is now 0 -> integer division
double i = 1.0;
double j = 3.0;
double k = i / j;
// k is now 0.3333333 -> floating point division
For correct result cast these expression's to double-
like this
fp=cp/N; // fp=(double)cp/N;
fn=cn/N; // fn=(double)cn/N;
fz=cz/N; // fz=(double)cz/N;
Working code
In previous case if cp(or cn or cz) is less than N then due to integer division you will get 0(fraction part will be discarded). Therefore m these casts .
Another method would be to use all variables as double .
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;