473,396 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,396 software developers and data experts.

Why won't my doubles hold their decimals?

I am trying to write a program to create boundaries for a polygon on a two-dimensional coordinate plane. Here is my code:

Expand|Select|Wrap|Line Numbers
  1. public class Example {
  2. public Example()
  3.     {
  4.         for (int y = 0; y < game1.o.l; y++)
  5.         {
  6.             System.out.println("( "+game1.o.bind[0][y]+", "+game1.o.bind[1][y]+" ) -> ( "+game1.o.bind[2][y]+", "+game1.o.bind[3][y]+" )\n");
  7.         }
  8.     }
  9. public class Obstacle {
  10.                 int cx, cy, dx, ex, ey;
  11.                 int w, l;
  12.                 double aw;
  13.                 int[][] bind;
  14.                 public Obstacle( int a, int cax, int cay, int eax, int eay, int dax )
  15.                 {
  16.                 cx = cax;
  17.                 cy = cay;
  18.                 dx = dax;
  19.                 ex = eax;
  20.                 ey = eay;
  21.                 w = ex - dx;
  22.                 l = ey - cy + 1;
  23.                 aw = (cx - dx)/(l - 1);
  24.                 System.out.println(aw+"\n");
  25.                 bind = new int[4][l]; 
  26.                 for ( int y = 0; y < l; y++ )
  27.                 {
  28.                     bind[0][y] = (int)Math.round( cx - ( aw * y ) );
  29.                     bind[1][y] = cy + y;
  30.                     bind[2][y] = (int)Math.round( cx - ( a * aw * y ) + w );
  31.                     bind[3][y] = cy + y;
  32.                 }
  33.             }
  34.         }
  35. }
Sorry it looks so messy. Anyways, when I ran it, it gives me this:

Expand|Select|Wrap|Line Numbers
  1. run:
  2. 0.0
  3.  
  4. 0.0
  5.  
  6. 0.0
  7.  
  8. 0.0
  9.  
  10. ( 10, 10 ) -> ( 15, 10 )
  11.  
  12. ( 10, 11 ) -> ( 15, 11 )
  13.  
  14. ( 10, 12 ) -> ( 15, 12 )
  15.  
  16. ( 10, 13 ) -> ( 15, 13 )
  17.  
  18. ( 10, 14 ) -> ( 15, 14 )
  19.  
  20. ( 10, 15 ) -> ( 15, 15 )
  21.  
  22. ( 10, 16 ) -> ( 15, 16 )
  23.  
  24. ( 10, 17 ) -> ( 15, 17 )
  25.  
  26. ( 10, 18 ) -> ( 15, 18 )
  27.  
  28. ( 10, 19 ) -> ( 15, 19 )
  29.  
  30. ( 10, 20 ) -> ( 15, 20 )
  31.  
  32. BUILD SUCCESSFUL (total time: 0 seconds)
  33.  
What am I doing wrong?
Jun 2 '12 #1
4 2114
Rabbit
12,516 Expert Mod 8TB
How can that code even run? You're dividing by 0, that should throw an error.
Jun 3 '12 #2
@Rabbit
The only place where division occurs is in line 23, and the only way you can divide by zero is if there is no difference between cy and ey, meaning the shape is a line, which I would not do within the parameters of this program. Although perhaps your confusion is related to that fact that I somehow forgot a certain line of code:

Expand|Select|Wrap|Line Numbers
  1. Obstacle o = new Obstacle(1,10,10,10,20,5);
Or maybe it's just because l and 1 look a bit alike in the code font.
Jun 3 '12 #3
Rabbit
12,516 Expert Mod 8TB
That's probably what it was. It looked like one minus one to me. The reason you're not getting a fraction is probably because all the math is on ints. Change the 1 to 1.0 and that should force it into float math.
Jun 4 '12 #4
ddyer
8
IEEE floating point numbers cannot represent exact decimal values. Base 2 is not Base 10.
Jun 4 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: lostinspace | last post by:
After four+ years of using FrontPage in a limited capacity and having created over 600 pages on my sites, I've finally (at least for the most part) abandoned FP, to begin the process of converting...
25
by: Allan Rydberg | last post by:
hi i'm trying to shift a double, but i'm getting the error message '>>' illegal, left operand double. althought that the manpages say, '>>' and '<<' can be applied for int's only, i was able...
5
by: John Dumais | last post by:
Hello, I have been trying to figure out how to write an array of doubles (in this specific case) to a binary stream without using a loop. What I have been doing is... foreach(double d in...
5
by: John | last post by:
Which variable type (c#) can whole the largest whole number? I know this sounds silly but as double and decimal are made for numbers with decimals I am not sure. Also if anybody knows of any...
6
by: Joseph Turian | last post by:
I want to create several types of double, e.g. D1 and D2. I want strong typing, such that a function defined for D1 will not accept a D2 argument. One solution is: struct D1 { double d; }; ...
27
by: Thomas Kowalski | last post by:
Hi everyone, To determine equality of two doubles a and b the following is often done: bool isEqual ( double a, double b ) { return ( fabs (a-b) < THRESHOLD ); } But this a approach usually...
6
by: Kuldeep | last post by:
Hi All, I have this piece of code shown below: decimal ft = Convert.ToDecimal(txtft.Text); decimal inch = Convert.ToDecimal(txtin.Text); decimal metre = ((ft * 12) + inch) * 0.0254;
3
by: Cainnech | last post by:
Hi guys, I've been trying to write script that counts decimals. I know Javascript won't work with them so you have to bypass it. I've looked around on the net and it seems that most people are...
3
by: paradox6996 | last post by:
Here is the error: 01C.java Program01C.java:9: <identifier> expected import static java.lang.Math.*; ^ Program01C.java:9: '.' expected import static java.lang.Math.*; ...
4
by: DaveRook | last post by:
Hi I have a simple code which needs to add 2 decimals together but alas, it won't. The figures are stored on an SQL database (as a decimal). The site uses asp (vb script) Lets pretend MaleHeight...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...

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.