The values of ((int)0.7) and ((int)0.7) seem to be 0
Is this independent of implementation ?
TIA,
François Grieu 12 2698
On Mon, 23 Feb 2004 16:06:09 +0100, Francois Grieu <fg****@micronet.fr> wrote: 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.

Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed are my own, not my employers')
Francois Grieu <fg****@micronet.fr> writes: The values of ((int)0.7) and ((int)0.7) seem to be 0 Is this independent of implementation ?
Yes. A conversion from floatingpoint to integer type simply
discards the fractional part of the value.

int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)p;if(i>=(int)sizeof p)i=sizeof p1;putchar(p[i]\
);}return 0;}
In article <40***************@news21.on.aibn.com>, Le*********@td.com (Lew Pitcher) wrote: On Mon, 23 Feb 2004 16:06:09 +0100, Francois Grieu <fg****@micronet.fr> wrote:
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.
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.
Francois Grieu
In article <40***************@news21.on.aibn.com>, Le*********@td.com (Lew Pitcher) wrote: On Mon, 23 Feb 2004 16:06:09 +0100, Francois Grieu <fg****@micronet.fr> wrote:
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.
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.
François Grieu
Francois Grieu <fg****@micronet.fr> wrote: Le*********@td.com (Lew Pitcher) wrote: Francois Grieu <fg****@micronet.fr> wrote:
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.
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.

Peter
On 23 Feb 2004 15:36:50 0800, ai***@acay.com.au (Peter Nilsson) wrote
in comp.lang.c: Francois Grieu <fg****@micronet.fr> wrote: Le*********@td.com (Lew Pitcher) wrote: Francois Grieu <fg****@micronet.fr> wrote:
> 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.
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.
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 welldefined 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.

Jack Klein
Home: http://JKTechnology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/Cfaq/top.html
comp.lang.c++ http://www.parashift.com/c++faqlite/
alt.comp.lang.learn.cc++ http://www.contrib.andrew.cmu.edu/~a...FAQacllc.html ai***@acay.com.au (Peter Nilsson) wrote in message news:<63**************************@posting.google. com>... Francois Grieu <fg****@micronet.fr> wrote: Le*********@td.com (Lew Pitcher) wrote: Francois Grieu <fg****@micronet.fr> wrote:
> 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.
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.
how do you conclude that? can you point me to
the relevant sections in the standard please?

nethlek
Jack Klein <ja*******@spamcop.net> wrote in message news:<uj********************************@4ax.com>. .. On 23 Feb 2004 15:36:50 0800, ai***@acay.com.au (Peter Nilsson) wrote in comp.lang.c: Francois Grieu <fg****@micronet.fr> wrote: Le*********@td.com (Lew Pitcher) wrote: > Francois Grieu <fg****@micronet.fr> wrote: > > > 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.
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.
What does either 7/10, an integer division, or floor(0.7), a library function have to do with the question the OP asked?
floor() was clearly mentioned by the OP in the post I was replying to.
Without further context, it's not obvious to me how the OP was
removing tons of floor() operations. I imagine they were doing
operations like...
double x;
int y = floor(x);
....where x was potentially negative. This is quite different to how
rounding of x would behave on straight conversion to int.
Perhaps they were doing...
y = (x < 0) ? floor(x) : floor(x);
....I don't know!
The integer division is an aside which which the OP and other readers
may not have been aware of. [i.e. that integer division rounding may
not match the rounding of conversion from floating point to an integer
type.]
I should perhaps have said 'Aside: ...' rather than 'Note that...'.
He asked about casting a floating point value to int, which is absolutely welldefined 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 never said otherwise.

Peter ne*****@tokyo.com (Mantorok Redgormor) wrote in message news:<41**************************@posting.google. com>... ai***@acay.com.au (Peter Nilsson) wrote in message news:<63**************************@posting.google. com>... Francois Grieu <fg****@micronet.fr> wrote: Le*********@td.com (Lew Pitcher) wrote: > Francois Grieu <fg****@micronet.fr> wrote: > > > 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.
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.
how do you conclude that?
In C90, it is implementation defined as to whether rounding is up or
down when either operand is negative.
can you point me to the relevant sections in the standard please?
From the C89 draft (3.3.5)...
When integers are divided and the division is inexact, if both
operands are positive the result of the / operator is the largest
integer less than the algebraic quotient and the result of the %
operator is positive. If either operand is negative, whether the
result of the / operator is the largest integer less than the
algebraic quotient or the smallest integer greater than the
algebraic
quotient is implementationdefined, as is the sign of the result of
the % operator. If the quotient a/b is representable, the
expression
(a/b)*b + a%b shall equal a .
C99 'fixed' the behaviour as always rounding towards zero.

Peter
Jack Klein wrote: On 23 Feb 2004 15:36:50 0800, ai***@acay.com.au (Peter Nilsson) wrote in comp.lang.c:
Francois Grieu <fg****@micronet.fr> wrote: Le*********@td.com (Lew Pitcher) wrote: > Francois Grieu <fg****@micronet.fr> wrote: > > > 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.
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.
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 welldefined 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?

Joe Wright http://www.jwwright.com
"Everything should be made as simple as possible, but not simpler."
 Albert Einstein 
On Wed, 25 Feb 2004 01:20:11 GMT, Joe Wright
<jo********@earthlink.net> wrote in comp.lang.c: Jack Klein wrote: On 23 Feb 2004 15:36:50 0800, ai***@acay.com.au (Peter Nilsson) wrote in comp.lang.c:
Francois Grieu <fg****@micronet.fr> wrote: > Le*********@td.com (Lew Pitcher) wrote: > > Francois Grieu <fg****@micronet.fr> wrote: > > > > > 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. > > 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.
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 welldefined 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?
Neither the representation of floating point values nor of integer
types has anything at all to do with the results of conversion of a
floating point value to an integer type, whether by assignment or
cast. It is defined in terms of value, not representation.
If the integral part of the floating point type is outside the range
of values of the integer type the result is undefined. If the
integral part is representable in the destination integer type, the
value is truncated toward 0, the fractional part simply discarded.
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***
Since conversion from floating point types is not on this exclusive
(due to "only") list, an implementation must produce an ordinary, and
not a negative, zero from converting 0.7 to any integer type.

Jack Klein
Home: http://JKTechnology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/Cfaq/top.html
comp.lang.c++ http://www.parashift.com/c++faqlite/
alt.comp.lang.learn.cc++ http://www.contrib.andrew.cmu.edu/~a...FAQacllc.html
Jack Klein <ja*******@spamcop.net> wrote in message news:<j0********************************@4ax.com>. .. On Wed, 25 Feb 2004 01:20:11 GMT, Joe Wright <jo********@earthlink.net> wrote in comp.lang.c: 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?
... 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***
This appears to be C99. My C89 draft has no mention of negative zeros
or trap representations, though it seems the Committee decided that
these are not precluded from C90.
Is there are definitive answer in C90 (or C95) with similar Chapter
and Verse?

Peter This discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by G Kannan 
last post: by

3 posts
views
Thread by otto 
last post: by

3 posts
views
Thread by Eric Chang 
last post: by

16 posts
views
Thread by cwizard 
last post: by

4 posts
views
Thread by dmiller23462 
last post: by

7 posts
views
Thread by matthew_carver 
last post: by

13 posts
views
Thread by Stumped and Confused 
last post: by

1 post
views
Thread by Dan 
last post: by

21 posts
views
Thread by Michael Bierman 
last post: by
           