c++ - How does DBL_MAX addition work? -


code

#include<stdio.h> #include<limits.h> #include<float.h>  int f( double x, double y, double z){   return  (x+y)+z == x+(y+z); }  int ff( long long x, long long y, long long z){   return  (x+y)+z == x+(y+z); }  int main() {     printf("%d\n",f(dbl_max,dbl_max,-dbl_max));          printf("%d\n",ff(llong_max,llong_max,-llong_max));     return 0; } 

output

0 1 

i unable understand why both functions work differently. happening here?

in eyes of c++ , c standard, integer version , floating point version potentially invoke undefined behavior because results of computation x + y not representable in type arithmetic performed on. both functions may yield or anything.

however, many real world platforms offer additional guarantees floating point operations , implement integers in way lets explain results get.

considering f, note many popular platforms implement floating point math described in ieee 754. following rules of standard, lhs:

dbl_max + dbl_max = inf 

and

inf - dbl_max = inf. 

the rhs yields

dbl_max - dbl_max = 0 

and

dbl_max + 0 = dbl_max 

and lhs != rhs.

moving on ff: many platforms perform signed integer computation in twos complement. twos complement's addition assoziative, comparison yield true long optimizer not change contradicts twos complement rules.

the latter entirely possible (for example see this discussion), cannot rely on signed integer overflow doing explained above. however, seems "was nice" in case.


note never applies unsigned integer arithmetic. in c++, unsigned integers implement arithmetic modulo 2^numbits numbits number of bits of type. in arithmetic, every integer can represented picking representative of equivalence class in [0, 2^numbits - 1]. arithmetic can never overflow.

for doubting floating point case potential ub: n4140 5/4 [expr] says

if during evaluation of expression, result not mathematically defined or not in range of representable values type, behavior undefined.

which case. inf , nan stuff allowed, not required in c++ , c floating point math. required if std::numeric_limits::is_iec559<t> true floating point type in question. (or in c, if defines __stdc_iec_559__. otherwise, annex f stuff need not apply.) if either of iec indicators guarantees ieee semantics, behavior defined described above.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -