Dev-C++ 536870912 float variable output [duplicate] - c

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.

Related

why does this program output 37? [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 1 year ago.
#include <stdio.h>
int main()
{
float c = 5.0;
printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32);
return 0;
}
Change your statement to (9.0/5.0)*c + 32 as 9 and 5 are integers, their division returns integer that is 1. So write them in float variable format.

Wrong result printed after calculating the volume of a sphere [duplicate]

This question already has answers here:
Use of %d inside printf to print float value gives unexpected output
(4 answers)
Closed 2 years ago.
This is my code:
#include <stdio.h>
#define PI 3.141592f
#define RADIUS 10.0f
int main(void)
{
float volume;
volume = (4.0f/3.0f) * PI * (RADIUS * RADIUS * RADIUS);
printf("Volume: %d\n", volume);
return 0;
}
It's wrongly printing the value 536870912.
What am I doing wrong? As far as I know, the math is right.
%d is for printing int, so passing float to that invokes undefined behavior.
You should use %f or %g to print float.

How does one make a floating point number stop rounding to an integer? [duplicate]

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

Why the result is 1024 less instead of 1000 if we subtract 1000 from 2^63? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
Could you explain why the difference is 1024 instead of 1000?
int main(void) {
unsigned long long int x = pow(2,63);
double y = pow(2,63) - 1000;
double z = 9223372036854775808.0 - 1000.0;
printf("%llu\n%f\n%f\n", x,y,z);
}
Output is:
9223372036854775808
9223372036854774784.000000
9223372036854774784.000000
Because among the floating-pointer numbers representable in type double, 9223372036854774784 happens to be the closest to the mathematically-correct result 9223372036854774808.
Let's inspect the respresentable neighborhood of your 9223372036854774784
#include <float.h>
#include <math.h>
#include <stdio.h>
int main()
{
double d = 9223372036854774784;
printf("%lf\n%lf\n%lf\n", nextafter(d, -DBL_MAX), d, nextafter(d, DBL_MAX));
}
On my platform the output is
9223372036854773760.000000
9223372036854774784.000000
9223372036854775808.000000
Which one would you pick? Your implementation decided to go with 9223372036854774784.

How pow function works in this case? [duplicate]

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

Resources