Float variable comparison fails [duplicate] - c

This question already has an answer here:
Why does this program gives no output for float and double datatypes?
(1 answer)
Closed 7 years ago.
The below program seems to look like it will run for one time but when i run in Turbo C , the output is nothing.
Can any one explain this ?
#include<stdio.h>
int main()
{
float x=1.1;
while(x==1.1)
{
printf("%f \n",x);
x=x-0.1;
}
return 0;
}

By default, floating point numbers are stored as type 'double'. So, a comparison on float and double value is done.
I think,
if(x==1.1f)
it should solve the problem.
And also FLT_EPSILON is the smallest difference between two floating point numbers for them to be same.
if( abs(x-1.1f) <= FLT_EPSILON)
should work

You can re-write your loop condition as follows-
while((1.0009<x)&&(x<1.10001))
As because x=1.1 in this x is never exact 1.1. At higher decimal places it can have different value.
You can see here at higher decimal places what are values and working example for your code -https://ideone.com/IgrLAY

Related

Why do the square of input decreases by one when using pow function? [duplicate]

This question already has answers here:
unusual output from pow
(3 answers)
pow() function in C problems [duplicate]
(1 answer)
Why the result of pow(10,2) 99 instead of 100? [duplicate]
(1 answer)
What's wrong with long? Why is subtracting 1 automatically?
(1 answer)
Closed 2 years ago.
Here is a simple C program, which accepts a number from the user and results it's square.
#include <stdio.h>
#include <math.h>
int main()
{
int number;
int result;
printf("\nEnter the number\n");
scanf("%d",&number);
result=(pow(number,2));
printf("\nThe result is %d\n",result);
return 0;
}
The problem is, whenever i enter 5,25,26 etc as input, the output is 24,624,675 i.e. it decreases by 1 and this does not happen with all numbers. I am using CodeBlocks IDE. I figured out a fix for this problem but I want to know what is happening behind the scene, which is causing this error.
From this post, Sourav tells us:
pow() takes double arguments and returns a double.
If you store the return value into an int and print that, you may not get the desired result.
As for an explanation on why pow() is returning 1 less that you expect, see this post. Specifically:
pow() works with double numbers. These represent numbers of the form s * 2^e where s is a 53 bit integer. Therefore double can store all integers below 2^53, but only some integers above 2^53. In particular, it can only represent even numbers > 2^53, since for e > 0 the value is always a multiple of 2.

What wrong with my C code? Is it a compiler bug in CodeBlock 16.01? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I am using Code::Block 16.01, the current version, that come with compiler. The problem is when I change xMax to 1.2, the result does not change. It produce the same result as xMax=1.1. Did I do something wrong with my C code? Or is this a compiler problem? Here is my MWE:
#include<stdio.h>
int main()
{
double xMin=1.0;
double xMax=1.1;
double x=xMin;
double h=0.1;
while(x <= xMax)
{
printf("x=%f\n",x);
x=x+h;
}
return 0;
}
You have a float point precision problem. Since 1.2 cannot be represented exactly in the binary form, there is a precision loss. Your code should work if you change 1.2 to something bigger, let's say, 1.201
In general, please try to avoid = in float point comparisons.

Floating point number is rounding off in C [duplicate]

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 6 years ago.
i started learning c. Today, while i am working on a program, i found an interesting thing and i made another small program (similar to my issue) to check it.
#include<stdio.h>
int main(void)
{
float num1=867.0;
float num2=.6921;
printf("sum = %.4f \n",num1+num2);
return 0;
}
if i run the above program, i am getting 867.6921 as the answer. but if i changed the .4%f to %f the answer is changing to sum = 867.692078. why's the change in the output? Also, is there any way that i can get the answer without using the .4%f?
but if i changed the .4%f to %f the answer is changing to sum = 867.692078
printf rounds the result to that many digits after the floating point you specified, the default is .6. While rounding, it uses the current floating point rounding mode, the default is round to the nearest.
The float type has about seven digits of precision (can be anywhere from 6-9 digits). One alternative is to use g instead of f. This fixes the number of precise digits in the number, not just those after the decimal.
printf("sum = %.7g \n",num1+num2);
This produces output
sum = 867.6921
and will always give seven digits of precision for any input. Number formats would be like :
0.000000E+07
0000000
000000.0
00000.00
0000.000
000.0000
...etc

number hold by double container is greater than float even if values are equal [duplicate]

This question already has answers here:
Floating point comparison [duplicate]
(5 answers)
Closed 9 years ago.
main()
{
float f=0.7;
if(0.7>f)
printf("Hi");
else
printf("Hello"):
}
When I compile this program output comes out to be Hi.
Can someone please explain the scenerio how 0.7>0.7 is true? Is this due to fact that 0.7 used in if statement is a double and f is a float?
Even if it is double still its value is 0.7 difference created is just that in case of double it is stored in 8 byte while in case of float it is stored in 4 byte. But I think it does not matters how big is your container when the value stored in container is equal. So 0.7 can never be greater than f. So according to me it should result in Hello. then why Hi is the output?
The container does makes the difference here. The idea is how these values are stored and then retrieved. If you use a number that can be converted back to exact decimal value. You may see the difference.
Use this and see the output:
float f=0.7f;
If you define float f = 0.7, it is stored as manttisa and exponent. It depends on the precision. So in this case .7 will be less than .7. If you define 0.7f it will be equal to 0.7.

Why does this C computation involving exponents produce the wrong answer? [duplicate]

This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 9 years ago.
I am trying to implement the below formula on C
Here is my code:
int function(int x){
return pow(10, (((x-1)/(253/3))-1));
}
int main(void){
int z = function(252);
printf("z: %d\n",z);
return 0;
}
it outputs 10. However a calculator outputs 94.6.
could anyone explain me what I am doing wrong?
Note that in this line
(((x-1)/(253/3))-1))
You are dividing the integer value x - 1 by an integer value 253 / 3. This will truncate the value to an int, meaning that you'll be raising an integer power to an integer power.
To fix this, try changing this expression to
(((x-1)/(253.0 / 3.0))-1))
This now will use doubles in the expression, giving you the value you want.
Hope this helps!
Adding to that, integers do not give you the decimal part of numbers, so a number like 3.5 will get cut down to 3 using integer. To fix this double is the way to go. In other words 3 is different than 3.0

Resources