After having created the code for my last post, I began to search a way to improve it. In the version previously posted, I used a “double” and at the 1024th loop my Macbook is showing me an “inf” instead of a numerical value… which means that I have overflowed the value of a double. I then tried different thing, my goal was to get the “infinite” as far as possible. After some reflexion I decided to use the biggest value for Integer Unit and then Floating Point Unit. Using an “unsigned long long” I realized that the value would never become “infinite” but would stay constant at “18446744073709551615″ after the 63th execution loop. Using a “long double” I’ve been able to get the “infinite” to appear only after the 16383th execution loop with a value of “6E+4931″.
This bring me the interest of knowing the limits for the different types on my Macbook Pro. I search a bit on the net and found a simple piece of C code from “Alberto Bertogli” that simply display all the informations I was interested in.
max value of char: 127
min value of char: -128
max value of int: 2147483647
min value of int: -2147483648
max value of long: 2147483647
min value of long: -2147483648
min value of long long: -9223372036854775808
max value of long long: 9223372036854775807
max value of unsigned long long: 18446744073709551615
max value of signed char: 127
min value of signed char: -128
max value of short: 32767
min value of short: -32768
max value of unsigned char: 255
max value of unsigned int: 4294967295
max value of unsigned long 4294967295
max value of unsigned short: 65535
max value of double: 1.797693e+308
min value of double: 2.225074e-308
max value of double 10 exp: 308
min value of double 10 exp: -307
max value of long double: 1.18973e+4932
min value of long double: 3.3621e-4932
max value of long double 10 exp: 4932
min value of long double 10 exp: -4931
max value of float: 3.40282e+38
min value of float: 1.17549e-38
max value of float 10 exp: 38
min value of float 10 exp: -37
——————————————————-
sizeof char: 1
sizeof short: 2
sizeof int: 4
sizeof long: 4
sizeof long long: 8
sizeof double: 8
sizeof long double: 16
sizeof float: 4
——————————————————-
sizeof off_t: 8
sizeof size_t: 4
sizeof ssize_t: 4
name max: 255
path max: 1024
By executing this code I realized that the previously obtained “18446744073709551615″ was the maximum value of an “unsigned long long”. I then though that “6E+4931″ would be the maximum value of a “long double” but I was wrong, the limits for a “long double” is “10^4932″ and it’s easy to understand why I get the “infinite” after getting “6E+4931″ because by doing a multiplication by 2 and adding one (thats what the love code do) you get over the limit of a “long double” so … you get the “infinite”.
/* * limits.c * Alberto Bertogli (albertogli@telpin.com.ar) * 22/Oct/2002 * * Shows the limits of data types. * * Try compiling it with "gcc -std=c99 limits.c -o limits" */ #include <stdio.h> #include <limits.h> #include <stdint.h> #include <float.h> #include <fcntl.h> #include <sys/param.h> /* for PATH_MAX and NAME_MAX */ int main(void) { printf("max value of char: %d\n", CHAR_MAX); printf("min value of char: %d\n", CHAR_MIN); printf("max value of int: %d\n", INT_MAX); printf("min value of int: %d\n", INT_MIN); printf("max value of long: %ld\n", LONG_MAX); printf("min value of long: %ld\n", LONG_MIN); #ifdef LLONG_MIN printf("min value of long long: %lld\n", LLONG_MIN); printf("max value of long long: %lld\n", LLONG_MAX); printf("max value of unsigned long long: %llu\n", ULLONG_MAX); #else printf("long long limits unavailable (compile with -std=c99)\n"); #endif printf("max value of signed char: %d\n", SCHAR_MAX); printf("min value of signed char: %d\n", SCHAR_MIN); printf("max value of short: %d\n", SHRT_MAX); printf("min value of short: %d\n", SHRT_MIN); printf("max value of unsigned char: %u\n", UCHAR_MAX); printf("max value of unsigned int: %u\n", UINT_MAX); printf("max value of unsigned long %lu\n", ULONG_MAX); printf("max value of unsigned short: %u\n", USHRT_MAX); printf("max value of double: %e\n", DBL_MAX); printf("min value of double: %e\n", DBL_MIN); printf("max value of double 10 exp: %i\n", DBL_MAX_10_EXP); printf("min value of double 10 exp: %i\n", DBL_MIN_10_EXP); printf("max value of long double: %Lg\n", LDBL_MAX); printf("min value of long double: %Lg\n", LDBL_MIN); printf("max value of long double 10 exp: %i\n", LDBL_MAX_10_EXP); printf("min value of long double 10 exp: %i\n", LDBL_MIN_10_EXP); printf("max value of float: %g\n", FLT_MAX); printf("min value of float: %g\n", FLT_MIN); printf("max value of float 10 exp: %i\n", FLT_MAX_10_EXP); printf("min value of float 10 exp: %i\n", FLT_MIN_10_EXP); printf("-------------------------------------------------------\n"); printf("sizeof char: %d\n", sizeof(char)); printf("sizeof short: %d\n", sizeof(short)); printf("sizeof int: %d\n", sizeof(int)); printf("sizeof long: %d\n", sizeof(long)); printf("sizeof long long: %d\n", sizeof(long long)); printf("sizeof double: %d\n", sizeof(double)); printf("sizeof long double: %d\n", sizeof(long double)); printf("sizeof float: %d\n", sizeof(float)); printf("-------------------------------------------------------\n"); printf("sizeof off_t: %d\n", sizeof(off_t)); printf("sizeof size_t: %d\n", sizeof(size_t)); printf("sizeof ssize_t: %d\n", sizeof(ssize_t)); printf("name max: %d\n", NAME_MAX); printf("path max: %d\n", PATH_MAX); return 0; }#include <iostream> #define END_OF_LIFE 74*365 // 74 years in days // Accordingly to the Deathclock I'll die at 74 int main (int argc, char * const argv[]) { int dayPassedTogether; long double love = 1; for(dayPassedTogether = 1; dayPassedTogether <= END_OF_LIFE; dayPassedTogether++) { printf("Each day my love for you is incremented : %0.LE\n",love); love += love; love++; } return 0; }



