Re: question on conversion



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"
.



Relevant Pages

  • Re: Floating Point Rounding Mode
    ... I think the differences may arise due to co-processor differences Brian, ... rather than rounding modes. ... > type conversions done by C and FORTRAN use the chop rounding mode, ... > sure what happens during general floating point math operations using VB ...
    (microsoft.public.vb.general.discussion)
  • Re: Singles to Doubles
    ... all external representations are base 10 unless explicitly coded as ... Hence we can't apply optimisations on the conversions ... google on "floating point conversion" or similar and amongst ... routines but writing integer maths routines gave me a good idea of what's ...
    (microsoft.public.vb.general.discussion)
  • Re: VC++7.1 to 8.0 change, casting NaN or infinity to integer differs
    ... type to that integral type has undefined behaviour according to the C++ ... numeric conversions in a configurable manner. ... would only happen due to floating point inaccuracies, ... AND, these floating point ...
    (microsoft.public.vc.language)
  • Re: very confused....hex to decimal
    ... the conversions that you observe indicate) than your "expected" result. ... You must be attempting to convert the binary representation of a floating ... point number to a floating point decimal representation -- This cannot be ... Dick ...
    (microsoft.public.dotnet.languages.vb)
  • Re: With 12/16bit RAW do you still need an ND Grad
    ... can be useful to record scenes that would require many bits to record a ... Floating point notation was ... 16-bit unsigned integer: 0 to 65,535 ... shows flare, and typically a LOT of flare. ...
    (rec.photo.digital)