Re: [Vala] Int64 operations, I'm confused



Nope, it is not strange, it is what it should be.

%lld is not valid on windows, so it is taken as %d. so if you dont use the right format windows will execute this:

>      stdout.printf ("val6: %d, val7 : %d\n", val6, val7);

instead of

>      stdout.printf ("val6: %lld, val7 : %d\n", val6, val7);

What happens here is that %d will get the first 4 bytes of val6 and the second %d will get the next 4 bytes of val6. Resulting in 96 and 0. (little endian matters here).

So again, please use the correct, portable format strings. %lld is not portable. It will work on all known unixes, but fail on windows because they use %I64d.

On 07/31/13 10:54, raum no-log org wrote:
Ok, the value stored in int type variable is correct (I mean 12) but try
the following code :

void main() {
     int64 val6 = 96;
     int val7 = (int) (val6 / 8);
     stdout.printf ("val6: %lld, val7 : %d\n", val6, val7);
     stdout.printf ("%d\n", val7);
}

output :
$ a
val6: 96, val7 : 0
12

Int64 is correctly disdplayed with "%lld" format, int is not correctly
displayed at first.

Strange ? isn'it ? :)

But ok, I've got the right value.


generated C code :
/* a.c generated by valac 0.20.1, the Vala compiler
  * generated from a.vala, do not modify */

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




void _vala_main (void);


void _vala_main (void) {
        gint64 val6;
        gint val7;
        FILE* _tmp0_;
        FILE* _tmp1_;
        val6 = (gint64) 96;
        val7 = (gint) (val6 / 8);
        _tmp0_ = stdout;
        fprintf (_tmp0_, "val6: %lld, val7 : %d\n", val6, val7);
        _tmp1_ = stdout;
        fprintf (_tmp1_, "this is correct : %d\n", val7);
}


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






not for me

On 07/31/13 10:27, raum no-log org wrote:
      int64 val6 = 96;
        int val7 = (int) (val6 / 8);

$ cat a.vala
void main() {
          int64 val6 = 96;
          int val7 = (int) (val6 / 8);
          stdout.printf ("%d\n", val7);
}
$ vala a.vala
12

If you use the wrong format string you are going to display wrong data.

This is, windows is 32bit, and values are pushed on the stack from 4
bytes to 4, if the printf format expects a 64bit value it will fetch 4
bytes from the value and 4 bytes more from the stack (which can be zero
or any other value) causing the output you see.

Please, use the proper format string.

You can see the C code to verify if the compiler does it right, but you
didnt provided the valac version and I doubt vala can do such a simple
operation wrong.

--pancake





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