This question already has answers here:
Is floating point math broken?
(31 answers)
exact representation of floating points in c
(9 answers)
Comparing actual float value to a float variable in C
(3 answers)
Closed 5 years ago.
I got a question to find the output of below code snippet
int main(int argc, char const *argv[])
{
float a=0.7;
if(a<0.7)
std::cout<<"yes";
else
std::cout<<"no";
}
At first look, like everyone my answer was "no" because 0.7 is not less than 0.7. But when i run it, output was "yes". I found an explanation about this which says C++ internally takes 0.7 as double value and here actually it is checking whether 0.700 < 0.7ff which will be true. So i tried the same with another value 0.5. But then this program had "no" as output. Why it is so? Doesn't it really check whether 0.500<0.5ff?, or all my understanding about it is wrong?
Related
This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
return value of pow() gets rounded down if assigned to an integer
(4 answers)
Is floating point math broken?
(31 answers)
Strange behaviour of the pow function
(5 answers)
Closed 6 months ago.
So in code i need to check wheather a no is perfect square or not ,
bool issquare(int x){
int root=(int)pow(x,0.5);
int y=pow(root,2);
if(y==x){
return true;
}
return false;
}
Now,
issquare(25);
is returning false. I tried debugging and this is weird,
root:5
x:25
y:24
why is y=24?.
This question already has answers here:
Is there a functional difference between "2.00" and "2.00f"?
(6 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
When I compile and run the following code, x only gets printed when I initialize float x=1.5. It does not work for other values, for example, when I initialize float x=1.2 and write when(x==1.2), it does not print the value of x.
#include<stdio.h>
int main()
{
float x=1.5;
while (x==1.5)
{
printf("\n%f",x);
x-=1;
}
}
However, when I initialize float x=1.2f and write when(x==1.2f), the code runs as intended.
Also, if I declare and initialize x as double x=1.2, and write printf("\n%lf",x);, the code runs as intended.
This happens in both C and C++.
It tells the computer that this is a floating point number. If there is no f after the number, it is considered a double or an integer (depending on if there is a decimal or not).
3.0f -- float
3.0 -- double
3 -- integer
This question already has answers here:
What is the most effective way for float and double comparison?
(34 answers)
Closed 6 years ago.
This post stated that you shouldn't compare floating point variables with == because of rounding errors. What should I use then, and when?
You could use something along the lines of
if (abs(result - expected) < 0.00001)
or for a relative, rather than absolute, error:
float relativeError = abs((A - B) / B);
if (relativeError <= maxRelativeError)
See this for more details.
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 8 years ago.
when i try to calculate any expression, I always get an integer result, it's like:
float k= 5/12;
std::cout<< k<<std::endl;
the output in the console is always 0.
In C/C++, this is an integer division:
5/12
What you want is a floating point division:
5.0/12.0
Please note that this has absolutely nothing to do with GLUT or OpenGL.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
Why when I use multiplication
float a= 1.0500000f;
float b= a*100.0f;
why b is 104.99999 but not 105.0 ?
and when I
int f= (int)b;
f is 104
Floating point numbers are not infinitely accurate -- the wikipedia page on Float numbers is quite interesting to read.
On your second question: (int)b truncates anything that occurs after the comma. This means that, in your case, 104.99999 becomes 104. So when you create a rounding error, and then cast it to an integer, you are indeed running the risk of getting a lower number.