D arithmetic problem

Paul D. Anderson paul.d.removethis.anderson at comcast.andthis.net
Tue Jun 2 21:08:28 PDT 2009


The behavior is consistent with the specification -- see http://www.digitalmars.com/d/1.0/type.html.

Most C-based languages will do the same.

Paul

----------

davidl Wrote:

> ÔÚ Wed, 03 Jun 2009 10:48:02 +0800£¬Tim Matthews <tim.matthews7 at gmail.com>  
> дµÀ:
> 
> > davidl wrote:
> >> Following two piece of code demonstrate different behaviors currently.
> >>  import std.stdio;
> >> void main()
> >> {
> >>     uint v;
> >>     v=31234;
> >>     byte t= -1;
> >>     v |= t;
> >>     writefln(v);
> >> }
> >>  Output:4294967295
> >>  import std.stdio;
> >> void main()
> >> {
> >>     uint v;
> >>     v=31234;
> >>     byte t= -1;
> >>     v |= cast(ubyte)t;
> >>     writefln(v);
> >> }
> >>  Output:31487
> >>  I would always want the second result. What's your opinion?
> >>
> >
> > should be in d.learn but try this:
> >
> > import std.stdio;
> > void main()
> > {
> >      uint v;
> >      v=31234;
> >      ubyte t= -1;
> >      v |= t;
> >      writefln(v);
> > }
> 
> I mean the first behavior can cause problems. I doubt any coder would try  
> to get that result by writing that piece of code. I ported some C# source  
> to D, and I got this semantic different issue.
> 
> -- 
> ʹÓà Opera ¸ïÃüÐԵĵç×ÓÓʼþ¿Í»§³ÌÐò: http://www.opera.com/mail/




More information about the Digitalmars-d mailing list