469,631 Members | 978 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

value of ((int)0.7) and ((int)-0.7)

The values of ((int)0.7) and ((int)-0.7) seem to be 0
Is this independent of implementation ?

TIA,

François Grieu
Nov 14 '05 #1
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')
Nov 14 '05 #2
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 floating-point 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 p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #3
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
Nov 14 '05 #4
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
Nov 14 '05 #5
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
Nov 14 '05 #6
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 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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7
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
Nov 14 '05 #8
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 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 never said otherwise.

--
Peter
Nov 14 '05 #9
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 implementation-defined, 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
Nov 14 '05 #10
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 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?
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #11
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 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?


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://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #12
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
Nov 14 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by otto | last post: by
3 posts views Thread by Eric Chang | last post: by
13 posts views Thread by Stumped and Confused | last post: by
1 post views Thread by Dan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.