Re: question on conversion
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Wed, 14 Nov 2007 23:40:49 -0800
Walter Roberson wrote:
[...]
Sometimes C compilers warn about possible integer overflow when
assigning a floating point number to an integer, as the largest
floating point number is usually larger than the largest integer.
However, in this particular case, it is an unsigned integer that
is the target type, and the semantics of assigning a "too-large value"
to an unsigned integer are well defined, so in fact no overflow
can occur on this particular numeric conversion, so an overflow
warning would not be appropriate even on compilers that bother
to generate such warnings.
Sadly, that turns out not to be the case. It's true for integer-to-unsigned conversions, but not for floating-to-unsigned conversions.
C99 6.3.1.4p1:
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.
With a footnote:
The remaindering operation performed when a value of integer type
is converted to unsigned type need not be performed when a value
of real floating type is converted to unsigned type. Thus, the
range of portable real floating values is (?1, Utype_MAX+1).
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- Follow-Ups:
- Re: question on conversion
- From: Roman Mashak
- Re: question on conversion
- References:
- question on conversion
- From: Roman Mashak
- Re: question on conversion
- From: Walter Roberson
- question on conversion
- Prev by Date: Re: Memory leaking..
- Next by Date: 2008 .comp.lang.c++
- Previous by thread: Re: question on conversion
- Next by thread: Re: question on conversion
- Index(es):
Relevant Pages
|