This question already has an answer here:
C++ pow function get a weird result [duplicate]
(1 answer)
Closed 8 years ago.
#include <stdio.h>
#include <math.h>
int main()
{
int i = 11;
printf("%d ^ 2 = %d\n",i,(int)pow(i,2));
getchar();
return 0;
}
In this case instead of getting 121,i am getting 120.What is the mistake i am making?
(I really need to print pow(i,2) as an int.)
Casting to integer truncates the fraction, possibly pow returned something like 120.99999998 or so...
Don't cast to (int) and use %g format instead of %d to print double result of pow().
It is to do with rounding from double to int.
As the power is constant why have the overhead.
Stick to
printf("%d ^ 2 = %d\n",i, i*i);
Related
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.
This question already has answers here:
Is floating point math broken?
(31 answers)
Why are floating point numbers inaccurate?
(5 answers)
Is floating-point == ever OK?
(14 answers)
Closed 11 months ago.
sample code is here, desire output is 2 ::
#include <stdio.h>
int main()
{
double i, a, b;
int j;
for (i = 0; i <= 3; i = i + .20)
{
if (i == 2)
{
printf("I=%lf\n", i);
}
}
}
When I use
#include <stdio.h>
int main()
{
double i, a, b;
int j;
for (i = 0; i <= 3; i = i + .25)
{
if (i == 2)
{
printf("I=%lf\n", i);
}
}
}
it works; but in the first case, it is not working. WHY ??
The short answer is that the use of a floating control variable for a for loop is unwise... comparing a floating value for equality is even less so.
Due to the storage of floating point numbers as a mantissa and an exponent, your 0.20000000 may well be 0.199999999...9 or 020000000...01 thus the comparison fails.
Typically, 0.25 and 2.000 will store exactly, as they are powers of 2. Hence a step of 0.25 works as anticipated.
MISRA C:2012 has Rule 14.1 to protect against using float or doubles as loop counters... and previously had a Rule to protect against testing float/double for equality -perhaps we should reinstate that Rule.
This question already has answers here:
Printing int as float in C
(7 answers)
Closed 1 year ago.
#include <stdio.h>
#include <math.h>
int main(void)
{
float a = 16777215;
int b = pow(2, 26);
float c = 22345678;
printf("%f\n", a);
printf("%f\n", b);
puts("---------------");
printf("%f\n", c);
printf("%f\n", b);
return 0;
}
output:
16777215.000000
16777215.000000
---------------
22345678.000000
22345678.000000
why the former printf output can have influence to the subsequent printf output?
b isn't a float. Try %i for integer or %d for decimal.
#include <stdio.h>
#include <math.h>
int main(void)
{
float a = 16777215;
int b = pow(2, 26);
float c = 22345678;
printf("%f\n", a);
printf("%i\n", b);
puts("---------------");
printf("%f\n", c);
printf("%i\n", b);
return 0;
}
You need to match the type or strange things can happen:
printf("%d\n", b);
Compiling the original code with clang gives helpful warnings:
pow.c:10:20: warning: format specifies type 'double' but the argument has type 'int' [-Wformat]
printf("%f\n", b);
~~ ^
%d
pow.c:13:20: warning: format specifies type 'double' but the argument has type 'int' [-Wformat]
printf("%f\n", b);
~~ ^
%d
varargs functions like printf have a hard time pinning down their type requirements, it's up to compilers like clang to go the extra mile and show hints like this. You'll need to be extra careful when calling functions of that sort and be sure you're doing it precisely as documented.
As to how this ended up happening, it's not clear, but it doesn't have to be. Undefined behaviour is just that: Anything can happen. It could print the same thing. It could work. It could crash. There doesn't have to be an explanation.
Undefined behavior as you try to print integer with %f
Try
printf("%d\n", b);
To answer your specific question, my guess is that, at the assembly level, if the conversion is not correctly indicated, it will jump to ret, using the value previously stored in eax register.
This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
How to divide integers and get a float in C
(2 answers)
Closed 2 years ago.
I'm trying to write a basic programme, which gets a user's input, divides by seven, and then returns the result.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int s = get_int("Input: ");
float f = (s / 7);
printf("%f\n", f);
}
Eg. if I input "8", I would expect 1.142857. But I instead simply get "1.000000". Why is this?
#include <stdio.h>
int main()
{
int s=4;
float f = ((float)s / 7);
printf("%f\n", f);
getch();
}
You just have to typecast the int to float. int/float division give you the floor of given integer whereas float/float division gives you float
This question already has answers here:
C: printf a float value
(7 answers)
Closed 3 years ago.
I just made this code:
#include <stdio.h>
#include <stdlib.h>
#define PI 3.1416
int main (){
float x;
x = PI;
printf("x equals: %i.\n",x);
system("pause");
return 0;
}
and the 'Pi' number is 536870912. Can anybody tell me what is wrong?
You're using the %i format specifier for printf. This is for signed integers. Instead use %f as x is a float.