Page 1 of 1

0.5 != 0.5000000000 ??? why ??

Posted: Thu Jul 07, 2011 19:40
by belkacem
hello again!!

my problem this time is not programming problem ( i think it is Boolean one )

i create a tab button and tab control without any problem but when i resize it i do two value
0.5 and 0.500 , but imagine what happen
if i use 0.5 i get very small size in other hand i use 0.500 i get big size

and you know that in math 0.5 == 0.50000000000000 ( even how zero that i have )

any help !!

Re: 0.5 != 0.5000000000 ??? why ??

Posted: Mon Jul 11, 2011 18:42
by Jamarr
This does not make any sense. Please provide the relevant code and screenshots.

Re: 0.5 != 0.5000000000 ??? why ??

Posted: Tue Jul 12, 2011 11:25
by Mikademus
belkacem wrote:hello again!!
and you know that in math 0.5 == 0.50000000000000 ( even how zero that i have )


Unfortunately, for computer floating points this is not true. Basically, you never compare floats or doubles for equality (with ==), that will result in bugs, always compare the difference of two floats against a comparison criterion (usually called "epsilon"), as in

Code: Select all

const float epsilon = 0.000001f;
float a = 100.f;
float b = 50.f;
//wrong!: if (a - b == 50.f) doStuff();
if (a - b < epsilon) doStuff(); // correct


Note that this is beginner's programmers knowledge and you should already have been aware of it. It may or may not have anything to do with your widget sizing problem.

Re: 0.5 != 0.5000000000 ??? why ??

Posted: Tue Jul 12, 2011 11:29
by Kulik
Mikademus wrote:

Code: Select all

if (a - b < epsilon) doStuff(); // correct



should be:

Code: Select all

if (std::fabs(a - b) < epsilon) doStuff(); // correct


And you can use std::numeric_limits<float>::epsilon() as epsilon.

Still, I think this could be a parsing problem, not necessarily a floating point precision problem.

Re: 0.5 != 0.5000000000 ??? why ??

Posted: Tue Jul 12, 2011 17:20
by Jamarr
i create a tab button and tab control without any problem, but when i resize it i do two value 0.5 and 0.500, but imagine what happen: if i use 0.5 i get very small size, in other hand i use 0.500 i get big size.


I am pretty sure he is not talking about comparing two floats, despite the title of this thread. This is why I asked for more info ;)