Re: [Vala] Why is 77 greater than 4294967295?



Wow! Why the comparison behavior of a signed long with an unsigned int
isn't same as C/C++ in Vala?

if (i > uint.MAX)  => if (_tmp6_ > (G_MAXUINT))

On Fri, Nov 23, 2018 at 1:46 PM Michele Dionisio via vala-list
<vala-list gnome org> wrote:

You are working on an architecture where long and int have the same size.
And at the end you compare a long with an unsigned int. The unsigned int is
converted to long and the maximum unsigned long is a negative signed long.


Il giorno ven 23 nov 2018, 04:34 Jason Martin via vala-list <
vala-list gnome org> ha scritto:

// simple vala code

void main () {

     long i = 77;

     stdout.printf ("%ld\n", i);
// outputs 77

     stdout.printf ("%u\n", uint.MIN);
// outputs 0

     if (i < uint.MIN) {
         stdout.printf ("Never made it here.\n");
     }

     stdout.printf ("%u\n", uint.MAX);
// outputs 4294967295
// G_MAXUINT

     if (i > uint.MAX) {
         stdout.printf ("Why am I here.\n");
     }

}

valac --cc=gcc gbug.vala



C code generated

/* gbug.c generated by valac 0.40.11, the Vala compiler
  * generated from gbug.vala, do not modify */



#include <glib.h>
#include <glib-object.h>
#include <stdio.h>




void _vala_main (void);


void
_vala_main (void)
{
     glong i = 0L;
     FILE* _tmp0_;
     glong _tmp1_;
     FILE* _tmp2_;
     glong _tmp3_;
     FILE* _tmp5_;
     glong _tmp6_;
     i = (glong) 77;
     _tmp0_ = stdout;
     _tmp1_ = i;
     fprintf (_tmp0_, "%ld\n", _tmp1_);
     _tmp2_ = stdout;
     fprintf (_tmp2_, "%u\n", 0);
     _tmp3_ = i;
     if (_tmp3_ < ((glong) 0)) {
         FILE* _tmp4_;
         _tmp4_ = stdout;
         fprintf (_tmp4_, "Never made it here.\n");
     }
     _tmp5_ = stdout;
     fprintf (_tmp5_, "%u\n", G_MAXUINT);
     _tmp6_ = i;
     if (_tmp6_ > ((glong) G_MAXUINT)) {
         FILE* _tmp7_;
         _tmp7_ = stdout;
         fprintf (_tmp7_, "Why am I here.\n");
     }
}


int
main (int argc,
       char ** argv)
{
     _vala_main ();
     return 0;
}

_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list

_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list



-- 
LargePrefPlaceholder-XKUz1MEJBwkOM


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]