473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

What is the input range of (x >> 8) before overflow kicks in?

I am running such segment of codes, but errors kick in after maybe
absolute of x above 10^6.

What is the theoritical bound of x before I get errors?

double x;
int y;

x = ......;

y = ((int)x) >8;

Thanks.

Jan 22 '07 #1
8 1281

Mr. Ken napsal:
I am running such segment of codes, but errors kick in after maybe
absolute of x above 10^6.

What is the theoritical bound of x before I get errors?

double x;
int y;

x = ......;

y = ((int)x) >8;

Thanks.
#include <limits>

if (x <= std::numeric_limits<int>::max() && x >=
std::numeric_limits<int>::min())
{
// x typecasted to int will fit into int
}

Jan 22 '07 #2

"Ondra Holub" <on*********@post.czwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...
>
Mr. Ken napsal:
I am running such segment of codes, but errors kick in after maybe
absolute of x above 10^6.

What is the theoritical bound of x before I get errors?

double x;
int y;

x = ......;

y = ((int)x) >8;

Thanks.

#include <limits>

if (x <= std::numeric_limits<int>::max() && x >=
std::numeric_limits<int>::min())
{
// x typecasted to int will fit into int
}
Thank you but it doesn't solve my problem since sometimes x is not shifted.

I am using y = (int)(x/256.0), which is nearly correct.

However I liked the ">>" due to its readability.

Jan 22 '07 #3

Mr. Ken napsal:
"Ondra Holub" <on*********@post.czwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...

Mr. Ken napsal:
I am running such segment of codes, but errors kick in after maybe
absolute of x above 10^6.
>
What is the theoritical bound of x before I get errors?
>
>
>
double x;
int y;
>
x = ......;
>
y = ((int)x) >8;
>
Thanks.
#include <limits>

if (x <= std::numeric_limits<int>::max() && x >=
std::numeric_limits<int>::min())
{
// x typecasted to int will fit into int
}

Thank you but it doesn't solve my problem since sometimes x is not shifted.

I am using y = (int)(x/256.0), which is nearly correct.

However I liked the ">>" due to its readability.
Hi. I am not sure what you exactly need. (int)(x/256.0) is not the same
as ((int)x)/256; Former first divides x with 256 and then typecasts it
to int. Latter variant first typecasts x to int and then divides it
with 256.

So you can still check the result of x / 256.0 whether it fits to int.

BTW: x >8 is IMO not more readable than x / 256 and I bet every
compiler is able to change x / 256 to bit shift automatically (of
course when x is integer value).

Jan 22 '07 #4
Mr. Ken wrote:
>
"Ondra Holub" <on*********@post.czwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...
>>
Mr. Ken napsal:
I am running such segment of codes, but errors kick in after maybe
absolute of x above 10^6.

What is the theoritical bound of x before I get errors?

double x;
int y;

x = ......;

y = ((int)x) >8;

Thanks.

#include <limits>

if (x <= std::numeric_limits<int>::max() && x >=
std::numeric_limits<int>::min())
{
// x typecasted to int will fit into int
}

Thank you but it doesn't solve my problem since sometimes x is not
shifted.
If it does not solve your problem, then you did not explain you problem
correctly: you asked about the range of error-free behavior for the line

y = ( (int)x ) >8;

This line has well-define behavior if and only if the double x can be
converted to an int without overflow or underflow. The test suggested by
Ondra Holup ensures exactly that. What you do with the int-casted double
afterwards is immaterial.

I am using y = (int)(x/256.0), which is nearly correct.

However I liked the ">>" due to its readability.
Maybe you should fill us in on the precise requirements. If you tell us the
specs for the transformation x -y, someone might be able to suggest code
that is more than "nearly" correct.
Best

Kai-Uwe Bux
Jan 22 '07 #5

Kai-Uwe Bux wrote:
[snip]
[...]you asked about the range of error-free behavior for the line
(assuming double x)
y = ( (int)x ) >8;

This line has well-define behavior if and only if the double x can be
converted to an int without overflow or underflow.
[snip]

Are you sure? So far as I know shifts of integer types only have well
defined behaviour when the left argument is non-negative.

/Peter

Jan 22 '07 #6
peter koch wrote:
>
Kai-Uwe Bux wrote:
[snip]
>[...]you asked about the range of error-free behavior for the line
(assuming double x)
> y = ( (int)x ) >8;

This line has well-define behavior if and only if the double x can be
converted to an int without overflow or underflow.
[snip]

Are you sure? So far as I know shifts of integer types only have well
defined behaviour when the left argument is non-negative.
Ah, I should have said "not undefined" instead of "well-defined". You are
right: for negative integers, the resulting value is implementation defined
[5.8/3]. However, I would consider that still error-free behavior.
Best

Kai-Uwe Bux
Jan 22 '07 #7

"Ondra Holub" <on*********@post.czwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...
>
BTW: x >8 is IMO not more readable than x / 256 and I bet every
compiler is able to change x / 256 to bit shift automatically (of
course when x is integer value).
Of course when x is an _unsigned_ integer. -1 / 256 == 0, but on most
implementations, -1 >8 == -1 due to the use of the arithmetic shift right
on a signed int (and an implementation using a logical shift left can't even
use that instruction for dividing signed integers ;)). This leads to the
need for extra (perhaps relatively expensive, of course depending on the
platform) checks, therefore a regular divide instruction rather than a shift
can be chosen to "optimize" the expression.

- Sylvester
Jan 22 '07 #8

"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:ep**********@murdoch.acc.Virginia.EDU...
Mr. Ken wrote:

"Ondra Holub" <on*********@post.czwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...
>
Mr. Ken napsal:
I am running such segment of codes, but errors kick in after maybe
absolute of x above 10^6.

What is the theoritical bound of x before I get errors?

double x;
int y;

x = ......;

y = ((int)x) >8;

Thanks.

#include <limits>

if (x <= std::numeric_limits<int>::max() && x >=
std::numeric_limits<int>::min())
{
// x typecasted to int will fit into int
}
Thank you but it doesn't solve my problem since sometimes x is not
shifted.

If it does not solve your problem, then you did not explain you problem
correctly: you asked about the range of error-free behavior for the line

y = ( (int)x ) >8;

This line has well-define behavior if and only if the double x can be
converted to an int without overflow or underflow. The test suggested by
Ondra Holup ensures exactly that. What you do with the int-casted double
afterwards is immaterial.

I am using y = (int)(x/256.0), which is nearly correct.

However I liked the ">>" due to its readability.

Maybe you should fill us in on the precise requirements. If you tell us
the
specs for the transformation x -y, someone might be able to suggest code
that is more than "nearly" correct.
Best

Kai-Uwe Bux
Sorry for my bad communication.
Basically the C++ will be converted to hardware with logic gates. I will use
maybe 35-bits
for integers in 2's complement formats.

In C++ sims, I would define everything in "double" and x may end up be

z = sign * (0.0, 1.0, ...8191.0);
x = z * pow(2.0, 21);

y = ((int)x) >8.

y will be scaled down version of x. And ">>" is the usual operator we use in
the company.

I would like to know the exact z when y becomes erronous.

Jan 23 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: leroybt.rm | last post by:
I don't understand why this does not work: <FILE1> test1.py #Import Packages import string # data=0 data=data+1
2
by: not 2 swift | last post by:
I thought I would update an old page on which I had used a <INPUT TYPE=image ...> with a <BUTTON><IMG SRC=...></BUTTON> The problem is that <BUTTON> always provides a shadow/emboss effect. Can...
3
by: TR | last post by:
Is it possible with CSS to prevent this wrapping alignment with a checkbox with a nested label? This is the label of the checkbox that wraps beneath it I'd prefer it looked like...
3
by: Ben | last post by:
Here's my form: <form name="aForm" method='post'> <input type=file name=file1 onkeypress='KeyPress()'><br> <a id='attachMoreLink' href='javascript:AddFileInput()">Attach More Files </a> <input...
2
by: Richard Maher | last post by:
Hi, I'm trying to use the Visibility Style attribute for a Div to effectively PopUp a lightweight window with some additional context-sensitive information, when a user mouses over a given...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.