473,471 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3182
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
3
by: otto | last post by:
i need to read a variable in a javascript and translate it to a form in html the javascript variable is: <SCRIPT LANGUAGE='JavaScript'>RF2N('Total');</script> and i need to put that...
3
by: Eric Chang | last post by:
I was working on this simple form with radio boxes. And when I click on one of the radio box, it tell me the value is "undefined" Why is that ? I did defined the value of each radio box: ...
16
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error... document.frmKitAmount.txtTotalKitValue is null or not...
4
by: dmiller23462 | last post by:
So here's my problem.....I need to set up different email distributions based on which option in the following Select form has been chosen....For instance if "Putaway" is chosen it needs to email...
7
by: matthew_carver | last post by:
Hello, I have an ASP page that loops through a SQL Server 2000 table, then downloads an Excel sheet the users can save, etc. Works fine, except, I see that in one particular "comments" field the...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
1
by: Dan | last post by:
This is one that has me stumped and I need an expert's input. Any ideas why the values from the second script-generated drop down list isn't recognized by the script to add time values to the...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
2
vivekgs2007
by: vivekgs2007 | last post by:
Hi i am doing a application that add the rate of the staff evaluation please go through o=it and need help <?php include("../db.php"); session_start(); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.