Bug with Bool operation in PB6.21 ASM and C-Backend

Post bugreports for the Windows version here
SMaag
Enthusiast
Enthusiast
Posts: 324
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Bug with Bool operation in PB6.21 ASM and C-Backend

Post by SMaag »

Code: Select all

EnableExplicit

Macro IIFn(_Expression_, _TruePart_, _FalsePart_)
  ; Bool(Not _Expression_) * _FalsePart_ + Bool(_Expression_) * _TruePart_
  Bool(_Expression_) * _TruePart_ + Bool(Not _Expression_) * _FalsePart_
 EndMacro
 
 Debug IIFn(1=0, 11.1, 10) ; should be 10, gives 0.0!
 
 Define res.d
 
 res = IIFn(1=0, 11.1, 10) ; should be 10, gives 0.0!
 Debug res
 
 Define.d a=11.1, b=10
 
  res = IIFn(a=b, a, b) ; should be 10, gives 0.0!
 Debug res

It is a bug! Try both different lines in the IIFn Macro.
The result is different!

here is the C-Output.
You can see that the Compiler precalculate the result and set it to 0.0 - that's wrong.
The last (a=b, a, b) with variables is processed correct.
So the bug is in the precalculation of the expression in the compiler!

Code: Select all

SYS_InitPureBasic();
// EnableExplicit
// 
// Macro IIFn(_Expression_, _TruePart_, _FalsePart_)
// Bool(Not _Expression_) * _FalsePart_ + Bool(_Expression_) * _TruePart_
// 
// EndMacro
// 
// Debug IIFn(1=0, 11.1, 10) 
// 
// Define res.d;
// 
// res = IIFn(1=0, 11.1, 10) 
v_res=0.0;			; here the compiler sets a 0.0 what is impossible!
// Debug res
// 
// Define.d a=11.1, b=10
v_a=11.0999999999999996447286321199499070644378662109375;
v_b=10.0;
// 
// res = IIFn(a=b, a, b) 
int r0=((!((v_a==v_b)))?1:0);
int r1=(((v_a==v_b))?1:0);
v_res=((double)((double)r0*v_b)+((double)r1*v_a));
// Debug res
// 
SYS_Quit();
}
User avatar
Piero
Addict
Addict
Posts: 916
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Bug with Bool operation in PB6.21 ASM and C-Backend

Post by Piero »

The problem seems to be the Not with a numeric expression like #True and Floats/Doubles…

Code: Select all

Macro IIFn(e,t,f)
   Debug t*Bool(e)+f*~Bool(e)&1
EndMacro

Macro bug(e,t,f)
   Debug t*Bool(e)+f*Bool(Not e)
EndMacro

IIFn(1=1,11.1 ,10) ; 11.0999999999999996
bug (x=0,11.1 ,10) ; 11.0999999999999996
bug (1=1,11   ,10) ; 11
bug (1=1,11.1 ,10) ; 21.1000000000000014
PS: Also happens on Mac M1… not a big issue, but can cause head-scratching when "forcing an expression" to test/debug
Post Reply