Issue with fahrenheit conversion formula in C [duplicate] - c

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.

Related

Noob Question! Why does this always return 0? [duplicate]

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);

float number is allways .0 [duplicate]

I need some help with a program for converting Fahrenheit to Celsius in C. My code looks like this
#include <stdio.h>
int main(void)
{
int fahrenheit;
double celsius;
printf("Enter the temperature in degrees fahrenheit:\n\n\n\n");
scanf("%d", &fahrenheit);
celsius = (5 / 9) * (fahrenheit - 32);
printf("The converted temperature is %lf\n", celsius);
return 0;
}
Every time I execute it it the result is 0.000000. I know I'm missing something but can't figure out what.
5/9 will result in integer division, which will = 0
Try 5.0/9.0 instead.
You problem is here :
celsius = (5/9) * (fahrenheit-32);
5/9 will always give you 0. Use (5.0/9.0) instead.
try celsius = ((double)5/9) * (fahrenheit-32); Or you can use 5.0.
The fact is that "/" looks at the operand type. In case of int the result is also an int, so you have 0. When 5 is treated as double, then the division will be executed correctly.
write 5/9.0 instead of 5/9 -- this forces double division
You need to use floating point arithmetic in order to perform these type of formulas with any accuracy. You can always convert the final result back to an integer, if needed.
When dealing with floats, it needs to be 5.0f / 9.0f.
When dealing with doubles, it needs to be 5.0 / 9.0.
When dealing with integers, remainders/fractions are always truncated. 5 / 9 results between 0 and 1, so it is truncated to just 0 every time. That multiplies the other side by zero and completely nullifies your answer every time.

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

Casting int as a double is returning #INF00

So I am trying to cast (3/17) as double. This is for an assignment so the professor wants it to be this way for some reason.
I am trying to cast it by doing the following:
(double)(3/17)
Actual code:
int assignment7()
{
#include <stdio.h>
#define PI 3.14
int a=0;
double Ny=0,y=0,z=0,x=0,amod2=0;
printf("Enter values for x,y,z and a(must be an odd number): ");
scanf("%lf%lf%lf%d",&x,&y,&z,&a);
amod2=a%2;
printf("%.2lf\n",test);
Ny=y / (double)(3/17) - z + x / amod2 + PI;
printf("%lf\n",Ny);
}
The problem is occurring on the second to last line where it is interpreting 3/17 as an int thus it would equal 0. y / 0
Professors exact instructions:
"General equation: y = y / (3/17) - z + x / (a % 2) + PI (recall: a is an integer; the 3 and 17 constants in the equation should be left as integers initially, but explicitly type-casted as floating-point values)"
(3/17) is equal to 0 because it is evaluated using integer arithmetic, and so you get a divide by zero, which is of course a run-time error. Change:
(double)(3/17)
to:
(3.0 / 17.0)
Note that the cast is redundant.
You have a division by zero as 3/17 is zero.
Instead use doubles to begin with: 3.0 / 17.0, then you don't even need the cast.

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