wrap around problem for "double"?

Dave Korn dave.korn.cygwin@googlemail.com
Sun Dec 20 06:56:00 GMT 2009


Phan, Linh H (343H) wrote:
> Hi,
> 
> I was wondering why when "double" is assigned into a "short int" in cygwin
> 1.7, it doesn't wrap around correctly, 

  A double is a floating-point type, not an integer.  Your program is
incorrect; it invokes undefined behaviour per n1256#6.3.1.4.1:

> When a finite value of real floating type is converted to an integer type
> other than _Bool, the fractional part is discarded (i.e., the value is
> truncated toward zero). If the value of the integral part cannot be
> represented by the integer type, the behavior is undefined.

  So, you can't do that.  Whether it wraps, truncates, throws a floating-point
exception, or returns a random number, is entirely unpredictable and
system-dependent; the way you expect it to behave is just the way it happens
to behave by chance on the systems that you've tested it on, not how it is
supposed to behave by design.

    cheers,
      DaveK


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list