This question already has answers here:
strange output in comparison of float with float literal
(8 answers)
Is floating point math broken?
(31 answers)
Closed 5 years ago.
Why the output is not as expected?
#include <stdio.h>
void main(){
float a,b,c;
b=0.7;
if( b<0.7 )
printf(" It should NOT be here");
else
printf("It Should be here");
}
0.7 is double value. Try 0.7f in code. It should work.
Floating point numbers
have limited accuracy
some value doesn't exist. 0.7 is such value. On my platform is
0.699999988.
Reason: we write numbers as decimal, but internal are realised as binary. Many longer materials exist
https://en.wikipedia.org/wiki/Floating-point_arithmetic#Representable_numbers.2C_conversion_and_rounding
So behaviour You are surprised
Please Try the below Code, it works!!!:
Code
#include <stdio.h>
int main(void)
{
float a,b,c,temp;
temp=0.7;
b=0.7;
if( b<temp )
printf(" It should NOT be here");
else
printf("It Should be here");
}
Related
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:
strange output in comparison of float with float literal
(8 answers)
Is floating point math broken?
(31 answers)
Closed 2 years ago.
For float value the output is coming"okNOT OK", It should have shown "okOK". But with other datatypes it is working fine. Why is it hapenning?
#include<stdio.h>
int main()
{
float a=0.5, b=0.7;
if(a==0.5)
printf("ok");
else
printf("not ok");
if(b==0.7)
printf("OK");
else
printf("NOT OK");
return 0;
}
By default, decimal values are double, which means you compare 0.7f (float) with 0.7 (double). These values are different, as double uses more bits to save data about the number itself. To fix your problem, change your if statements to:
if(a == 0.5f)
...
if(b == 0.7f)
...
This way you will compare float a, b with corresponding float values.
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:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I have written this piece of code in my computer and the result is 7 instead of 8 (the correct result ... I think).
I don't know why... Can somebody help me?
#include <stdio.h>
int main() {
int num;
num = (68/10.0 - 68/10)*10;
printf("the result %d", num);
return 0;
}
double typically represents exactly about 264 different numbers. 68/10.0 is not one of them,
As a binary64, 68/10.0 is about
6.7999999999999998223643161..., the closest value to 6.8 that is a multiple of a dyadic rational. # AntonH
68/10 is an integer division with a quotient of 6.
(68/10.0 - 68/10)*10 is thus about 7.9999999999999982236431606...
Assigning that to an int is 7 not 8 as the fraction is discarded even though it is very close to 8.
When converting a floating point value consider round to the the closest, rather than truncating.
num = lround((68/10.0 - 68/10)*10);
This question already has answers here:
Division result is always zero [duplicate]
(4 answers)
Closed 8 years ago.
This is my code, please help me out, percentage showing 0.00 instead of what i want.
I want to calculate percentage, as you will know this by code below...
#include<stdio.h>
#include<conio.h>
int main()
{
int marks[2][3]={80,70,50,90,50,60};
int total[2]={0,0};
float per[2]={0.0f,0.0f};
for (int x=0;x<2;x++)
{
for(int y=0;y<3;y++)
{
printf("[%d][%d]=%d\t",x,y,marks[x][y]);
total[x]=total[x]+marks[x][y];
per[x]=total[x]/300*100;
}
printf("total [%d]=%d",x,total[x]);
printf("\n\npercentage [%d]=%2.2f \n",x,per[x]);
putchar('\n');
}
getch();
return 0;
}
In the expression
total[x]/300*100
all involved values are integers, so the result is truncated before the assignment to the floating point array entry.
Change to e.g.
total[x]/300.0f*100.0f
Replace per[x]=total[x]/300*100; with per[x]=total[x] * 1.0f / 300 * 100;
You need to convert int to double / float before division to make sure you don't loose the precision because of truncation of integer division.
per[x]=total[x]/300*100; /* Assuming total[x] = 280 */
per[x]=280/300*100;
per[x]=(280/300)*100; /* Associativity is left-to-right */
per[x]=0*100;
per[x]=0;
You may also want to read integer division in C and operator associativity