A problem using doubles/floats

i have an applet with 4 radio buttons and 5 check boxes! when i click on some (mix and match) the value on the text field sometimes goes like this 8.8888889 etc, and other times it is like 2.34! how can i change that 8.88889 to be displayed as 2.34! i have tried changing the value in the code, but that solves the problem but then a diff combination does the same thing! :(

But anyway, since we're in this thread now...
The short version is that there are a finite number of values that can be stored in a float or double, but there are infinitely many float/double values in the real numbers in the range between he min value and max value. So a lot of numbers are going to be aproximations.
Why is a "simple" number like, say, 0.9, not exact, you ask? Java's VM (and pretty much all modern computer hardware) stores floating point number in base-2, not base ten. So if you look at the bits, and you see "1" in the first position after where the binary-point (base-2 equivalent of a decimal point) would be, that doesn't mean 1/10, it means1/2. So "0.5" in decimal (base-10) is the same as "0.1" in binary (base-2). What's "0.1" base-10 in base-2? Don't know off the top of my head, but it's a repeating fraction, just like 0.3333333... for 1/3 (which, in base-3, is 0.1, exactly).
For more details, see:
http://docs.sun.com/source/806-3568/ncg_goldberg.html

Similar Messages

  • Using double/floats for co ordinates?

    hi is it possible to use float/double values for the following code or simlar?
    g.drawImage(cards, 0.5, 0.8, this);
    does the drawimage on support ints?

    Update and demo: Am I the only one who is forever tweaking rendering hints
    to get the right effect? Anyhowdy, I think INTERPOLATION was the hint I needed
    to tweak to get subpixel shifting of images:
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import javax.swing.*;
    public class ShiftyExample extends JPanel {
        private BufferedImage image;
        private float delta;
        public ShiftyExample(BufferedImage image, float delta) {
            this.image = image;
            this.delta = delta;
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2.drawRenderedImage(image, null);
            g2.drawRenderedImage(image, AffineTransform.getTranslateInstance(delta, 0));
            g2.dispose();
        public static void main(String[] args) {
            EventQueue.invokeLater(new GuiBuilder());
        static class GuiBuilder implements Runnable {
            public void run() {
                BufferedImage image = createImage();
                JPanel cp = new JPanel(new GridLayout(1,0));
                cp.add(new ShiftyExample(image, 0f));
                cp.add(new ShiftyExample(image, 0.5f));
                cp.add(new ShiftyExample(image, 1f));
                cp.add(new ShiftyExample(image, 1.5f));
                cp.add(new ShiftyExample(image, 2f));
                JFrame f = new JFrame("ShiftyExample");
                f.setContentPane(cp);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(300,200);
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            BufferedImage createImage() {
                BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
                Graphics2D g = image.createGraphics();
                g.setColor(Color.BLACK);
                g.drawLine(10,0,10,100);
                g.dispose();
                return image;
    }Explanation: the image is a vertical black line on a clear background.
    What you see on the frame are five versions of the image displayed
    against a shifted copy of itself, shift 0, 0.5, 1, 1.5 and 2 units. If you really stare at the
    fractional examples, you will see gray pixels being used instead of just black
    and white. Woop-de-doo...

  • My airport extreme has connected to a Tplink router, i set my AE in Create network, so i get a problem of Double NAT. How can i repair this probleme because i have to use TPlink for my IP TV and i want to use AE to creat my network and the guest NW.

    my airport extreme has connected to a Tplink router, i set my AE in Create network, so i get a problem of Double NAT. How can i repair this probleme because i have to use TPlink for my IP TV and i want to use AE to creat my network and the guest NW. If we don't have a solution for this question, can i set my AE to use the IP TV and how??? I'm using a 4th Airport extreme

    No, not unless it is simply a network connection.
    How is your IPTV being provided.. if it is using a separate vlan or separate vpi/vci in the adsl connection. Who is your ISP?
    Is the TP-Link ADSL?
    Is the IPTV using a separate voice channel on adsl?
    Or a different vlan.
    Sorry but it is something where a definite answer is not possible unless you provide all the details.
    The setups are unique to each ISP around the world..
    Here is one for our local ISP.
    http://www.avenard.org/iptv/Setup.html
    He has a section on getting this working with apple router over wireless.. look
    http://www.avenard.org/iptv/IPTV_and_Wireless.html
    Google around and see if other people have had success.. the whole setup is tricky.. and it is hard if nobody has used it who actually understands networking.

  • How to convert value from exp. value to double/float???

    Hi all,
    I am fetching values from database, which is of type double / float.
    eg. values will be like -22,777,548 will be stored in database.
    NOw if i use
    float a= rs.getFloat(1);
    out.println("a="+a);
    I get output as a= -2.2777548E7
    I want to remove "E" from out put value and display as it was given in DB.
    How to solve this problem?
    Please Help!
    Regards
    Ashvini

    What do you mean - 'how does it work' ? All format classes (MessageFormat, DecimalFormat , SimpleDateFormat etc) are used to convert data from one form to other. Go through the java.text.* api. They work based on some pattern searches, pattern matches etc - dont know in detail :)
    As for the other things, thanks :)
    cheers,
    ram.

  • Double - Float casting or Why isn't there float sqrt?

    Is there any reason why not to include float sqrt? I'm pretty pissed since, it is just... well plain stupid. I hope it can be added, not to mention that casting takes away performance, not much, but I have to do a lot of casting since I'm doing 3d grahpics... Anyone know any solluions? Is there any point going the hard way and implementing own sqrt for float?
    WHY WHY WHY, OH WHY.
    Something is rotten in the state of java.

    The absence of Math.sqrt(float) and float versions of other relevant methods is apparently becuase early versions of the Java compiler could not distinguish overloaded methods which only differed in double/float parameters. The current compilers do not have this problem and it has been proposed that the extra methods should be added but I don't know when this might happen.
    In any case how sure are you that casting between double and float is causing any significant performance problem? This cast is NOT particularly expensive (casting to 'int' from float or double is usually much more expensive).
    What processor are you using --- as far as I know Intel processors do not give any speed benefit from doing sqrt on float instead of double. I believe that many RISC processors do benefit from using float where possible.

  • JDeveloper IDE Crashing using double click on DataAction in struts-diagram.

    Hello,
    Has anyone had this problem with Jdeveloper10g?
    We have a workspace for an application that has grown fairly large. The
    problem happens when we try to drag a DataAction from the Component Palette
    onto the struts-diagram when in the the JDeveloper10g IDE and then
    double click the icon to create a data action. We get this message:
    An Error has occurred in the Java VM
    An operation has failed because an error has occurred in the Java VM. The current
    activity may fail and the system may have been left in an unstable state. The following
    is a stack trace.
    java.lang.StackOverflowError
    If further errors occur, you should restart the application.
    If the error was an OutOfMemoryError, try closing some components, such as some
    diagrams, or increase the Java heap size.
    Also, report the problem on the JDeveloper forum on otn.oracle.com, or contact Oracle
    support, giving the information in this message.
    The Version of JDeveloper we are using is: 10.1.2.1.0 Build 1913 but we also had the
    same problem using this same workspace in Build 1811. If we try a different workspace
    there is no problem and we don't see the issue. Neither do we see the problem if we drag
    either a DataPage or a plain Action from the Component Palette and the double click
    either of those to create a Jsp page or an action.
    We have spent hours using diffing software recursively on the workspace directories trying
    to determine what may have gone wrong. We have eliminated a great deal from the problem
    workspace including deleting everything from the struts-diagram (In a copy of the ws of course).
    We really can't afford to rebuild the workspace from scratch and this problem causes us a
    significant headache. It seems like a serious bug in JDeveloper itself.
    Any help would be appreciated.
    Thanks,

    Problem Solved! This condition is caused by a circular dependency set up when the view was checked as a depndency for the model. We had a small class in the view that was being used in the model so the model was made dependant on the view and this causes an infinite loop condition of some sort when double clicking on a newly dropped in DataAction as described below.

  • Problems using Animation Presets from Bridge

    Dear all,
    Using the CC range, and recently had a new problem using the animation presets from Adobe Bridge. I'm in After Effects as normal, and then go to the Animations drop down, click browse recent presets to open up Bridge, and then as normal use Bridge to select the preset I want. Double click it whilst highlighting my selected track in AE, and then AE reports this:
    And the same error codes no matter what effect/animation I choose, whether its to an image, video or text.
    If I just go, in AE, to my Recent Animation Presets, and choose one from there it loads up absolutely fine. But this is massively annoying.  Any ideas? This problem appeared out of nowhere.

    This is fixed in the After Effects 2014.2 (13.2) update, which is now available.
    Details here: http://adobe.ly/1IVopxh

  • Problem Using Aperture as an External Editor

    I'm having a problem using Aperture as an external editor for iPhoto. I've set preferences to "Edit in Aperture" and selected the Aperture application. When I click edit a photo in iPhoto, Aperture fires up as expected, but it does not load my photo. Nothing seems to be exported from iPhoto into Aperture.
    I can export the same photo to my desktop and then load it into Aperture fine.
    When I set preferences to edit in CS2, this works fine, only Aperture seems to be the problem.
    What am I doing wrong?
    I'm using the following versions:
    - iPhoto 7.0.2
    - Aperture 1.5.4

    If you have Photoshop CS2 then you'd have a better editor using it within iPhoto as follows:
    Using Photoshop (or Photoshop Elements) as Your Editor of Choice in iPhoto.
    1 - select Photoshop as your editor of choice in iPhoto's General Preference Section's under the "Edit photo:" menu.
    2 - double click on the thumbnail in iPhoto to open it in Photoshop. When you're finished editing click on the Save button. If you immediately get the JPEG Options window make your selection (Baseline standard seems to be the most compatible jpeg format) and click on the OK button. Your done.
    3 - however, if you get the navigation window that indicates that PS wants to save it as a PS formatted file. You'll need to either select JPEG from the menu and save (top image) or click on the desktop in the Navigation window (bottom image) and save it to the desktop for importing as a new photo.
    This method will let iPhoto know that the photo has been editied and will update the thumbnail file to reflect the edit..
    If you want to use both iPhoto's editing mode and PS without having to go back and forth to the Preference pane, once you've selected PS as your editor of choice, reset the Preferences back to "Open in main window". That will let you either edit in iPhoto (double click on the thumbnail) or in PS (Control-click on the thumbnail and seledt "Edit in external editor" in the Contextual menu). This way you get the best of both worlds
    Do you Twango?
    TIP: For insurance against the iPhoto database corruption that many users have experienced I recommend making a backup copy of the Library6.iPhoto database file and keep it current. If problems crop up where iPhoto suddenly can't see any photos or thinks there are no photos in the library, replacing the working Library6.iPhoto file with the backup will often get the library back. By keeping it current I mean backup after each import and/or any serious editing or work on books, slideshows, calendars, cards, etc. That insures that if a problem pops up and you do need to replace the database file, you'll retain all those efforts. It doesn't take long to make the backup and it's good insurance.
    I've written an Automator workflow application (requires Tiger), iPhoto dB File Backup, that will copy the selected Library6.iPhoto file from your iPhoto Library folder to the Pictures folder, replacing any previous version of it. It's compatible with iPhoto 08 libraries. You can download it at Toad's Cellar. Be sure to read the Read Me pdf file.

  • Double Float Margin Bug "error"

    I did a browser compatibility check on one of the pages that
    I am building and got this error.............
    "Line 112, Double Float Margin Bug 'error'
    When a margin is applied to a floated box on the same side as
    the direction of the float, the margin is doubled. This bug only
    affects the first float in a row of one or more floats.
    Affects: Internet Explorer 6.0
    Likelihood: Very Likely"
    I have many thumbnails for a gallery page in their own divs,
    within the content div, and they are floated in there.
    When I viewed solutions, they suggested:
    "When a margin is applied to a floated box on the same side
    as the direction the box is floated AND the floated box is the
    first one inside its containing box, the margin is doubled. (In
    other words, a left-floated box with margin-left set to move it
    away from its containing box will be twice as far away from the
    left edge of the containing box as intended.)
    SOLUTION
    Specify display: inline; for the affected float.
    DETAILED DESCRIPTION
    This bug only reproduces when the floated element's margin
    extends in the same direction as the float itself, and is between
    the float and the container box's inside edge. Subsequent floats
    with similar margins won't reproduce this bug- just the first float
    in any row of floated elements."
    Does anyone have any solutions, suggestions as how to fix
    this? I will attach my code. And if anyone has a better solution as
    to laying my thumbnail divs in another div container, please say
    so. The reason I am putting them in separate divs is because that
    is what someone suggested to use them with Lightbox2. He said they
    needed some kind of special CSS to enable some of the functions and
    they (the thumbnails) were easier to style if they were in their
    own separate divs. I'm open for any suggestions.
    Thanks!

    Just add "display:inline" to the styling like this:
    .image {
    background-color: #121922;
    height: 100px;
    width: 100px;
    float: left;
    margin: 20px;
    display: inline;
    }

  • When i import movies from my iPhoto app to my iMovie app does it make a copy (thereby using double the hard disk space) or does it link to my iPhoto app? The reason i ask is i want to archive all my videos from iPhoto/iMovie. HELP!

    When i import movies from my iPhoto app to my iMovie app does it make a copy (thereby using double the hard disk space) or does it link to my iPhoto app? The reason i ask is i want to archive all my videos from iPhoto/iMovie. Do i need to archive the movies in iPhoto AND iMovie? HELP!

    Hi - I'm having the same problem with freeing up space on the HD. If I can't free space if I'm using any part of the clip, how can I split up the clips so that I can discard the part I won't use? This is crazy! I have only 4 GB of free space left!
    Also, I've tried to transfer the project file to my external HD but I keep getting an error saying I can't transfer it. I've been told this is because I don't have enough space on the HD for the transfer. I tried a similar transfer with another computer with plenty of HD space but the same error occurred. Is there a way to break up the project file into small pieces for the transfer (I'm thinking the whole movie file is just too big)?
    Thanks for any info!

  • How to use Double selection If statements?

    Ok, this is what I have to do:
    Present a menu to the user from which there will be 6 choices, A = add three number, S = subtract 2 numbers, M = multiply three numbers, D = divide two numbers, O = modulus two numbers, E = Exit.
    2) If the user enters an A,S,M,O,D the program will prompt the user for numbers and perform the needed math on them.
    3) if the user enters an E then the program will thank the user, and exit.
    4)the program HAS to use Double Selection If Statements (no idea how to do these)
    5) the program should use float point numbers for all the numbers entered, besides modulus.
    heres what i have so far.. -_-
    import javax.swing.JOptionPane;
    public class Ifprogram
    public static void main (String[] args)
      String Intro;
      String A;
      String S;
      String M;
      String D;
      String O;
      String Exit;
      Intro = JOptionPane.showInputDialog(null, "Enter A to add 3 numbers \n S to Subtract two numbers \n M to Multiply three numbers \n D to Divide two numbers \n O to Modulus two numbers \n and E to Exit.");
       A = JOptionPane.showInputDialog(null, "You selected to add 3 numbers. Enter the first digit to add.");
       S = JOptionPane.showInputDialog(null, "You selected to substract two numbers. Enter the first digit to subtract.");
    }IM SO LOST.
    If anyone could give me some tips on what to do, or where to get, that'd help A LOT! Thank you.

    I can see that working, but
    if(user entered 'A') {
                        A = JOptionPane.showInputDialog(null, "You selected to add 3 numbers. Enter the first digit to add.");
                    } else if(user entered 'S') {
                       S = JOptionPane.showInputDialog(null, "You selected to substract two numbers. Enter the first digit to subtract.");
                   }Is not working..
    The errors i get:
    16 ')' expected
                   if(user entered 'A') {
                                    ^
    21: illegal start of expression
                    ^
    2 errors
    Tool completed with exit code 1Edited by: JackJ11 on Mar 16, 2010 4:29 PM

  • Programming problems with INT / FLOAT

    I got a problem with datatypes float and int.
    I'm just developing a c-program on HP-UX with the gcc-compiler. 'cause i have not ever been done an ORACLE-access before, i has started with the "cdemo2.c" - example from the oracle-demo-dircetory in version 8.0.4.
    in the DB there's a column with NUMBER-TYPE, but when i call the oci-function (or what ever it is) "odescr()", the variable "scale" (which is supposed to differentiate between FLOAT_TYPE and INT_TYPE) always is set to "0". this variable should be set to anything else but "0" if a FLOAT_TYPE is detected, but it isn't.
    what I do wrong ???
    How can I know the exact datatype, when a NUMBER_TYPE in the DB appears ???
    if there is a better way to realize an oracle-access in C, please don't wait to tell it to me
    many thanks
    null

    You basically got it. Another approach is to always work on the rightmost digit; that way you'll always be dividing, mod'ing, or multiplying by 10.
    You don't need to know the length of anything to use a for loop. Remember that a for loop has 4 parts: the initialization, the conditional, the "update", and the body. (There are probably more correct names for these that I can't recall right now.) The conditional and the update tend to be length checks and increments only because for loops are commonly used over arrays or strings, but they don't have to be.
    Another hint: how do you know when you're done pulling the digits out of the source number? What is the value of the source number when you're done?

  • Using double frame mode with a Hamamatsu C8484-05 and a PCI 1426

    Hi,
    I am using a hamamatsu C8484-05 CCD camera (http://sales.hamamatsu.com/en/products/system-division/machine-vision/part-c8484-05.php), with a PCI-1426. 
    Despite the NI site saying that they are compatible, Hamamatsu tell me that the camera is only compatible with the 1426 using the NI drivers , and not the Hamamatsu vi's. (http://sales.hamamatsu.com/index.php?id=13200492&language=1&)
    I located the .icd file for the C8484-05 on the NI website, and using IMAQ, i can control most of the settings using the set camera attribute vi. 
    However, there does not seem to be an option to set double exposure mode for PIV (i.e. 2 frame 2 exposure using double buffering)
    Do I have to do this manually using the low-level buffer controls? For a previous camera (IDT X3) I was able to select this option in their provided VI.
    If so, could you point me in the direction of some good tutorials for the use of buffers? 
    Additionally, is there an easy way to get all of the options without copying the text from each menu in MAX and using multiple case structures?
    Kind regards
    Joe Sherwood
    Solved!
    Go to Solution.

    Hi Zeyad,
    The HiSense MkII is just a DANTEC relabelled C8484-05C, apparently it has some differences, but I have found none (except the price tag!). For me, the icd file in the previous post (and this one) work for both cameras.
    The file I uploaded was a quick fix, but some of the options were not available when in PIV mode. The attached file is one i I improved at a later date, so you may have more luck with it (see note below) When you say you open it with IMAQ, what do you mean specifically? Vision Assistant, or IMAQ within labview? I personally check functionality using MAX, via the acquisition card (PCI 1426).
    So to answer your questions,
    1) If you just mean have I used it in LabVIEW, yes. It also works in Vision assistant and MAX
    2) I have used it to acquire double frame images with no problems
    I have used the camera at full exposure, and it was fine, but of course this is dependent on the illumination. If you are using laser pulses and your room is sufficiently dark the exposure should not matter.
    I should mention that the one problem I have had is that IMAQ within labview seems incapable of changing the camera mode from double to single, so I have to change it in MAX (I had a guy from LabVIEW look at this, but he couldn't solve it).
    Perhaps I could help more if you gave me a little more idea about your setup/application/ details of the errors etc.
    Regards
    Joe
    I could not upload an icd file (interesting choice from NI), so I have saved it as a txt, you will just have to odify the extension to use it
    Attachments:
    Hamamatsu C8484-05-C2.txt ‏74 KB

  • SQL*loader support database columns Of Binary Double/Float

    Does SQL*loader support database columns Of Binary Double/Float when applying Direct Path?

    Why not use Google and find out for yourself.
    The first hit on Google is this
    Go see for yourself
    Regards
    FJ

  • Problem using Mosaic Screensaver

    Like many, I find the Mosaic screensaver included with Leopard very cool. However, I have a problem using it and wonder if anyone has a workaround. First of all, my iPhoto library is on an external FW HD and has 25-30,000 photos. When I first tried to setup Mosaic, the test under System Prefs worked fine, but in the real world all I got from Mosaic was a black screen and a locked-up system. Sometimes I had to reboot to interrupt it. So I exported 1500 selected photos, disconnected the FW HD, and created a new, smaller iPhoto library. This worked great - Mosaic ran perfectly with no lock-up. Unfortunately, when I reconnected the FW HD, Mosaic started re-indexing and adding in the 1000s of photos from my original library. Back to square one.
    Does Mosaic have a preferences file or some other way to set a limit on the number of photos it uses, or to turn off re-indexing when it detects new photos? Anyone have a similar or larger number of photos and doesn't have this problem? Any other solutions?

    I guess I'm not making myself clear. This is not an issue with managing iPhoto libraries or albums. I've used iPhoto Library Manager, iPhoto Buddy, and my library has dozens of albums. I know how to select a library by pressing Option + iPhoto, double-clicking an iPhoto library, etc., etc.
    I apologize for not making myself clear. The problem is not with managing iPhoto libraries, rather it is with Mosaic automatically updating its database when it detects an additional source of photos. Let me explain again:
    I have one large iPhoto library on an external HD, and a small library on my main HD. I setup Mosaic using the small library and with the external HD detached from my laptop. Under these conditions Mosaic works fine. No problems. However, if I run Mosaic with my external HD attached, the screen goes black and the message "Creating Mosaic Database..." appears together with a progress bar. All of this occurs without iPhoto running at all. After a long wait the database update finishes and Mosaic will no longer run because there are too many photos in its database (I presume). THIS is the problem.

Maybe you are looking for

  • How do I transfer itunes purchases to another hard drive

    how do I transfer itunes purchases to another hard drive

  • Making a backup to be used on a different Mac

    Hey there. I am just about to sell my iMac 20" and then purchase a Powermac G5. However, I have a ton of personal files (music, pictures, videos) as well as a ton of programs for personal and work use (Photshop, After Effects, etc.) Now, backing up m

  • New Dell XPS PC 03/27/11

    So i bought this new Core i5 8300 Dell desktop a few days ago (march 27), this model was affected by the sandy bridge recall. I asked in the store if it still had the issue or not and they couldn't tell. I took the risk and bought it, i called dell s

  • Change default character set of JVM

    Is there a way to change the default character set of JVM to say, UTF-8? System.out.println("Default Character Set: " +  new java.io.OutputStreamWriter(new java.io.ByteArrayOutputStream()).getEncoding()); System.out.println("File Encoding: " + System

  • Error in code, need help

    import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CropProfit2 extends JApplet implements ActionListener private JTextField n1Text = new JTextField(); private JTextField n2Text = new JTextField(); private JTextField n3Text