This question already has answers here:
Floating point comparison [duplicate]
(5 answers)
Closed 8 years ago.
1)#include<stdio.h>
int main()
{
float x=0.5;
if(x>0.5)
printf("\ngreater");
else
printf("\nlesser ");
return 0;
}
output->lesser
2)#include<stdio.h>
int main()
{
float x=0.1;
if(x>0.1)
printf("\ngreater ");
else
printf("\nlesser ");
return 0;
}
output->greater
Why in the first case output is "lesser" while in the second one output is "greater"? What is the difference?
EDIT: I understood that 0.1 is not equal, but then why 0.5 is showing as equal?
I almost sure it's because you are comparing float and double.
There's an answer to why it is greater answered here : 0.1 float greater than double
Related
This question already has answers here:
Strange behaviour of the pow function
(5 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
other number number raised to 2 are giving correct answer but 5,11,15... are showing 1 number less than the correct answer.
#include<stdio.h>
#include<math.h>
int main(){
int side;
printf("Enter side value \n");
scanf("%d", &side);
printf("The area is %d", (int) pow(side,2.0));
return 0;
}
This question already has answers here:
Is floating point math broken?
(31 answers)
Why are floating point numbers inaccurate?
(5 answers)
Closed 3 years ago.
Made a code that calculated the amount of negative numbers in an array and shows those numbers. The problem is when the program prints out the number, it changes the number a slight bit, adding or subtracting something like 0.003.
I have absolutely no clue as to what's wrong with it, tried asking my professor; she said she didn't know, so I am here.
float col[10];
...
for (int i = 0; i < 10; ++i)
{
scanf_s("%f", &col[i]);
}
...
printf("\n");
printf("There are %d negative numbers\n", ct);
for (int i = 0; i < 10; ++i)
{
if (col[i] < 0)
{
printf("[%d]=%f ", i, col[i]);
}
}
...
Put in -7786.88, command line printed out -7786.888184. It's fine on integers, just prints out a bunch of zeroes after the dot.
This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 4 years ago.
I'm trying to make and print a series of the form 1/n; where n is a natural number.
int Number;
float NumberInverse, NumberInverseNext;
for ( Number = 1; Number < 1000; Number++)
if I try to print 'Number' after this, I get a series of natural numbers from 1-1000, as I'd expect. But if I do
NumberInverse = 1/Number;
and try to print NumberInverse, I get 0 as output. I'm not sure what I'm doing wrong and what I should be doing.
EDIT : This is not a duplicate of the question mentioned as even after changing
NumberInverse = 1/Number;
to
NumberInverse = 1/(float)Number;
I can't get the series of 1/n which was the original question
try casting, the answer in your case is an integer so you will get 0, try this:
int Number;
float NumberInverse, NumberInverseNext;
for ( Number = 1; Number < 1000; Number++) {
NumberInverse = 1/(double)Number;
printf("%f \n",NumberInverse);
}
This question already has answers here:
Why printf() isn't outputting this integer as float number? [duplicate]
(8 answers)
Closed 4 years ago.
Why does the following code output 0 instead of 40?
#include <stdio.h>
int main()
{
int volume;
int length = 5;
int width = 8;
volume = length * width;
printf("%f", volume);
return 0;
}
The variable "volume" is Integer , you need in the printf function to change from %f that is for float variable, to %d that is for print Integer variable.
Declaration of a variable is int volume; and you are printing it by format specifier %f which belongs to float type of variable. Type casting requires (float)volume. In C programming it happens a lot because compiler dependency comes in picture.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
This program is supposed to input a number and its square value, then tell me if right or wrong. I have some problems but I can't figure out what they are.
#include <stdio.h>
#include <math.h>
int main()
{
float P;
float q;
float r;
printf("Enter the value of p\n");
scanf("%f",p);
q= p*p;
printf("Enter the square value of %f \n",p);
scanf("%f",r);
if (r = q){
printf("You are right\n");
}
else{
printf("you are wrong\n");
}
return 0;
}
so tell me my mistakes
Please compile the program with flags -Werror -Wall -Wextra although the first mistake is always a compilation error (typo): replace float P; with float p; because C is case-sensitive.
Then you need to pass the address of a variable to scanf, these two lines
scanf("%f",r);
...
scanf("%f",p);
should be
scanf("%f",&r);
...
scanf("%f",&p);
Lastly, there is a syntax error where you test for equality with
if (r = q)
but this changes r and tests if it is non-0. With an integer type you should use
if (r == q)
but with floating point types, equality tests don't work well, please see why in this question.