Search code examples
linuxubuntudebianpowzynq

Getting two different result for pow() function on two different Linux OS


Here is my code

#include<stdio.h>
#include<math.h>

void main(void)
{
    printf("pow as double: %lf\n\r", pow(2,32));
    printf("pow as long int: %ld\n\r", ((long int)pow(2,32)));
}

I compiled the code on 2 different Linux OS. (gcc powfn.c -o powfn)

On VirtualBox Ubuntu, I got the following result

pow as double: 4294967296.000000  
pow as long int: 4294967296

On Debian GNU/Linux 8 OS running on a Znyq ARM Cortex A9 processor, I got the following result

pow as double: 4294967296.000000
pow as long int: 2147483647  

What is going on? Why the two different results?


Solution

  • It's highly likely the two processors have different sizes for the same data types. You can test it by compiling and running this code on both machines:

    #include <stdio.h>
    int main()
    {
        int integerType;
        long int longIntType;
        float floatType;
        double doubleType;
        char charType;
    
        // Sizeof operator is used to evaluate the size of a variable
        printf("Size of int: %ld bytes\n",sizeof(integerType));
        printf("Size of long int: %ld bytes\n",sizeof(longIntType));
        printf("Size of float: %ld bytes\n",sizeof(floatType));
        printf("Size of double: %ld bytes\n",sizeof(doubleType));
        printf("Size of char: %ld byte\n",sizeof(charType));
    
        return 0;
    }
    

    Here is the result of running the program on a Wandboard with Cortex-A9 and Ubuntu 15.10:

    wandboard:~$ ./test.exe
    Size of int: 4 bytes
    Size of long int: 4 bytes
    Size of float: 4 bytes
    Size of double: 8 bytes
    Size of char: 1 byte