SphereServer BugTracker - SphereServer
View Issue Details
0002246SphereServerexecutable - genericpublic27-11-13 03:0330-11-13 20:19
Stanic 
RanXerox 
normalminoralways
closednot fixable 
16-06-2013, 0.56b Prerelease 
 
Automated (specify build number)
None
None
0002246: UVAL
I was trying to use UVAL (evaluates an expression and returns the result as an unsigned integer) but it don't work, returning a random number.

[FUNCTION PlusTest]
SYSMESSAGE <UVAL 1-2>

Result: 4294977295
No tags attached.
Issue History
27-11-13 03:03StanicNew Issue
29-11-13 17:58CorujaNote Added: 0001784
29-11-13 23:05StanicNote Added: 0001786
30-11-13 04:27CorujaNote Added: 0001787
30-11-13 20:15RanXeroxNote Added: 0001788
30-11-13 20:19RanXeroxNote Added: 0001789
30-11-13 20:19RanXeroxStatusnew => closed
30-11-13 20:19RanXeroxAssigned To => RanXerox
30-11-13 20:19RanXeroxResolutionopen => not fixable

Notes
(0001784)
Coruja   
29-11-13 17:58   
maybe unsigned values doesnt accept negative numbers, it will result a huge value ([2^-32]-1 or something like this) so it returns the max allowed value which is 4294967295 ([2^32]-1)
(0001786)
Stanic   
29-11-13 23:05   
Please Coruja... Your comment have no sence!
If the function is UVAL (unsigned integer), the negative numbers will be transformed to positive oO
(0001787)
Coruja   
30-11-13 04:27   
I think unsigned int will result an error if you try to solve a value lower than min (1), higher than max (32bit char) or which is not a integer number

and in case of error it wont return a random number, but the max 32bit value for this number x using [2^32]-x


testing UVAL -1, -10, -100 and -1000 it will return something like this

UVAL -1 = 2^-1 = 0,5 <- error: this value is not a integer number and it is < 1
so it will return the max 32bit value based: [2^32]-1 = 4294967295

UVAL -10 = 2^-10 = 0,0009765625 <- error: this value is not a integer number and it is < 1
so it will return the max 32bit value based: [2^32]-10 = 4294967286

UVAL -100 = 2^-100 = 7,8886090522101180541172856528279e-31 <- error: this value is not a integer number and it is > 32bit
so it will return the max 32bit value based: [2^32]-100 = 4294967196

UVAL -1000 = 2^-1000 = 9,3326361850321887899008954472382e-302 <- error: this value is not a integer number and it is > 32bit
so it will return the max 32bit value based: [2^32]-1000 = 4294966296

so I think it's not really a bug, but the default behavior for unsigned int calculation. Maybe to prevent wrong results the value (decimal) must be checked before to match these conditions
-value must be between min and max (1 ~ 4294967295)
-value must be a valid integer number
(0001788)
RanXerox   
30-11-13 20:15   
It is not a bug. What you are seeing when you pass negative numbers into UVAL is the conversion/promotion of a signed number to an unsigned number - both of which are limited by the fact they are stored as a DWORD (a 32 bit integer). This behavior is defined in the C99 specification:

    C99 6.3.1.3-p2

    ...if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

which essentially means "add (UINT_MAX+1)".

For example, on a typical 32 bit system, UINT_MAX is 0xFFFFFFFF. Now suppose you want to convert the following:

int ival = -1;
unsigned int uval = (unsigned int)ival;

According to the standard, ival will be promoted to unsigned int by doing the following:

uval = (UINT_MAX+1) + (-1);

Which of course, results in uval = UINT_MAX;. This is defined by the standard.
(0001789)
RanXerox   
30-11-13 20:19   
Some more details on this topic can be found on the wiki at http://wiki.sphere.torfo.org/index.php/Bitwise_Operations [^]