strange output in comparison of float with float literal
This happens because in your statement
if(f == 0.7)
the 0.7 is treated as a double. Try 0.7f to ensure the value is treated as a float:
if(f == 0.7f)
But as Michael suggested in the comments below you should never test for exact equality of floating-point values.
Strange output when comparing same float values?
Floating point numbers are never accurate.
This statement is wrong. Some floating point numbers are accurate, such as 1.0, 12345.0, 12345.5, -2.25. All these numbers can be represented as integers didived by a power of 2. All numbers that cannot also are not accurate.
In your specific case, float x = 0.5
results in x
having the value 1.00000000 * 2^-1
. When you compare this against double 0.5
, both operands are converted to double
, so the comparison becomes 1.000000000000000 * 2^-1 == 1.000000000000000 * 2^-1
, which succeeds.
For float x = 0.1
, it looks different. The value is stored as 1.01010101 * 2^-3
(or similar). Note that this is already not precise. When you compare this against double 0.1
, the float is extended with zeros at the end, the comparison becomes 1.010101010000000 * 2^-3 == 1.010101010101010 * 2^-3
, which fails.
C : Strange error when using float and double [duplicate]
No guarantee that the result will be false and true, but the basic idea is pretty simple: 3.4
has type double. When you assign it to a float
, it'll get rounded. When you compare, that rounded number will be promoted back to a double, not necessarily the same double as 3.4
.
In the second case, everything's double throughout.
Why is the Output of This code , BYE.. and not HI [duplicate]
floating points numbers cannot be represented precisely. so you cannot equate two float directly. it differs normally with epsilon.
if(fabs(floatVarialbe-expectedValue) < **FLT_EPSILON**)
{
//statements to execute if they are equal
}
you should fabs function to get absolute value [remove negative sign], its defined in math.h library.
FLT_EPSILON is a relative error and its defined in float.h library
Due to rounding errors, most floating-point numbers end up being
slightly imprecise. As long as this imprecision stays small, it can
usually be ignored. However, it also means that numbers expected to be
equal (e.g. when calculating the same result through different correct
methods) often differ slightly, and a simple equality test fails.
if you want to know more, What Every Computer Scientist Should Know About Floating-Point Arithmetic
Why is there no output for this program? [duplicate]
Computers only store digital information. Integers can be represented accurately in binary, but floating point numbers are approximated.
It seems that in the approximations, additional tiny values are preventing your exact comparison from being true.
Now is a good time to google "what every programmer should know about floating point numbers" and read it. It will save you countless hours of future programming and debugging if you learn it early.
In addition, there are two floating point types "float" and "double". You are comparing a float to a double, so the approximations of the value are likely not the same approximation, creating more of a chance that the two values won't be equal.
comparing float values with double resulting different behaviour [duplicate]
x == 0.1
0.1
is not of type float
but of type double
. float
and double
don't have the same precision. 0.1f
is of type float
.
Why it works with 0.5
is because 0.5
has an exact representation in both float
and double
(in binary IEEE-754) types.
Related Topics
How to Read Unknown Number of Inputs
Most Efficient Way of Copying a Raw Byte Array into an Empty Byte Vector
Can a Local Variable'S Memory Be Accessed Outside Its Scope
Difference Between #Include ≪Filename≫ and #Include "Filename"
Why Should I Prefer to Use Member Initialization Lists
Why Is Volatile Not Considered Useful in Multithreaded C or C++ Programming
Usr/Bin/Ld: Cannot Find -L≪Nameofthelibrary≫
C++ Sorting and Keeping Track of Indexes
C++ Convert String to Hexadecimal and Vice Versa
Why Should I Not #Include ≪Bits/Stdc++.H≫
What Are All the Common Undefined Behaviours That a C++ Programmer Should Know About
How to Get the Cpu Cycle Count in X86_64 from C++
What Are the Advantages of List Initialization (Using Curly Braces)