Bugs

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Bugs

Post by deltarho[1859] »

If I suspect that one exists, how do I report it?
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Bugs

Post by fxm »

deltarho[1859] wrote: Feb 23, 2022 5:42 In fact, we get this warning with CBool(10).

The manual has an example: CBool(1) which is OK.

I think we have a bug. :)

For CBOOL behavior with constant different from 0, -1, +1 (overflow conversion warning)

For me this is not a bug but wanted for now.
Excerpt from the BOOLEAN documentation page:
Notes on definition of boolean data type:
- Ideally, the definition of the boolean data type is that it holds the value of True or False, and that's it. However, to make this concept a reality, we need a definition that uses real world connections.
- A more realistic definition is that the boolean data type is a 1-bit integer, having the value 0 to indicate False and 1 to indicate True.
- For a practical definition, we must consider, yet again, additional factors. The most significant factor is that the hardware (processor) on which code is executed does not directly support a 1-bit data type; the smallest register or memory size we can work with is 8-bits or 1-byte.
- Assume "false" is 0 in both C/C++ and FB. C/C++ has logical 'not' operator '!' such that '!0' produces '1'. FB has a bitwise Not operator such that 'not 0' produces '-1'.
- Nevertheless the definition under the hood for a FB boolean remains an 1-bit integer, zero extended to fill larger integer types.
- Therefore when assigning a boolean with an integer value (by implicit conversion and not with the False or True value), '0' induces the False state and '1' or '-1' induces the True state (any other value also induces the True state, but with a warning message).
- Otherwise when assigning a numeric type with a boolean (by implicit conversion), False induces the '0' value and True induces the '-1' value.
- However, the purpose and intent of the boolean data type remains, that it should only ever hold a True value or False value, regardless of the underlying details.

Maybe a reaction from @Jeff first ?
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Bugs

Post by deltarho[1859] »

@fxm

I read your excerpt several times, but “(any other value also induces the True state, but with a warning message)” did not sink in. :)

CBool: Converts numeric or string expression to a boolean (Boolean)

Anyway, it seems silly to me to issue a warning when a True will be induced.

And as for CBool(10) - is 10 a numeric expression or not? Erm .....

It looks like my advice is OK – ignore the warning. :)

Thanks, fxm.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Bugs

Post by fxm »

The definition under the hood of a Boolean is an 1-bit integer.
So a conversion overflow warning is applied as for any other numeric datatype (Byte, Short, ...) only at compile time (only for a literal value).
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Bugs

Post by Munair »

fxm wrote: Feb 23, 2022 6:41 The definition under the hood of a Boolean is an 1-bit integer.
That should not apply to a function converting a numeric expression to boolean. IOW, Passing 10 to cbool() should not be hindered by the definition of a boolean, but rather converting it to it. Even more so when: dim a as byte = 10: print cbool(a) does not give a warning.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Bugs

Post by fxm »

CBOOL(x) is similar to CAST(BOOLEAN, x)
Dim As Byte b = Cast(Byte, 256) also induces a warning.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Bugs

Post by Munair »

OK, then I propose the cbool() function be redefined to something much more simple (example from SharpBASIC library):

Code: Select all

_sb_cbool:
    push    edx
    push    eax
    mov     eax, 0
    pop     edx
    cmp     edx, eax
    je      .__L0
    mov     eax, -1
  .__L0:
    pop     edx
    ret
Without cast, it converts any numerical expression result or value (whatever eax holds) to 0 or -1.
Last edited by Munair on Feb 23, 2022 8:21, edited 2 times in total.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Bugs

Post by Munair »

fxm wrote: Feb 23, 2022 7:08 Dim As Byte b = Cast(Byte, 256) also induces a warning.
Of course it does, an so does Dim As Byte b = Cast(UByte, 256), which is a correct warning.

But booleans do not have a range, so ANY number - if so intended - should be converted to 0 or -1 without warning.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Bugs

Post by Munair »

IMO, Cast() performs the same operation as CBool(), i.e. intended conversion. For example when casting UByte to Byte, no warning is given:

Code: Select all

Dim As Byte b = Cast(uByte, 255)
print b  ' -1
I think FB should at least make an exception for Cast(BOOLEAN, x) because no other datatype will fit a 1-bit datatype. Even more so because the compiler does not do a range-check on a variable:

Code: Select all

dim x as integer = 256
Dim As Byte b = Cast(Byte, x) ' no warning
So why insist on constant values?
SARG
Posts: 1755
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Bugs

Post by SARG »

@Munair
How I'm converting to boolean in gas64 , no jump :

Code: Select all

cmp edx, 0
setne al
neg al
movsx eax, al
Always many ways to go to Roma. ;-)
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Bugs

Post by Munair »

Thanks SARG!

I just tested your code (replacing mine in the lib). Indeed, same result and slightly faster. :D

Code: Select all

_sb_cbool:
    cmp      eax, 0
    setne    al
    neg      al
    movsx    eax, al
    ret
Result:

Code: Select all

main do
  print(cbool(10 * 5 + 3)); ' -1
  print(cbool(10 * 5 + 3 - 53)); ' 0
end
Last edited by Munair on Feb 23, 2022 9:18, edited 2 times in total.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Bugs

Post by Munair »

@deltarho:

Code: Select all

#define IsTrue(x) iif(x <> 0, -1, 0)
#define IsFalse(x) iif(x <> 0, 0, -1)
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Bugs

Post by fxm »

Munair wrote: Feb 23, 2022 7:11
fxm wrote: Feb 23, 2022 6:55 So a conversion overflow warning is applied as for any other numeric datatype (Byte, Short, ...) only at compile time (only for a literal value).
So why does dim a as byte = 10: print cbool(a) not give a conversion warning? It would seem more appropriate here with an explicit data type than with a numerical constant (intentionally) without explicit datatype. The warning is hard to understand, especially for beginners and doesn't really serve a practical purpose IMO.
Because is not a test at run-time but only a test at compilation-time.
(this test only works with a literal value passed to CBOOL)
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Bugs

Post by Munair »

fxm wrote: Feb 23, 2022 9:43 Because is not a test at run-time but only a test at compilation-time.
(this test only works with a literal value passed to CBOOL)
Yes, I understand that, but from an end user's (source code) point of view the distinction isn't logical. So either the test should be extended to run-time or it should not be tested at all. Just my opinion.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Bugs

Post by fxm »

Munair wrote: Feb 23, 2022 10:28 So either the test should be extended to run-time or it should not be tested at all. Just my opinion.
The same principle of tests (only at compile-time) are also applied to the initialization of all numeric variables (Byte, Short, Long, ...)
Post Reply