+ 1

we can't compare float and double values??

#include<stdio.h> void main () { float s= 1.1; double e=1.1; if (s==e) pf ("compared"); else pf ("not compared"); } I'M getting lossy correction error like that....... wt is the correct form to compare float and double values it can b possible or it can't b compared??? I'm in seek of ans......😊

28th Apr 2017, 6:16 PM
Zameer shaikh
Zameer shaikh - avatar
3 Respostas
+ 12
double has more digits so it may contain the value 1.0000000...1, with more 0's than the float. In order to compare float and double, take the absolute value of their difference and see if it's less than epsilon to see if they're 'equal'. Though, as the others said, it may be better to just cast. 0.1 (or 1/10) is represented as 0.000110011...etc in binary, so it likely will not store '0.1'. In short, floating point numbers are not accurate. As for why you're getting lossy conversion error, Idk, pretty sure double is the default decimal value in c++. Maybe add an 'f' after 0.1 in the float?
28th Apr 2017, 6:33 PM
Rrestoring faith
Rrestoring faith - avatar
+ 11
You have to convert the type of one of them, to compare the same type
28th Apr 2017, 6:32 PM
Emore Anzolin
Emore Anzolin - avatar
0
Use static_cast<float>(variable)
28th Apr 2017, 6:36 PM
aklex
aklex - avatar