Bugs
-
- Posts: 4313
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Bugs
If I suspect that one exists, how do I report it?
Re: Bugs
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 ?
-
- Posts: 4313
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Bugs
@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.
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.
Re: Bugs
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).
So a conversion overflow warning is applied as for any other numeric datatype (Byte, Short, ...) only at compile time (only for a literal value).
Re: Bugs
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.
Re: Bugs
CBOOL(x) is similar to CAST(BOOLEAN, x)
Dim As Byte b = Cast(Byte, 256) also induces a warning.
Dim As Byte b = Cast(Byte, 256) also induces a warning.
Re: Bugs
OK, then I propose the cbool() function be redefined to something much more simple (example from SharpBASIC library):
Without cast, it converts any numerical expression result or value (whatever eax holds) to 0 or -1.
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
Last edited by Munair on Feb 23, 2022 8:21, edited 2 times in total.
Re: Bugs
IMO, Cast() performs the same operation as CBool(), i.e. intended conversion. For example when casting UByte to Byte, no warning is given:
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:
So why insist on constant values?
Code: Select all
Dim As Byte b = Cast(uByte, 255)
print b ' -1
Code: Select all
dim x as integer = 256
Dim As Byte b = Cast(Byte, x) ' no warning
Re: Bugs
@Munair
How I'm converting to boolean in gas64 , no jump :
Always many ways to go to Roma.
How I'm converting to boolean in gas64 , no jump :
Code: Select all
cmp edx, 0
setne al
neg al
movsx eax, al
Re: Bugs
Thanks SARG!
I just tested your code (replacing mine in the lib). Indeed, same result and slightly faster.
Result:
I just tested your code (replacing mine in the lib). Indeed, same result and slightly faster.
Code: Select all
_sb_cbool:
cmp eax, 0
setne al
neg al
movsx eax, al
ret
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.
Re: Bugs
@deltarho:
Code: Select all
#define IsTrue(x) iif(x <> 0, -1, 0)
#define IsFalse(x) iif(x <> 0, 0, -1)
Re: Bugs
Because is not a test at run-time but only a test at compilation-time.Munair wrote: ↑Feb 23, 2022 7:11So 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.
(this test only works with a literal value passed to CBOOL)
Re: Bugs
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.