0
#include<iostream>
using namespace std;
int main(){
float a=1.11;
if(a==1.11){cout<<"yes";} else {cout<<"no";}
return 0;
}

Result:

no

Process exited after 0.2579 seconds with return value 0 Press any key to continue . . . Captured Result here

Appleshell
  • 7,088
  • 6
  • 47
  • 96

1 Answers1

2

Type 1.11f

#include<iostream>
    using namespace std;
    int main(){
    float a=1.11f;
    if(a==1.11f){cout<<"yes";} else {cout<<"no";} //here not
    return 0;
}
Attersson
  • 4,755
  • 1
  • 15
  • 29
  • Downvote away, it works. – Attersson May 19 '18 at 23:09
  • This is actually correct: floating point numbers are merely *counterintuitive*, not nondeterministic. – o11c May 19 '18 at 23:10
  • @Claies No, it's a type error: `(double)(float)1.11 != (double)1.11` – o11c May 19 '18 at 23:11
  • 1
    Most people forget that `1.11` has type `double`. – o11c May 19 '18 at 23:12
  • @Attersson it really worked... but whats the reason behind using "f" after the value.... – HAMZA PERVAIZ May 19 '18 at 23:14
  • @HAMZAPERVAIZ because converting `1.11` loses precision, then the `==` (which needs arguments of the same type) expands it with zeros instead, which doesn't match the unconverted value. – o11c May 19 '18 at 23:15
  • @HAMZAPERVAIZ it is a long story. You have to research on how floats are really stored and the precision. Even if you don't touch the value, floats and doubles have precision issues and every operation often adds a very small error. Whenever you tipe a literal or perform operations, remember to cast to the correct type or use the correct suffixes. – Attersson May 19 '18 at 23:17
  • @o11c: They can sure do a good imitation of nondeterminism, though. Most floating-point operations depend on the floating-point mode (even rounding) which is global state, so the compiler can't predict what mode will be in effect at runtime, and so compile-time computations may not match identical runtime computations (which in turn might produce different results if executed again after the mode is changed) – Ben Voigt May 19 '18 at 23:20
  • Let us say that pseudorandom is to random as nondeterminism is to floats (and doubles). So, even though not really, in practice, they can be considered so. But go figure. – Attersson May 19 '18 at 23:23
  • @BenVoigt if `FLT_EVAL_METHOD == 0` (which is usually the case these days), it happens pretty much as you expect. Positive values are also somewhat sensible, it's only the negative values that cause problems. – o11c May 19 '18 at 23:24
  • @o11c: Only if the compiler's function prologue fixes the floating-point control word (at least x87, SSE2, and ARM have them)... and I'm not aware of any compiler that does this (it's kinda expensive, not what you want to do on every function entry). So `FLT_EVAL_METHOD` really only tells you what operand size the compiler will use, and not the actual behavior of the floating point instructions. – Ben Voigt May 19 '18 at 23:57
  • Very nice knowledge. `FLT_EVAL METHOD`. Your comments have made this answer worth keeping despite the ignorant downvotes. Thanks for sharing. – Attersson May 20 '18 at 00:06
  • @BenVoigt Then it's as simple as saying "any program that *modifies* the control word is nonstandard. By far, the vast majority of programs never do, and libc startup ensures and *initial* sane state. – o11c May 20 '18 at 16:51