double vs. float vs. int

• 02-16-2013, 09:51 PM
ajetrumpet
double vs. float vs. int
hello all,

I know this is elementary but I need some ground rules for myself to follow. I came from a VB background which basically leaves me with languages in the 'C' fam. like Java doing a lot of work for me when I don't recognize it. Still learning that part.

My end goal here is to output shapes in an applet that have coords that are rounded to the nearest pixel (int) but the calculations are done with precision before hand using variable calcs. I'm a little confused on how to accomplish this.

here is what I have so far (which I am testing in a simple class and printing out the results):

Code:

```int a; double b; int c; int d; int e;```
here are the assignments:

Code:

```a=6; b=(float)(1/6); c=(int)(100 * b); d=(int)(100 * ((a - 2) * c)); e=(int)(100 * ((a - 2) * c));```
here is what I'm getting in the output:

Code:

```6 0.0 0 0 0```
but this is what I *want*:

Code:

```6 0.1667 16 (derived from 16.667) 66 (derived from 66.667) 66 (derived from 66.667)```
can someone throw me a bone? I'm close but I'm missing the connecting dots. thanks!
• 02-16-2013, 11:13 PM
Ronin
Re: double vs. float vs. int
Hi ajetrumpet, welcome to the forums.

If you are performing mathematic functions on two numbers Java produces an integer result unless one of the values is defined as another data type.
The easiest way to correct this is to use the equation (1.0/6).

Regards.
• 02-16-2013, 11:38 PM
jim829
Re: double vs. float vs. int
It probably won't be a problem in this example but decimal numbers (e.g. 1.0) default to double. The float equivalent is (1.0f). So however you achieve casting to the correct float, you are assigning it to a double and losing 32 bits of precision along the way. Compare the following:

Code:

```double a = (float)(1.0/6);  // displays 0.1666666716337204 double b = 1.0/6;    // no cast needed.  Displays 0.16666666666666666```
I don't know why the designers made double (64 bits) the default for floating point numbers and int (32 bits) the default for whole numbers. I would think execution wise floats should be the default. But then I am not an expert on their design philosophy. I could speculate that when decimals are involved, higher precision is normally required.

Regards,
Jim
• 02-17-2013, 12:07 AM
ajetrumpet
Re: double vs. float vs. int
Quote:

Originally Posted by Ronin
Hi ajetrumpet, welcome to the forums.

If you are performing mathematic functions on two numbers Java produces an integer result unless one of the values is defined as another data type.
The easiest way to correct this is to use the equation (1.0/6).

Regards.

Ronin,

thanks for the welcome. I do already know this about Java. There are other problems with doing what you suggest as well. You see, if you produce a float or a double by your math, then the definition becomes as such. The problem with that is when I get to the inputs for the drawOval() function or anything similar. The inputs are integers and the outputs are in pixels via the image.

I did find a solution to this. If you can trim it, that would be much appreciated. Here's what the compiler accepts (my solution):

Code:

```double a; double b; double c; double d; double e;```
assignments:

Code:

```a=6; b=(float)(1/6); c=(float)(100 * b); d=(float)(100 * ((a - 2) * c)); e=(float)(100 * ((a - 2) * c));```
re-assignments:

Code:

```a2=(int)a b2=(int)b c2=(int)c d2=(int)d e2=(int)e```
at this point I can throw them into the graphic command arguments. It's terribly long but works fine. What do you think of this? Can it be consolidated??
• 02-17-2013, 12:19 AM
jim829
Re: double vs. float vs. int
Hi,

I do graphics stuff all the time. My recommendation is to first do what Ronin suggested by replacing 1/6 with 1.0/6. In your updated code you are still assigning the value 0 to b because 1/6 is evaluated as an int. And subsequently this cause c to be assigned 0. In your case above, you can do all your math using doubles and then just cast the values to int when done. You might also want to round up or down before casting to int by adding .5 to the decimal result (or take advantage of some of the rounding functions in java.lang.Math).

Regards,
Jim
• 02-17-2013, 01:30 AM
ajetrumpet
Re: double vs. float vs. int
Quote:

Originally Posted by jim829
Hi,

I do graphics stuff all the time. My recommendation is to first do what Ronin suggested by replacing 1/6 with 1.0/6. In your updated code you are still assigning the value 0 to b because 1/6 is evaluated as an int. And subsequently this cause c to be assigned 0. In your case above, you can do all your math using doubles and then just cast the values to int when done. You might also want to round up or down before casting to int by adding .5 to the decimal result (or take advantage of some of the rounding functions in java.lang.Math).

Regards,
Jim

Jim,

Does casting to int() immediately treat the floating point double value as being "floored" by the floor() function? is it the same result?

and where do I find a list of casting functions and in what class are they found? The string class is what comes to mind.

thanks. Your answer was fine too, I just picked the first one. this would be a lot easier if I had a coder to do this for me. but alas, I do not so you find the easiest way to get it done.
• 02-17-2013, 03:19 AM
ajetrumpet
Re: double vs. float vs. int
Jim,

Just for your entertainment I thought I would post the code that I came up with because I didn't have time to learn the "proper" way to do these things. I'm sure it's *way* longer than needed. If you care to, I would certainly love to see the consolidated version of it, generally the way it *should* have been done. Thank you for your help, and Ronin's too.

Code:

```import java.awt.*; //AWT = ABSTRACT WINDOWS TOOLKIT. import javax.swing.*; //SWING = GUI COMPONENTS. import java.lang.Math; //MATH CLASS. /*  * Class app_pictures - This applet will produce a block of connected shapes in no particular order or arrangement. The paint method will use variables to assign grid placement integers based on the shapes that are output.  This is necessary because the connected shapes that are output to the screen are required to be centered to the window with regard to their presentation.  * Shape Layout - The output of this applet will be produced on a 500px * 500px window and use 5 different "areas" of 100px * 100px each.  These areas will be in the form of rectangles or enclosed shapes that are created with other drawing commands performed on the Graphics object.  * Shape Dimensions - The shape dimension and placement variables will be set according to the mathematics that define a 500px * 500px window and 100px rectangles.  * Rectangle Numeric Assignments - The coordinates of Rectangles 1 - 5 are assigned to variables using the following syntax: "yRect(#)".  Rectangles are assigned indexes based on their placement in the window.  The first rectangle is located i nthe upper-left corner of the window and subsequent rectangle indexes are assigned per clockwise rotation from that point.  * Angle Orientations - Methods drawArc() and fillArc() use a Start Angle oriented with the right side of the screen and an Arc Angle that rotates counter-clockwise. */ public class app_pictures extends JApplet {     // SHAPE COLORS     Color clrWhite;     Color clrBlack;         //GENERAL USE     double circX_dbl; //x coord.     double circY_dbl; //y coord.     int circX_int; //x coord.     int circY_int; //y coord.            //BLACK RECTANGLE COORDINATES / WIDTH / HEIGHT     int xRect1;     int yRect1;     int xRect2;     int yRect2;     int xRect3;     int yRect3;     int xRect4;     int yRect4;     int xRect5; //middle rectangle     int yRect5; //middle rectangle     int htRect; //rectangle height     int wtRect; //rectangle width         //WHITE CIRCLE COORDINATES + WIDTH + HEIGHT (doubles for pricision)     double circNumCoords_dbl; //number of possible coordinate placements for the 'x' and 'y' of each circle     double RectSplitFraction_dbl; //the fraction used to split a rectangle's dimension into equal portions.     double circPxPortions_dbl;     double htCirc_dbl; //circle height     double wtCirc_dbl; //circle width     //WHITE CIRCLE COORDINATES + WIDTH + HEIGHT (ints for shape arguments)     int circNumCoords;     int circPxPortions;     int htCirc;     int wtCirc;     //GRID PLACEMENTS     int x100px;     int x200px;     int x300px;     int x400px;     int x500px;     int y100px;     int y200px;     int y300px;     int y400px;     int y500px;     public void init()     {                 //VARIABLE INSTANCES ON INIT.         clrWhite = Color.white;         clrBlack = Color.black;         //pixel placements.         x100px = 100;         x200px = 200;         x300px = 300;         x400px = 400;         x500px = 500;         y100px = 100;         y200px = 200;         y300px = 300;         y400px = 400;         y500px = 500;         //rectangle placements (upper left)         xRect1 = x100px;         yRect1 = y100px;         xRect2 = x300px;         yRect2 = y100px;         xRect3 = x300px;         yRect3 = y300px;         xRect4 = x100px;         yRect4 = y300px;         xRect5 = x200px;         yRect5 = y200px;             //rectangle dimensions         htRect = 100;         wtRect = 100;                 //PRICISION VALUES FOR ARCS/CIRCLES (double)         circNumCoords_dbl = 6; //6 possible coodinates for 'x' and 'y' based on moon-shape arcs shown.         RectSplitFraction_dbl = ((float)1/6); //fraction by which rectangles can be split to coincide coordinates.         circPxPortions_dbl = ((float)100 * (RectSplitFraction_dbl));         //RectSplitFraction, in PIXELS.         htCirc_dbl = ((float)htRect * ((circNumCoords_dbl - 2) * RectSplitFraction_dbl));         wtCirc_dbl = ((float)htRect * ((circNumCoords_dbl - 2) * RectSplitFraction_dbl));         //INTEGER VALUES FOR ARCS/CIRCLES (int)         circNumCoords = (int)circNumCoords_dbl;         circPxPortions = (int)circPxPortions_dbl;         htCirc = (int)htCirc_dbl;         wtCirc = (int)wtCirc_dbl;         JRootPane rootPane = this.getRootPane();            rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);         }     public void paint(Graphics g)     {         //OUTER RECTANGLES         g.setColor(clrBlack);         g.drawRect(xRect1,yRect1,wtRect,htRect);         g.fillRect(xRect1,yRect1,wtRect,htRect);         g.drawRect(xRect2,yRect2,wtRect,htRect);         g.fillRect(xRect2,yRect2,wtRect,htRect);         g.drawRect(xRect3,yRect3,wtRect,htRect);         g.fillRect(xRect3,yRect3,wtRect,htRect);         g.drawRect(xRect4,yRect4,wtRect,htRect);         g.fillRect(xRect4,yRect4,wtRect,htRect);                 //MIDDLE RECTANGLE         g.setColor(clrWhite);         g.drawRect(xRect5,yRect5,wtRect,htRect);         g.fillRect(xRect5,yRect5,wtRect,htRect);         //WHITE ARCS/CIRCLES INSIDE RECTANGLES         //Arc 1 (use 2 full circles)         g.setColor(clrWhite);         circX_dbl = Math.floor(xRect1 + circPxPortions);         circX_int = (int)circX_dbl;         circY_dbl = Math.floor(yRect1 + circPxPortions);         circY_int = (int)circY_dbl;         g.drawOval(circX_int, circY_int, wtCirc, htCirc);         g.fillOval(circX_int, circY_int, wtCirc, htCirc);         g.setColor(clrBlack); //black circle to make an arc.         circX_dbl = Math.floor(xRect1 + circPxPortions);         circX_int = (int)circX_dbl;         circY_dbl = Math.floor(yRect1 + circPxPortions * 2);         circY_int = (int)circY_dbl;         g.drawOval(circX_int, circY_int, wtCirc, htCirc);         g.fillOval(circX_int, circY_int, wtCirc, htCirc);         //Circle 2         g.setColor(clrWhite);         circX_dbl = Math.floor(xRect2 + circPxPortions);         circX_int = (int)circX_dbl;         circY_dbl = Math.floor(yRect2 + circPxPortions);         circY_int = (int)circY_dbl;         g.drawOval(circX_int, circY_int, wtCirc, htCirc);         g.fillOval(circX_int, circY_int, wtCirc, htCirc);         //Arc 3 (use 2 full circles)         g.setColor(clrWhite);         circX_dbl = Math.floor(xRect3 + circPxPortions);         circX_int = (int)circX_dbl;         circY_dbl = Math.floor(yRect3 + circPxPortions);         circY_int = (int)circY_dbl;         g.drawOval(circX_int, circY_int, wtCirc, htCirc);         g.fillOval(circX_int, circY_int, wtCirc, htCirc);         g.setColor(clrBlack); //black circle to make an arc.         circX_dbl = Math.floor(xRect3 + circPxPortions);         circX_int = (int)circX_dbl;         circY_dbl = Math.floor(yRect3);         circY_int = (int)circY_dbl;         g.drawOval(circX_int, circY_int, wtCirc, htCirc);         g.fillOval(circX_int, circY_int, wtCirc, htCirc);         //Circle 4         g.setColor(clrWhite);         circX_dbl = Math.floor(xRect4 + circPxPortions);         circX_int = (int)circX_dbl;         circY_dbl = Math.floor(yRect4 + circPxPortions);         circY_int = (int)circY_dbl;         g.drawOval(circX_int, circY_int, wtCirc, htCirc);         g.fillOval(circX_int, circY_int, wtCirc, htCirc);         //Arc Angle Middle         g.setColor(clrBlack);         circX_dbl = Math.floor(xRect5 + circPxPortions);         circX_int = (int)circX_dbl;         circY_dbl = Math.floor(yRect5 + circPxPortions);         circY_int = (int)circY_dbl;         g.drawArc(circX_int, circY_int, wtCirc, htCirc, 45, 270);         g.fillArc(circX_int, circY_int, wtCirc, htCirc, 45, 270);         //Rectangle 5 Border         g.drawLine(xRect5, yRect5, xRect5, yRect4);         g.drawLine(xRect5, yRect4, xRect3, yRect3);         g.drawLine(xRect3, yRect3, xRect2, yRect5);         g.drawLine(xRect2, yRect5, xRect5, yRect5);                 //Rectangle 5 Diagonals         g.drawLine(xRect5, yRect5, xRect3, yRect3);         g.drawLine(xRect5, yRect4, xRect2, yRect5);                 //Rectangle 2 Diagonal         g.drawLine(xRect2, yRect5, x400px, y100px);                 //Rectangle 4 Diagonal         g.drawLine(x100px, y400px, xRect5, yRect4);     } }```
• 02-17-2013, 04:02 AM
jim829
Re: double vs. float vs. int
Yes. Casting to an int always truncates the fractional part of the decimal number. So does casting to a long. I am not certain what you mean by casting functions. I am pretty certain the casting behavior is documented in the JLS but I have never looked it up before. The java.lang.Math class which is a series of static functions offers a variety of rounding methods. This has been around since 1.0.

Also, if you want your curved lines to look smoother, you can turn on anti-aliasing. Here is a sample paint function:
Code:

```  public void paint(Graphics g) {       Graphics2D g2d = (Graphics2D) g;       g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,             RenderingHints.VALUE_ANTIALIAS_ON);         //         // rest of code goes here.         //   }```
Since nothing is for free there may be some performance hits. But I have never really noticed any in the stuff that I do.

Regards,
Jim
• 02-17-2013, 04:24 AM
ajetrumpet
Re: double vs. float vs. int
Quote:

Originally Posted by jim829
Yes. Casting to an int always truncates the fractional part of the decimal number. So does casting to a long. I am not certain what you mean by casting functions.

Jim,

The problem with what we all do is that we work with a majority of people who will throw terms around all day long every day and not understand that their words are more confusing than helpful because everyone else is doing the same thing. That's where engineering competence comes in. Unfortunately it is tough to find, especially in young people.

"Function" means nothing more than "method". VB has two types of routines, functions and subroutines. In Java, the equiv. would be a "method". Here are some equiv's for you, just for entertainment:

• VB (module) / Java (class)
• VB (library) / Java (class (or subclass in a layered hierarchy). e.g. - java.lang.Number => java.lang.Double
• VB (function routine) / Java (method)
• VB (sub routine) / Java (method)
• VB (function) / Java (class method) e.g. - VB function = INT() ------- JAVA method = Math.floor()

VB has nothing else associated with it unless you delve into widget-type stuff like ActX components. Goes to show the difference between the families doesn't it? WOW.
• 02-17-2013, 04:56 AM
jim829
Re: double vs. float vs. int
I figured you meant function as either a method or subroutine or some other similar construct. However, the only casting function that I am aware of perse is in the java reflection classes where you actually cast an object by calling a cast method (or function as you will). In Java as well as the C language family, casting is typically done by enclosing the desired type in parentheses and applying it as a sort of operator to an expression which warrants casting.

Regards,
Jim
• 02-17-2013, 05:10 AM
ajetrumpet
Re: double vs. float vs. int
Quote:

Originally Posted by jim829
I figured you meant function as either a method or subroutine or some other similar construct. However, the only casting function that I am aware of perse is in the java reflection classes where you actually cast an object by calling a cast method (or function as you will). In Java as well as the C language family, casting is typically done by enclosing the desired type in parentheses and applying it as a sort of operator to an expression which warrants casting.

Regards,
Jim

so something like this?

Code:

```double x; x = 3.02938347; int y; y = (int)x;```
I used that in my code... is it as simple as that?
• 02-17-2013, 05:17 AM
jim829
Re: double vs. float vs. int
Exactly. If you print y it should print 3. I do this all the time because most of the graphics routines take ints as arguments. So you can cast them to assign to an int or cast them in the method call itself.

Regards,
Jim
• 02-17-2013, 05:28 AM
ajetrumpet
Re: double vs. float vs. int
wow. a new rule I can write down in my notes! who would've thunk it. thanks Jim. have a good night.