convert real number to integer, What's the principle? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last month.
Improve this question
#include <stdio.h>
int main(void) {
float a;
scanf("%f", &a);
printf("%f\n", (int)((a + 1) / 2) * a);
printf("%d\n", (int)((a + 1) / 2) * a);
return 0;
}
if I typed 4, output is 8.0000
I thought it should be 10.0000 because that code is 1+2+~+n
I don't know the reason.
and second output is 0
I thought it should be 10
why this happen?

printf("%f\n", (int)((a + 1) / 2) * a); is evaluated:
Since a has type float and value 4, a+1 has type float and value 5.
Since (a+1) has type float and value 5, (a+1) / 2 has type float and value 2½.
Since (a + 1) / 2 has type float and value 2½, ((a + 1) / 2) has type float and value 2½.
Since ((a + 1) / 2) has type float and value 2½, (int) ((a + 1) / 2) has type int and value 2.
Since (int) ((a + 1) / 2) has type int and value 2 and a has type float and value 4, (int)((a + 1) / 2) * a has type float and value 8.
printf("%f\n",…_); with an argument of type float and value 8 converts 8 to “8.000000” and prints it. (The float argument is automatically promoted to double in this call.)
In printf("%d\n", (int)((a + 1) / 2) * a);, the argument again has type float and value 8. The behavior of passing an argument of type float for a conversion %d is not defined by the C standard. You need to match the argument type to the conversion specification.

The expression
(int)((4.0f + 1) / 2) * 4.0f
evaluates to 8.0f because the typecast to int has higher precedence than multiplication. Thus, the above expression is equivalent to
((int)((4.0f + 1) / 2)) * 4.0f
You can imagine that being evaluated as
4.0f + 1 --> 5.0f
5.0f / 2 --> 2.5f
(int) 2.5f --> 2
2 * 4.0f --> 8.0f
And with respect to your first printf, it's good that precedence works that way, for the argument corresponding to a %f directive in a printf format must have type double. The default argument promotions apply here, converting float to the needed double, but they would not apply to an int.
and second output is 0 I thought it should be 10 why this happen?
I get a different output (neither 8 nor 10) from the second printf. And that's not surprising, because the %d format directive there requires the corresponding argument to have type int, whereas it actually has type double, as discussed above. Undefined behavior results.
Always remember that printf and scanf formatting directives convey not only the wanted output format, but even more the expected type of the corresponding argument. If you do not provide a corresponding argument of the correct type then all bets are off.

Related

Using the int type in the printf function does not add the same value as the float type [duplicate]

This question already has answers here:
What happens to a float variable when %d is used in a printf?
(4 answers)
C printf using %d and %f
(4 answers)
c-behaviour of printf("%d", <double>) [duplicate]
(1 answer)
Why is this code printing 0?
(5 answers)
Closed 6 months ago.
When I use the following code, the output result is 0 when adding the sum in the printf function, but when I add the printf function it should be printf("test: %d", c/ 2);The output is 1,
Why?
int main() {
float a = 1.5;
int b = 2;
int c = a + b;
printf("test: %d", (a+b)/ 2);
}
Since you assign the result of a + b to an int variable, its result will be truncated.
So while the result of 1.5 + 2 is 3.5, the result assigned to c will be the integer 3.
And since both c and 2 are int values the result of c / 2 will be an int which will be truncated. So the result of 3 / 2 will simply be 1.
With the edit and the new print:
printf("test: %d", (a+b)/ 2);
Because one of the values involved in the expression a + b if a float, the result will be a float. And because of that the result of the division will also be a float.
The result of (a + b) / 2 will be a float. But since it's passed as an argument to a variable-argument function like printf it will be promoted to a double. So your statement is essentially equivalent to:
printf("test: %d", (double)((a+b)/ 2));
Now for the big problem: The %d format specifier expects an int value. Mismatching format specifier and argument type leads to undefined behavior.

the code does not work error: called object is not a function or function pointerr

#include <math.h>
#include <stdio.h>
int main() {
double x0, x, eps, dx, r;
x0 = 0.60000;
x = x0;
eps = 0.0001;
do {
r = x + pow(x, 1 / 2) + pow(x, 1 / 3) - 2.5;
dx = (r / (1 - 1 / 2 (x , -1 / 2) + 1/3 (x, -1 / 3)));
x = (x - dx);
} while ((abs(dx)) > eps);
printf("%f %f\n", x, r);
return 0;
}
*error: called object is not a function or function pointer *
the code does not work, what needs to be done to make everything right
You have tons of syntax and semantic errors:
pow(x, 1 / 2)
Here 1/2 does integer division which means that the resulting 0.5 is chopped to 0.
To get floating point results you must ensure that at least one operand is a floating type like 1.0/2.
Then you have a similar piece
1/2(x,-1/2)
Again, due to integer division -1/2 evaluates to 0.
Furthermore you are missing some operator before the brackets.
That means the compiler expects some function call.
But 2 is not a function. That is probably the place where you get your error message.
If you use , in other situations than a parameter list or variable definition list, it is taken as a "comma operator" that evaluates to the second operand. That means (x,-1/2)evaluates to (-1/2) which again evaluates to 0.
In this case I have no idea what you want to achieve with that expression and cannot give some fix for that.
Finally, you use a function that is not suitable for floating point numbers and don't provide a prototype for it as well:
abs(dx)
You should use fabs instead.
Usage of abs is also broken because it requires header stdlib.h which you do not include.

C basic - called object is not a function or function pointer

That's my code -
main()
{
double x;
double y = pow(((1/3 + sin(x/2))(pow(x, 3) + 3)), 1/3);
printf("%f", y);
return 0;
}
I get an error in double y = pow((1/3 + sin(x/2))(pow(x, 3) + 3), 1/3);, it says that called object is not a function or function pointer. I don't get it though - (1/3 + sin(x/2))(pow(x, 3) + 3) is the first element of pow(x, y); that is the x I want to raise to y (1/3) power. Where lies the problem? I'm pretty new to c basic but I can't find the answer anywhere.
If you want to multiply, you need to use the * operator. You can't put parenthesized expressions adjacent to each other to denote multiplication.
(1/3 + sin(x/2))*(pow(x, 3) + 3)
That's because the return value of pow is a double, not a function. Maybe what you're trying to do is pass a call to pow as the second argument to the first pow.
#include <stdio.h>
#include <math.h> // For pow() function
int main() {
// Initialize with whatever value you want
double x = 100;
// Make sure to use an arithmetic operator
double y = pow(((1/3.0 + sin(x/2))*(pow(x, 3) + 3)), 1/3.0);
// Use right format specifier
printf("%lf", y);
return 0;
}
Don't forget to include math.h library.
Initialize x with a value.
Avoid using integer division 1/3, instead use 1/3.0 or 1.0/3.0 because 1/3 == 0.
Use an arithmetic operator (+, -, *, /) before middle pow() like pow(((1/3.0 + sin(x/2))+(pow(x, 3) + 3)), 1/3.0).
(Opt.) Use right format specifier for y which is %lf. But you may get away with %f.

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.

Issue with fahrenheit conversion formula in C [duplicate]

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.

Resources