Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can something be bigger than (unsigned long long) > LONG_MAX?

Tags:

c

I found this code in an algorithm I need to update:

 if (value > (unsigned long long) LONG_MAX)

EDIT: value is the result of a division of two uint64_t numbers.

I understand that (unsigned long long) LONG_MAX is a VERY big number:

   #include "stdio.h"
   #include "limits.h"

   int main() {
     unsigned long long ull = (unsigned long long) LONG_MAX;
     printf("%lu",ull);
     return 0;
   }

prints 9223372036854775807

So what I am comparing here? In what case this if statement will evaluate to true?

like image 262
transient_loop Avatar asked Feb 18 '20 13:02

transient_loop


1 Answers

A float or double can be larger than that. Appendix Ep5 of the C standard states that either type must be able to hold a value at least as large as 1E37 which is a larger value than LONG_MAX which must be at least 2147483647:

The values given in the following list shall be replaced by implementation-defined constant expressions with values that are greater than or equal to those shown:

#define DBL_MAX 1E+37
#define FLT_MAX 1E+37
#define LDBL_MAX 1E+37

So if value is either of those types, it could evaluate to true.

EDIT:

Since value is a uint64_t, whose max value is 18446744073709551615, this can also be larger than LONG_MAX.

like image 67
dbush Avatar answered Sep 19 '22 16:09

dbush