www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10129] New: problem with short numbers operator

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10129

           Summary: problem with short numbers operator
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: udefranettet gmail.com


--- Comment #0 from cbr <udefranettet gmail.com> 2013-05-21 12:11:12 PDT ---
Created an attachment (id=1215)
compile error for small number types

I would expect that it is allow to use the operator + and -.
on byte,ubyte,short and ushort numbers by dmd (and gdc) convert those to an
int.

The attach example gives the following complier error.
short_int_test.d(8): Error: cannot implicitly convert expression (cast(int)n +
cast(int)k) of type int to ubyte

When the Num alias is ubyte,byte,ushort and short.

So if we what to use those type we have to cast to an int isn't this a little
overkill.

So instead of 

 r=n+k;
We have to write.
 r=cast(Num)(n+k);
Where r,n,k is the to of Num.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 21 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10129


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc


--- Comment #1 from bearophile_hugs eml.cc 2013-05-21 12:21:19 PDT ---
I am compiling this program, and I don't see errors or warnings:


void main() {
    alias Num = uint;
    Num n = 12;
    Num k = 11;
    Num r;
    r = n - k;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 21 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10129


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |schveiguy yahoo.com
         Resolution|                            |INVALID


--- Comment #2 from Steven Schveighoffer <schveiguy yahoo.com> 2013-05-21
12:42:53 PDT ---
The CPU does math at an integer level.  So what happens is, short, ushort,
byte, and ubyte are integer promoted to int, then the operation is performed. 
After the operation, the number may or may not fit into the short, ushort,
byte, or ubyte.  So the compiler requires a cast so you can verify "yes, I know
I'm throwing away data."

This is working as expected.

(In reply to comment #1)
 I am compiling this program, and I don't see errors or warnings:
Right, the report states you have to change Num to byte, ubyte, short, or ushort. Probably the attachment should be one of those. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 21 2013