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
Related
This question already has answers here:
Integer division returns 0 in C
(2 answers)
Closed last year.
I wanted to get the convert from Fahrenheit to Celsius with this program, but it always returns 0. Why?
#include <stdio.h>
int main() {
int fahr = 15, celsius;
celsius = (5 / 9) * (fahr - 32);
printf("%d\n", celsius);
return 0;
}
It seems that the problem is in celsius = (5 / 9) * (fahr - 32);. I already know that celsius = 5 * (fahr - 32) / 9; fixes the problem. But why did the first one return zero?
In short, when performing arithmetic operations, C compiler has to choose which number format to use. To do this, it won't pick the one of the first operand, nor try to guess what the result would be, but will opt for the most precise type of both operand.
In your particular example, (5 / 9) use two integers, so the result will remain an integer too, thus zero.
A quick way to fix this is to directly specify a decimal in your constant :
(5 / 9.0)
The other answers and comments are steering you in the correct direction with regards to integer division vs floating point. But if you want to avoid floating point altogether and still produce a correct answer (as an integer), then structure your equation such that division comes last.
Since (x/y) * z is the same as (x * z)/y, then this holds:
celsius = (5 * (fahr - 32)) / 9;
In C, if you do a division between two integer type variables, you get an integer back.
So this:
(5 / 9)
gets a result of 0.555, which is then rounded down. (In C, integer division rounds towards zero.)
If you changed it like this, it would be floating point division instead:
celsius = (5.0 / 9) * (fahr - 32);
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:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Why does division result in zero instead of a decimal?
(5 answers)
Closed 9 years ago.
When writing a program in C to convert celsius to fahrenheit, the following formula gives the incorrect output:
int fahr = 9 / 5 * celsius + 32;
Now, I understand that this is probably an issue with 9/5 being interpreted as an integer, but what I don't understand is that using double or floatit still gives the same incorrect output.
Oddly enough the following formula gives the correct output despite also setting the type to int:
int fahr = celsius / 5 * 9 + 32;
Furthermore, i've noticed even something as simple as the below, when the type is set to double, still gives the output as 1.0 instead of 1.8:
double x = 9 / 5;
printf("%lf\n", x);
I've read this thread:
C program to convert Fahrenheit to Celsius
but I still don't understand why int fahr = celsius / 5 * 9 + 32; works but not int fahr = 9/5 * celsius+32; ?
You're doing math with integers. This expression:
9 / 5
Yields 1 in C. When you say you used double or float, you probably just changed the type of fahr, which doesn't do anything to the operations taking place on the right side of the assignemtn operator. To get the right behaviour, you need to make at least one of those constants a double, too:
9.0 / 5
Likewise, in this statement:
double x = 9 / 5;
You're still doing integer math, and then assigning the result to a double variable. There isn't anything else going on. You'll get the right answer by doing one of these:
double x = 9.0 / 5;
double x = 9 / 5.0;
double x = 9.0 / 5.0;
The reason this expression:
int fahr = celsius / 5 * 9 + 32;
appears to work is just an order of operations thing - here you divide the input by 5 and then multiply by nine, rather than doing the constant operation first. You'd still get more accurate answers by doing:
int fahr = celsius * 9 / 5 + 32;
Besides that, you could also do floating point math in this expression:
int fahr = celsius * 9.0 / 5 + 32;
If you want to do the original calculation using integers, you certainly can - you just need to multiply before dividing:
int fahr = 9 * celsius / 5 + 32;
This expression is equivalent to one of the ones used above.
I still don't understand why int fahr = celsius / 5 * 9 + 32; works but not int fahr = 9/5 * celsius+32;
For the former, you probably declared celsius as a float or as a double, making the entire right side evaluate as such. Assuming you used float, it works out like this:
celsius / 5 * 9 + 32
(celsius / 5.0) * 9 + 32
(celsius / 5.0 * 9.0) + 32
(celsius / 5.0 * 9.0 + 32.0)
For the latter, 9/5 is integer arithmetic that evaluates to 1 before the rest of the math happens as floating point. In this case:
9 / 5 * celsius + 32
1 * celsius + 32 // Because of this, you get an incorrect answer
celsius + 32
celsius + 32.0
Note the the type of the left hand side is irrelevant; the right-hand side is evaluated without regard to that.
Update: You said celsius is an int, which means you just happened to get lucky and test with a value that is a multiple of 5, giving you a correct integer result to celsius / 5 before doing valid integer arithmetic for the rest of the statement. In your second example, being a multiple of 5 doesn't help you.
In any case, now you know why you got lucky, but the linked question gives you the answer to what you actually need to to do have a formula that works when celsius isn't a multiple of 5: use floating point math, as demonstrated in all of the answers there.
The type of each expression or subexpression is (in most cases) evaluated without regard to the context in which it appears.
In this declaration:
double x = 9 / 5;
the initialization expression is 9 / 5; it consists of two int expressions and a division operator. Since the operands of / are of type int it's an int division, resulting in an int value. Since integer division truncates, the result is 1, of type int.
The result of that expression is then used to initialize x. Since x is of type double, the value is implicitly converted from int to double, resulting in x holding the value 1.0.
If you want the value of x to be 1.8, you need to do floating-point division, which means you need floating-point operands. The simplest and clearest way to do this is:
double x = 9.0 / 5.0;
There are several other (IMHO less clear) approaches. If a / operator has two operands, one of type int and one of type double, the int operand is promoted to double, so either of these will also set x to 1.8:
double x = 9.0 / 5;
/* or */
double x = 9 / 5.0;
But be careful with this approach:
double y = 9 / 5 / 3.0;
This is equivalent to:
double y = (9 / 5) / 3.0;
which computes 9 / 5 as an int, yielding 1, then promotes that result to double and divides it by 3.0, yielding 0.333333333.
The point is that the context of an expression does not impose a type on the expression or its operands; the expression is evaluated as if it were isolated, and then the result may be converted depending on its context.
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;
I have the following in a program (part of a much larger function, but this is the relevant testing bit):
int test = 100 + (100 * (9 / 100));
sprintf (buf, "Test: %d\n\r", test);
display_to_pc (buf, player);
Basically it amounts to:
x = a + (a * (b / 100))
Where a is a given figure, b is a percentage modifier, and x is the result (the original plus a percentage of the original)... I hope that makes sense.
It gives me:
Test: 100
I thought the math in my head might be wrong, but I've checked several calculators and even the expression evaluator in my IDE, and they all give me the expected result of 109 for the first expression.
Can anyone enlighten me as to what I'm missing here?
Thanks much. :)
Replace
int test = 100 + (100 * (9 / 100));
with
int test = 100 + (100 * 9 / 100);
// x = a + (a * b / 100)
and it will work as expected. 9 / 100 is performed using integer division; the nearest integer to .09 is 0 (and 0 * 100 is still 0).
Doing the multiplication first results in 900 / 100, which gives you the 9 that you were expecting.
If you need more than integer precision, you may need to go the floating point route.
You're using integer math.
9/100 = 0.
100 + (100 * (0) = 100.
Your calculators use floating point math, and can handle decimals appropriately.
As others have pointed out, the problem is that you are doing integer arithmethic. You need to do the calculation as floating point by casting the variable to double or using a double constant, then allow truncation to give you just the integer portion.
x = a + (a * (b / 100.0));
or
x = a + (a * ((double)b / 100)));
Your mistake is that 9 / 100 is interpreted as integer division, and evaluates to 0 and not 0.09.
You can write 9 / 100.0 instead, or rearrange the expression.
int test = 100 + (100 * (9 / 100));
9/100 = 0
0 * 100 = 0
100 + 0 = 100
you are using integer division so 9 / 100 is zero so test = 100 + 0 = 100
Use Daniel L's answer if you will only be working with expressions that will result in whole integer values. If the values you'll be working with are less clean, use double literals instead of integer literals:
int test = 100.0 + (100.0 * (9.0 / 100.0));
The answer of 9/10 gets truncated to 0, and then you multiply that by 100, and then add 100 to it.
Change:
int test = 100 + (100 * (9 / 100));
to
int test = (int)(100 + (100 * (9 / 100.0)));
and see if it works as expected.
EDIT: wow. Lots of answers while I was typing. Most of them will work, too.
You should be using floating point arithmetic so 9/100 becomes 0.09.
int test = 100.0 + (100.0 * 9.0 / 100.0);
You are using integers for your variable types, so the inner most expression (9 / 100) will result in a 0. Then the next expression would be 100 * 0, which is 0, and finally 100 + 0...
To get your desired result try using float instead:
float test = 100.0 + (100.0 * (9.0 / 100.0));
printf("result: %0.2f\n", test);