Page 1 of 1

Float bug?

Posted: Mon Apr 03, 2006 5:35 pm
by mike74
Why do these two pieces of code produce different results?

Code: Select all

test.f=0.15
For x=1 To 5
z.f = x
If test = z*0.05: MessageRequester("", "OK"): endif
next x
end

Code: Select all

test.f=0.15
For x=1 To 5
z.f = x
z*0.05
If test = z: MessageRequester("", "OK"): endif
next x
end

Posted: Mon Apr 03, 2006 6:55 pm
by tomijan
look in help - Variables and types
Storing numbers such as 0.11 are more difficult and may be stored as a number such as 0.10999999. You can try to display to only a limited range of digits, but do not be surprised if the number displays different from what you would expect!
Try like this

Code: Select all

x.f = 0.11
debug f
I think you should use rounded floating point to get acceptable (and repetitive ) result, or i'm wrong?

tom

Posted: Mon Apr 03, 2006 8:59 pm
by mike74
tomijan,

Thanks for replying but the code I posted doesn't involve displaying values. It seems to me that the value that "test" is being compared to should be the same in both cases (z*0.05). I don't understand why it would be different.

Posted: Tue Apr 04, 2006 10:57 am
by Trond
This looks strange.

Code: Select all

test.f = 0.15

For x = 1 To 5
  z.f = x 
  If test = z*0.05
    Debug "OK"
  Else
    If cmp(z*0.05, test)
      Debug "OK in procedure?"
    EndIf
  EndIf
Next

Posted: Tue Apr 04, 2006 11:00 am
by Fred
You can not compare the result of an expression (stored in 80 bits float precision) with the result already casted to float (32 bits precision) as it will be probably a bit rounded. Equalities with float are not accurate.

Posted: Tue Apr 04, 2006 1:22 pm
by mike74
Fred wrote:You can not compare the result of an expression (stored in 80 bits float precision) with the result already casted to float (32 bits precision) as it will be probably a bit rounded. Equalities with float are not accurate.
I suppose that's one for the wishlist, then.

Are equalities with quad and double accurate?

Posted: Tue Apr 04, 2006 1:45 pm
by Fred
It's a more general langage problem, even in C you got the problem. You can still use a procedure to 'cast' it correctly:

Code: Select all

Procedure.f Float(a.f)
  ProcedureReturn a
EndProcedure

test.f=0.15
For x=1 To 5
z.f = x
If test = Float(z*0.05): MessageRequester("", "OK"): EndIf
Next x
End
If you want, you can try this in C:

Code: Select all

void main()
{
  float float1 = 0.15;
  float float2 = 1;

  if (float1 == float2*0.15)
    printf("OK1\n");

  if (float1 == (float)(float2*0.15))
    printf("OK2\n");
}
The OK1 will never display.

BTW, the double will probably suffer from similar problem, but not quad.

Posted: Tue Apr 04, 2006 2:05 pm
by mike74
Ok, thank you, Fred.