473,569 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

integer division

hi,
i'm new to MS Visual Studio and C++ but not to C programming in general. i'm
trying to divide two integers and get their actual quotient (eg 5/3 =
1.666667 etc). i thought i had type cast correctly, but please let me know,
because it appears to be rounding off. my code follows. Thanks!

void DrawLine(GLint x1, GLint y1, GLint x2, GLint y2)

{
int i, pk, dy, dx, yc, xc, inc, temp;
float m;
/*
x1 = 100;
y1 = 200;
x2 = 110;
y2 = 206;
*/

if((x1 >= x2) && (y1 >= y2)) {
temp = x1;
x1 = x2;
x2 = temp;
temp = y1;
y1 = y2;
y2 = temp;
}

dy = y2 - y1;
dx = x2 - x1;
if (dx != 0) m = (float)(dy/dx);
else m = -999;

// Debugging info
printf("\nDrawL ine called!\n");
printf("x1 = %d\n", x1);
printf("y1 = %d\n", y1);
printf("x2 = %d\n", x2);
printf("y2 = %d\n", y2);
printf("dx = %d\n", dx);
printf("dy = %d\n", dy);
if (dx != 0) printf("dy/dx = %d\n", dy/dx);
printf("m = %.2f\n", m);
}
OUTPUT:


DrawLine called!
x1 = 286
y1 = 204
x2 = 309
y2 = 187
dx = 23
dy = -17
dy/dx = 0
m = 0.00
Nov 14 '05 #1
9 17232
Darius Fatakia wrote:
hi,
i'm new to MS Visual Studio and C++ but not to C programming in
general. i'm trying to divide two integers and get their actual
quotient (eg 5/3 =
1.666667 etc). i thought i had type cast correctly, but please let me
know, because it appears to be rounding off. my code follows. Thanks!

void DrawLine(GLint x1, GLint y1, GLint x2, GLint y2)

{
int i, pk, dy, dx, yc, xc, inc, temp;
float m;
/*
x1 = 100;
y1 = 200;
x2 = 110;
y2 = 206;
*/

if((x1 >= x2) && (y1 >= y2)) {
temp = x1;
x1 = x2;
x2 = temp;
temp = y1;
y1 = y2;
y2 = temp;
}

dy = y2 - y1;
dx = x2 - x1;
if (dx != 0) m = (float)(dy/dx);


One solution would be:

if (dx != 0) m = (float)dy/(float)dx;

Nov 14 '05 #2
You are typecasting the result of the INTEGER
arithmetic (dy/dx) to a FLOAT. You should rather
typecast both dy and dx to a FLOAT (or double)
in the expression.

e.g.

m = ( float(dy)/float(dx) );

or declare float( or double) variables to which you
assign the integer values of dy and dx.


Darius Fatakia wrote:
hi,
i'm new to MS Visual Studio and C++ but not to C programming in general. i'm
trying to divide two integers and get their actual quotient (eg 5/3 =
1.666667 etc). i thought i had type cast correctly, but please let me know,
because it appears to be rounding off. my code follows. Thanks!

void DrawLine(GLint x1, GLint y1, GLint x2, GLint y2)

{
int i, pk, dy, dx, yc, xc, inc, temp;
float m;
/*
x1 = 100;
y1 = 200;
x2 = 110;
y2 = 206;
*/

if((x1 >= x2) && (y1 >= y2)) {
temp = x1;
x1 = x2;
x2 = temp;
temp = y1;
y1 = y2;
y2 = temp;
}

dy = y2 - y1;
dx = x2 - x1;
if (dx != 0) m = (float)(dy/dx);
else m = -999;

// Debugging info
printf("\nDrawL ine called!\n");
printf("x1 = %d\n", x1);
printf("y1 = %d\n", y1);
printf("x2 = %d\n", x2);
printf("y2 = %d\n", y2);
printf("dx = %d\n", dx);
printf("dy = %d\n", dy);
if (dx != 0) printf("dy/dx = %d\n", dy/dx);
printf("m = %.2f\n", m);
}

OUTPUT:

DrawLine called!
x1 = 286
y1 = 204
x2 = 309
y2 = 187
dx = 23
dy = -17
dy/dx = 0
m = 0.00


--
"It is impossible to make anything foolproof because fools are so
ingenious" - A. Bloch

Nov 14 '05 #3
On Tue, 27 Jan 2004 17:24:51 -0800, "Darius Fatakia"
<da************ @yahoo.com> wrote in comp.lang.c:
hi,
i'm new to MS Visual Studio and C++ but not to C programming in general. i'm
trying to divide two integers and get their actual quotient (eg 5/3 =
1.666667 etc). i thought i had type cast correctly, but please let me know,
because it appears to be rounding off. my code follows. Thanks! void DrawLine(GLint x1, GLint y1, GLint x2, GLint y2)

{
int i, pk, dy, dx, yc, xc, inc, temp;
float m;
[snip]
if (dx != 0) m = (float)(dy/dx);


The type of an expression in C depends solely on the type of its
arguments, and is not influenced in any way by what you might do with
the result of the expression.

dy and dx are ints, so dx/dy is an int division expression which
produces an int result, that is truncates the quotient to an int.

The fact that you cast the result afterwards does not change this.
The int result, with no fractional part, will be converted to float
even without the cast, simply by assigning it to the float object m.

To force the expression to be performed as a float operation, one or
both of the operands must be a float. So you need to do one of:

if (dx != 0) m = ((float)dy/dx);

if (dx != 0) m = (dy/(float)dx);

if (dx != 0) m = ((float)dy/(float)dx);

If you convert either of the operands to float by a cast, the compiler
must automatically promote the other float, perform a float divide,
and produce a float result.

--
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4
Jack Klein <ja*******@spam cop.net> wrote in message news:<sc******* *************** **********@4ax. com>...
if (dx != 0) m = (float)(dy/dx);


dy and dx are ints, so dx/dy is an int division expression which
produces an int result, that is truncates the quotient to an int.


This answer is, of course, correct, but I want to seize on it as
a simple example of the advantages of *Low-level* (WYSIWYG) languages.

One needn't be a C expert to deduce Mr. Klein's correct answer.
Just examining the parentheses one sees (dy/dx) or (5/3)
"should" be evaluated before the (float) part. Since (5/3) is 1,
(float)(5/3) is (float)(1) is 1.0000, not 1.6667. In this simple
case, if you understand parentheses you understand C.

Now perhaps a "friendly" high-level language would remember where
the (1) came from and evaluate (float)(1) as 1.6667. Would you like
that? Not I, but I gather some people find such "friendly" features
.... er, friendly!

C++ will also evaluate (float)(5/3) as 1.0000 but that's only because
the friendly C++ designers haven't gotten around to letting parentheses
be overloadable :-)

In slightly less egregious cases, C++ prides itself on doing the friendly
thing rather than the WYSIWYG thing.

That's what the debate is about between C++ fans and C fans.
The C fans aren't too lazy to construct operator inheritance mazes;
we just put more of a premium on transparent clarity.

James
Nov 14 '05 #5
ark

"Jack Klein" <ja*******@spam cop.net> wrote in message
news:sc******** *************** *********@4ax.c om...
On Tue, 27 Jan 2004 17:24:51 -0800, "Darius Fatakia"
<da************ @yahoo.com> wrote in comp.lang.c:
<snip>
To force the expression to be performed as a float operation, one or
both of the operands must be a float. So you need to do one of:

if (dx != 0) m = ((float)dy/dx);

if (dx != 0) m = (dy/(float)dx);

if (dx != 0) m = ((float)dy/(float)dx);

If you convert either of the operands to float by a cast, the compiler
must automatically promote the other float, perform a float divide,
and produce a float result.

<snip>
IMHO, compilers are not created equal, and while these three options are
mathematically equivalent, a compiler may have a better chance of optimizing
the 1st variant (essentially, by dividing integer mantissa of the
not-necessarily-fully-formed float). It may be more important for targets
without a hardware FPU or even a division instruction. For the same reason,
one may want to keep the divisor in an unsigned type when possible.
As a separate note, I believe there are 100% integral line drawing
algorithms out there (cannot give references now, try search).
Ark
Nov 14 '05 #6
On Wed, 28 Jan 2004 07:22:22 GMT, "ark" <ar****@comcast .net> wrote in
comp.lang.c:

"Jack Klein" <ja*******@spam cop.net> wrote in message
news:sc******** *************** *********@4ax.c om...
On Tue, 27 Jan 2004 17:24:51 -0800, "Darius Fatakia"
<da************ @yahoo.com> wrote in comp.lang.c:

<snip>

To force the expression to be performed as a float operation, one or
both of the operands must be a float. So you need to do one of:

if (dx != 0) m = ((float)dy/dx);

if (dx != 0) m = (dy/(float)dx);

if (dx != 0) m = ((float)dy/(float)dx);

If you convert either of the operands to float by a cast, the compiler
must automatically promote the other float, perform a float divide,
and produce a float result.

<snip>
IMHO, compilers are not created equal, and while these three options are
mathematically equivalent, a compiler may have a better chance of optimizing
the 1st variant (essentially, by dividing integer mantissa of the
not-necessarily-fully-formed float). It may be more important for targets
without a hardware FPU or even a division instruction. For the same reason,
one may want to keep the divisor in an unsigned type when possible.
As a separate note, I believe there are 100% integral line drawing
algorithms out there (cannot give references now, try search).
Ark


Optimization is not a language issue, and whether certain compilers
might generate more efficient code for one of the expressions above is
irrelevant in this group. What might be the "most efficient" on one
compiler might be least on another, or even on the same compiler with
a different set of options.

As for the algorithm you are referring to, a Google search on "Integer
Bresenham Algorithm" should turn up the references. Twenty years ago
I used it to generate pixel coordinates for displaying machine tool
contours on a Tecmar Grahpics Master (this was before such things as
EGA and VGA video) with an 8088.

There was no Internet or search engines in those days, so I invented
my own integer only algorithm similar to Breshenham's for circles and
circular arcs.

--
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7
ark

"Jack Klein" <ja*******@spam cop.net> wrote in message
news:io******** *************** *********@4ax.c om...
On Wed, 28 Jan 2004 07:22:22 GMT, "ark" <ar****@comcast .net> wrote in
comp.lang.c: <snip> Optimization is not a language issue, and whether certain compilers
might generate more efficient code for one of the expressions above is
irrelevant in this group. What might be the "most efficient" on one
compiler might be least on another, or even on the same compiler with
a different set of options.

<snip>

Languages are made to accomplish certain tasks, not just for syntactic fun.
For instance,
unsigned x; ..., x/=2;
is equivalent to x>>=1;
whereas
int x; ..., x/=2;
is roughly equivalent to
if(x<0) x+=1; x>>=1;
or about 200% overhead plus possible pipeline break, if any.
So I am not convinced that efficiency is totally off topic here...
- Ark
Nov 14 '05 #8
Nick Landsberg <hu*****@att.ne t> wrote:
You are typecasting the result of the INTEGER
arithmetic (dy/dx) to a FLOAT. You should rather
typecast both dy and dx to a FLOAT (or double)
in the expression.

e.g.

m = ( float(dy)/float(dx) );


That would be C++, but in C typecasts must be written (float)dx ...
Also Note that it should be enough to cast either of the operands
to float (or even better double) to force the calculation to be done
in float. The other operand will be magically converted according to
usual arithmetic conversions.

--
Z (Zo**********@d aimlerchrysler. com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 14 '05 #9
"ark" <ar****@comcast .net> wrote:
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:io******** *************** *********@4ax.c om...
Optimization is not a language issue, and whether certain compilers
might generate more efficient code for one of the expressions above is
irrelevant in this group. What might be the "most efficient" on one
compiler might be least on another, or even on the same compiler with
a different set of options.
Languages are made to accomplish certain tasks, not just for syntactic fun.


True, but the language specification cannot rule over everything. And
the topic of this newsgroup is ISO C, not C-as-it-happens-to-run-on-
your-system, because tomorrow you may have a new computer which may make
these micro-optimisations completely unreliable.
For instance,
unsigned x; ..., x/=2;
is equivalent to x>>=1;
True, because it is guaranteed by the Standard; but you do not know
which of them will turn out to be more efficient on any particular
system until you measure it, and those measurements will not be valid
for most other systems. And in fact, many good compilers will compile
these two expressions to the very same machine code.
whereas
int x; ..., x/=2;
is roughly equivalent to
if(x<0) x+=1; x>>=1;
Wrong, wrong, utterly wrong.
For positive values and zero, the same equivalence as for unsigned int
holds. For negative values, however, the result of the shift is
implementation-defined. It is certainly possible that _your_
implementation happens to define it as the above code; but this is by no
means guaranteed, and anyone relying on it is setting himself up for a
surprise.
The division, OTOH, is perfectly defined for all values. So the division
and the shift-complex aren't equivalent at all, not even roughly.
or about 200% overhead plus possible pipeline break, if any.
You do not know any of that. You do not know that the +=1 gives you a
200% overhead. You do not know that the target computer even _has_ a
pipeline, let alone that this code could break it. You do not know that
the end result is the same. And finally, you do not know how efficiently
the target system implements signed integral divisions and shifts, so
you have no idea which of the two alternatives actually _is_ the most
efficient until you measure it.
So I am not convinced that efficiency is totally off topic here...


It is. It is totally system-dependent, and therefore totally off-topic.

Richard
Nov 14 '05 #10

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

Similar topics

7
2637
by: Matthew Wilson | last post by:
Hi- I just discovered this: >>> -1 // 12 -1 >>> 1 // 12 0 >>>
2
1832
by: Michael Cornelius | last post by:
As an old C programmer, I'm surprised by some results I'm getting with integer division. For example: >>> -1/1000 -1 >>> -9/2 -5 I expect the results of these expressions to be 0 and -4, respectively.
19
5142
by: Imbaud Pierre | last post by:
integer division and modulo gives different results in c and python, when negative numbers are involved. take gdb as a widely available c interpreter print -2 /3 0 for c, -1 for python. more amazing, modulos of negative number give negative values! (in c). from an algebraic point of view, python seems right, but I thought python conformity...
24
19309
by: Teis Draiby | last post by:
In .NET, can I be sure that the result of a division between two integers always is truncated rather that rounded to nearest? Example: 99 / 50 = 1 regards, Teis
2
591
by: Darius Fatakia | last post by:
hi, i'm new to MS Visual Studio and C++ but not to C programming in general. i'm trying to divide two integers and get their actual quotient (eg 5/3 = 1.666667 etc). i thought i had type cast correctly, but please let me know, because it appears to be rounding off. my code follows. Thanks! void DrawLine(GLint x1, GLint y1, GLint x2, GLint...
3
5412
by: Sidney Cadot | last post by:
Hi all, As I understand it, the rounding direction of signed integer division in C is unspecified in C89/C94, and specified to be 'towards-zero' in C99. I need division 'towards -infinity' (with a 'mod' operation to match), i.e. for x and y I need to obtain D and M such that (1) D*abs(y)+M == x
6
35285
by: J.Marsch | last post by:
Suppose that I have an integer division problem, but I want the answer was a float. What's the cleanest syntax for that? Example: In this code, the variable z ends up == 0. And that is correct, because this is integer division and the values are trunc'd. No problem. But what's the most elegant code given that x and y have to be ints,...
2
1643
by: Ben | last post by:
Hi, I have an interesting example from my debugger. I have 2 variables: sourcewidthnet and targetwidthnet. Notice the results in the debugger. I'm going to be forced to use the int function of the decimal.toint32 since the \ operator doesn't appear to work, or I greatly misunderstand the documentation on the \ operator. ...
9
43535
by: PengYu.UT | last post by:
Hi, The usually integer division will round the result to the biggest integet smaller than the float version division.For example, 10/3 = 3. I'm wondering if there is any easy way to round it to 4 for this case? Thanks, Peng
1
1861
by: Marge | last post by:
Create a java program that performs integer division. The program will ask the user for two integer numbers and will divide the dividend by the divisor.It will then output the results(quotient) and the remainder of the division. *Proof Test Case: Dividing two negative numbers Dividing two positive numbers
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7679
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3657
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2117
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 we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.