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

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
 
L

Lew Pitcher

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')
 
B

Ben Pfaff

Francois Grieu said:
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.
 
F

Francois Grieu

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
 
F

Francois Grieu

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
 
P

Peter Nilsson

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

Jack Klein

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

Mantorok Redgormor

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?
 
P

Peter Nilsson

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?

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

Peter Nilsson

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

Joe Wright

Jack 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?
 
J

Jack Klein

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

Peter Nilsson

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***

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?
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top