F
Francois Grieu
The values of ((int)0.7) and ((int)-0.7) seem to be 0
Is this independent of implementation ?
TIA,
François Grieu
Is this independent of implementation ?
TIA,
François Grieu
The values of ((int)0.7) and ((int)-0.7) seem to be 0
Is this independent of implementation ?
Francois Grieu said:The values of ((int)0.7) and ((int)-0.7) seem to be 0
Is this independent of implementation ?
Yes.
No trouble found: working as designed.
Yes.
No trouble found: working as designed.
Francois Grieu said:Got the reference: 6.3.1.4.1 in ISO/IEC 9899:1999
"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.
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)."
Great. Will remove tons of floor() from my code.
Huh? floor(-0.7) is -1.0
Note that -7/10 may be 0 or -1 in C90.
Huh? floor(-0.7) is -1.0
Note that -7/10 may be 0 or -1 in C90.
Jack Klein said:What does either -7/10, an integer division, or floor(-0.7), a library
function have to do with the question the OP asked?
He asked about casting a floating point value to int, which is
absolutely well-defined in every version of the C standard and K&R
before that, providing the integral part of the floating point value
is within the range of the int.
(int)0.7 and (int)-0.7 both yield 0, always have, and always will.
Casting or assigning to int always truncates the fractional part and
leaves the integral part. Unless the floating point value was
completely integral to start with, the result always truncates toward
0.
how do you conclude that?
can you point me to
the relevant sections in the standard please?
I think the OP was asking about signs. In IEEE754 floats are signedJack said:What does either -7/10, an integer division, or floor(-0.7), a library
function have to do with the question the OP asked?
He asked about casting a floating point value to int, which is
absolutely well-defined in every version of the C standard and K&R
before that, providing the integral part of the floating point value
is within the range of the int.
(int)0.7 and (int)-0.7 both yield 0, always have, and always will.
Casting or assigning to int always truncates the fractional part and
leaves the integral part. Unless the floating point value was
completely integral to start with, the result always truncates toward
0.
I think the OP was asking about signs. In IEEE754 floats are signed
magnitude such that the only difference between 0.7 and -0.7 is the sign
bit. On the theoretical ones complement and signed magnitude integer
machines, -0 is also theoretically possible. What does C say about
(int)-0.7 on these?
Jack Klein said:...
The C standard does not allow the result of this conversion to be -0
even if the implementation does have a negative zero for integer
types. Here is paragraph 3 of 6.2.6.2 Integer types:
***begin quote***
If the implementation supports negative zeros, they shall be generated
only by:
? the &, |, ^, ~, <<, and >> operators with arguments that produce
such a value;
? the +, -, *, /, and % operators where one argument is a negative
zero and the result is zero;
? compound assignment operators based on the above cases.
It is unspecified whether these cases actually generate a negative
zero or a normal zero, and whether a negative zero becomes a normal
zero when stored in an object.
***end quote***
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.