Float bug?

Everything else that doesn't fall into one of the other PB categories.
mike74
User
User
Posts: 60
Joined: Mon Nov 21, 2005 1:44 pm

Float bug?

Post 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
tomijan
Enthusiast
Enthusiast
Posts: 107
Joined: Sun Dec 11, 2005 1:32 pm

Post 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
mike74
User
User
Posts: 60
Joined: Mon Nov 21, 2005 1:44 pm

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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.
mike74
User
User
Posts: 60
Joined: Mon Nov 21, 2005 1:44 pm

Post 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?
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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.
mike74
User
User
Posts: 60
Joined: Mon Nov 21, 2005 1:44 pm

Post by mike74 »

Ok, thank you, Fred.
Post Reply