Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

What happens if a negative floating point value is converted into a value of unsigned integral type? Standard quotes would be appreciated. The problem I'm facing is conversion into values of unsigned integral types from a variant class, that contains an object of floating-point type.

EXAMPLE:

unsigned i(-.1);
share|improve this question
1  
Please define converted. – Sourav Ghosh 3 hours ago
2  
Did you try it? What problems are you having? – jtbandes 2 hours ago
2  
@jtbandes: this is a deep question. It will be, at best, implementation defined. – Bathsheba 2 hours ago
1  
@SouravGhosh I would assume the C standard definition of conversion, as explained in chapter 6.3. – Lundin 2 hours ago
4  
@jtbandes trying is the worst way to approach it. – Antti Haapala 2 hours ago
up vote 15 down vote accepted

It invokes undefined behavior. See the C11 standard, ISO 9899:2011:

6.3.1.4

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. 61)

And then there is a non-normative foot note explaining the above text:

61) 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).

ISO/IEC 9899:1999 (C99) contains exactly the same text.

share|improve this answer
2  
Is that the C standard? Actually I've noticed the question is multi-tagged. Have an upvote. – Bathsheba 2 hours ago
1  
@Bathsheba the question is labeled both C and C++ so either standard would be relevant. But it's a good question. – Mark Ransom 2 hours ago
    
@Bathsheba Yes. Clarified with edit. – Lundin 2 hours ago
4  
@Lundin but the question explicitly does -.1, which, truncated would be representable. – Antti Haapala 2 hours ago

It is undefined behaviour in C99 if the floating point number is less than or equal to -1.0. If it's in the range (-1.0, 0.0), the resulting value will be 0.

From C99, §6.3.1.4, paragraph 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

Footnote 50 clarifies the behaviour for the (-1.0, 0.0) range.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.