2017-06-09 4 views
0

Hier ist mein CodeErste zwei verschiedene Ergebnis für pow() Funktion auf zwei verschiedenen Linux OS

#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))); 
} 

ich den Code auf 2 verschiedenen Linux-Betriebssystem kompiliert. (Gcc powfn.c -o powfn)

Auf VirtualBox Ubuntu habe ich folgendes Ergebnis

pow as double: 4294967296.000000 
pow as long int: 4294967296 

auf Debian GNU/Linux 8 OS auf einem Znyq ARM Cortex A9-Prozessor läuft, ich habe folgendes Ergebnis

pow as double: 4294967296.000000 
pow as long int: 2147483647 

Was ist los? Warum die zwei verschiedenen Ergebnisse?

+1

Versuchen Sie, die Größe von 'long int Überprüfung 'auf beiden Maschinen. – taras

+1

Sieht so aus, als wäre Ihr langer int auf ARM eigentlich eine 32-Bit-Ganzzahl. – steppo

+0

Yup. lang Int auf ARM ist 4 Bytes, während auf VirtualBox Ubuntu seine 8 Bytes – KharoBangdo

Antwort

1

Es ist sehr wahrscheinlich, dass die beiden Prozessoren unterschiedliche Größen für die gleichen Datentypen haben. Sie können es testen, indem Kompilieren und Ausführen dieses Codes auf beiden Maschinen:

#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; 
} 

Hier ist das Ergebnis auf das Programm läuft ein Wandboard with Cortex-A9 und 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 
+0

Hier sind die Ergebnisse der Kompilierung auf einem Wandboard mit einem Cortex-A9: [Pastebin: GCC Wandboard Cortex-A9] (https://pastebin.com/up7jwC5d). – jww

Verwandte Themen