472,803 Members | 889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,803 software developers and data experts.

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 3104
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
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.