Results 1 to 13 of 13
Thread: double vs. float vs. int
 02162013, 09:51 PM #1Member
 Join Date
 Feb 2013
 Posts
 57
 Rep Power
 0
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):
Java Code:int a; double b; int c; int d; int e;
Java Code:a=6; b=(float)(1/6); c=(int)(100 * b); d=(int)(100 * ((a  2) * c)); e=(int)(100 * ((a  2) * c));
Java Code:6 0.0 0 0 0
Java Code:6 0.1667 16 (derived from 16.667) 66 (derived from 66.667) 66 (derived from 66.667)
 02162013, 11:13 PM #2Senior Member
 Join Date
 Oct 2010
 Posts
 393
 Rep Power
 5
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.
 02162013, 11:38 PM #3Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,033
 Rep Power
 6
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:
Java Code:double a = (float)(1.0/6); // displays 0.1666666716337204 double b = 1.0/6; // no cast needed. Displays 0.16666666666666666
Regards,
Jim
 02172013, 12:07 AM #4Member
 Join Date
 Feb 2013
 Posts
 57
 Rep Power
 0
Re: double vs. float vs. int
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):
Java Code:double a; double b; double c; double d; double e;
Java Code:a=6; b=(float)(1/6); c=(float)(100 * b); d=(float)(100 * ((a  2) * c)); e=(float)(100 * ((a  2) * c));
Java Code:a2=(int)a b2=(int)b c2=(int)c d2=(int)d e2=(int)e
Last edited by ajetrumpet; 02172013 at 12:09 AM.
 02172013, 12:19 AM #5Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,033
 Rep Power
 6
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
 02172013, 01:30 AM #6Member
 Join Date
 Feb 2013
 Posts
 57
 Rep Power
 0
Re: double vs. float vs. int
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.
 02172013, 03:19 AM #7Member
 Join Date
 Feb 2013
 Posts
 57
 Rep Power
 0
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.
Java 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 upperleft 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 counterclockwise. */ 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 moonshape 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); } }
Last edited by ajetrumpet; 02172013 at 03:26 AM.
 02172013, 04:02 AM #8Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,033
 Rep Power
 6
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 antialiasing. Here is a sample paint function:
Java Code:public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // // rest of code goes here. // }
Regards,
Jim
 02172013, 04:24 AM #9Member
 Join Date
 Feb 2013
 Posts
 57
 Rep Power
 0
Re: double vs. float vs. int
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 widgettype stuff like ActX components. Goes to show the difference between the families doesn't it? WOW.Last edited by ajetrumpet; 02172013 at 04:28 AM.
 02172013, 04:56 AM #10Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,033
 Rep Power
 6
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
 02172013, 05:10 AM #11Member
 Join Date
 Feb 2013
 Posts
 57
 Rep Power
 0
 02172013, 05:17 AM #12Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,033
 Rep Power
 6
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
 02172013, 05:28 AM #13Member
 Join Date
 Feb 2013
 Posts
 57
 Rep Power
 0
Similar Threads

What is the difference between float and double?
By Nosrettap in forum New To JavaReplies: 6Last Post: 09232014, 01:20 PM 
int, float, double BUT String
By hisheeraz in forum New To JavaReplies: 4Last Post: 04252011, 06:47 AM 
double and float value in java
By arvindmer in forum New To JavaReplies: 6Last Post: 05282009, 05:51 PM 
Float vs. Double
By javanub in forum New To JavaReplies: 1Last Post: 11232008, 01:11 PM 
Put float value to double
By chamalsl in forum New To JavaReplies: 3Last Post: 09202008, 05:52 AM
Bookmarks