ParseFloat() oddity

Hello,
This is one of the most bizarre things I’ve seen in
actionscript: comparing 2 numbers and it comes out wrong.
In the code below, for some odd reason
“312346.46”, parsed to a float, then multiplied by 100,
and compared to 31234646 is wrong. Seems any kind of comparison is
wrong. Yet values like: “390000.16” compared to
39000016, gone though the same thing is correct.
However, I’m not sure if this is really a
‘fix’ as I don’t even know what’s wrong, if
I wrap Math.round() around everything, it seems to be okay.
(Though, this could cause problems in the future...)
Replacing:
var balance:Number = parseFloat(inputString) * 100;
with:
var balance:Number = Math.round(parseFloat(inputString) *
100);
Any ideas?

It's not ActionScript. It's the floating point unit on your
processor, and
is evident in all languages, and many software programs.
Google for
'floating point precsion' and you'll find all kinds of info.
Here's the real reason though, in a nutshell: floats are
represented
internally in base 2. The fraction one tenth is kept
internally as a
repeating binary number: 0.00011001100110011 etc. What that
means is that
the clean number you thought you had like '2.3' is really a
messy binary and
is not quite the number you thought you had. Multiplying by
.1 is not quite
the same as dividing by 10... It is just a FOL. I remember
one time I tried
to model the solar system to scale in 3ds max and things got
really whacky
because of floating point inaccuracies.
Dave -
www.offroadfire.com
Head Developer
http://www.blurredistinction.com
Adobe Community Expert
http://www.adobe.com/communities/experts/

Similar Messages

  • Null pointer Exception with Float.parseFloat

    I need to assign a float value from a database to a variable recurrently throughout a while loop. but i am getting a null pointer exception error:
    while ( rs.next() )
         out.println("<TR>");
              float corr = Float.parseFloat((String) request.getParameter("echo"));
              out.println("<center><TD><b><font color = #990000>" + rs.getString("race_number") + "</b></TD>");
              out.println("<center><TD><b><font color = #990000>" + rs.getString("sail_number") + "</b></TD>");
              out.println("<center><TD><b><font color = #990000>" + rs.getString("finish_time_elapsed") + "</b></TD>");
              out.println("<center><TD><b><font color = #990000>" + rs.getString("echo") + "</b></TD>");
              out.println("</TR>");
    I've also tried:
    float corr = Float.parseFloat( String request.getParameter("echo"));
    float corr = 0;
    corr Float.parseFloat((String) request.getParameter("echo"));
    corr = echo;
    corr = request.getParameter("echo");
    corr = rs.getString("echo");
    corr = Float.parseFloat(request.getParameter("echo"));
    temp = rs.getFloat("Problem_Description");
    Any ideas Please!!!

    Null pointer exception means that the value you are trying to turn into a Float is null. Probably request.getParameter("echo") returns null.
    Is "echo" a request parameter, or a field you want to get from the database?
    request.getParameter() has nothing to do with a database query.
    That is your http request coming from the form submission. It won't change.
    If "echo" is meant to be a request parameter, then make sure the parameter is actually present.
    If its a database field (stored as a number) then
    float corr = rs.getFloat("echo");
    should do the trick.
    Can the value be null in the database?
    Cheers,
    evnafets

  • Forum oddity, w.r.t high resolution screen.

    I started a thread on Windows/ Photoshop entitled "CS4 with a high resolution screen". People have replied to this, according to e-mail direct response. However, it is not on the list of forums, and neither does it appear on 'search'.
    To repeat: we have a high resolution HP LP 3065 screen, which supports up to 2560 x 1600 pixels. At this resolution, images are still less than print size, but CS4 is essentially unusable because the menu text and other icons do not scale. Does anyone know if it is possible to manage this silly interface to allow the user to see what they are doing? Is there even a visual disability option? Even MS Office 2000 - ten years old - allows "large icons".
    If not, I think this is another way in which CS4 is in fact worse than CS3, which (mostly) used the Windows UI and which was therefore considerably more flexible. The screen in question is supported by dual DVI connections to an Nvidia FX3700 card - a CUDA multiprocessor device optimised for 2D images and CAD - and an eight processor, 3 GHz machine. With all of this processor power, CS4 is still much slower than CS3 on a single processor laptop. The paint follows the pen by several centimetres, like early Photoshop on a pre-Pentium processor.

    I started this by saying that through an oddity of this forum software, my original thread had vanished from the list. Exactly the same thing happened to this one, which i had to find through searching the forum list. The previous one vanished completely. One appears to get a different list if one comes in via a direct URL or via support > Forums > windows: an .asp error, presumably, but Adobe please note.
    Someone asked why use a high res screen. If you work on a large image, you either operate at the pixel level - a plate on a table top if you are at 1020 x 768 and working with a 20 megapixel image - or you shrink the thing down to fit. In the latter case, each pixel that you see is, depending on the native image size, an average of a large number of adjacent pixels. In the case of a 24Meg image and the above screen size, each visible pixel is an average (or however Adobe calculate it) of a bit over 20 pixels from the native image. If you look at 2560 x 1600 you are still seeing a composite, but of a quarter fewer source pixels per point. If you look at the natve image as 'actual pixels', the 'dinner plate' is four time bigger in proportion to that of the SVGA image.
    That said, imagine the layers pallet in which one layer has, let's say, a football set against an group of players , P/shop users will expect to see a black blob representing its position in the layer. At high resolution, however, the blob is represented on the screen by a patch that is smaller than a full stop on a page of type, and essentially invisible from 150 cm from the screen. Thus: squint, lean forward, mutter angrily.
    Why can one not set the icons and the ephemeral facilities such as the layers pallet to be any size you want? It is really very silly.

  • Context menu oddity: 2 "QuickTime Player" items on OSX context menu?

    Hi,
    This pertains to QuickTime Pro 7.0.4. I never noticed this before I upgraded to QuickTime Pro 7--when I right-click on a file that can be opened in QuickTime, I see two QuickTime Player items on the context menu. Either one opens the movie.
    More specifically--when QuickTime is specified as the default application for a file, QuickTime Player appears at the top of the context menu as well as in the list of other applications that can open the file. When QuickTime is not the default application, two QuickTime Player items appear in the list of other application that can open the file when right-clicked.
    I looked through the Contextual Menu Items folders, but couldn't find any duplicate files. This affects both my G5 desktop system and my PB G4; both are runnning the latest version of Tiger (10.4.6) as well as the latest version of DivX. On my G5, I have Final Cut Studio installed; but not on the PB.
    Anyone else experience this oddity? Any idea how to get rid of the duplicate item on the context menu?
    TIA
    M

    The contextual menu "Open With..." command uses data from the Launch Services app (part of the system software).
    Having duplicates in the menu is a very old issue and there are dozens of reasons why multiple apps show up. It could be reading a "backed up" duplicate app or multiple copies (I have 4 versions of the QuickTime Player app installed) of the software.
    Cleaning out the Launch Service list can be done with some third party software (I don't know the names) or with some Terminal commands (don't know them, either).
    Maybe a Google search or a search at http://www.versiontracker.com/macosx can help you out.

  • IPhoto to Aperture Library Transfer Oddity

    Or maybe I should have just said iPhoto Library oddity, since Aperture seems fine, and in the end, seems to have done what it should have.
    Let me explain:
    A few weekends ago, I started to transfer my entire iPhoto library from iPhoto v7.x (current version) to my Aperture v2 library. I started my iPhoto library over three years ago, using iPhoto v5 and OS X 10.4, later upgrading to iPhoto v6 via iLife ’06 in mid 2006, and finally iPhoto v7 via iLife ’08 last Fall. I then upgraded to Leopard and Aperture v2 this past Spring.
    After transferring some 46,000 photos (with most being RAW + Jpeg pairs) from iPhoto to Aperture, I noticed that a there were a large number (approximately 1,800) of stacked photos were one photo was an iPhoto Original RAW photo, and the other was an iPhoto Edited RAW photo. The curious thing is, I didn't edit all of those RAW files in iPhoto, I only edited a select few dozen of them. It should be noted that all of the edited and original Jpegs pairs were also stacked, and the orientation of all of them looked fine.
    As it turned out, all of these mysteriously edited photos were shot as verticals, none were horizontals. And when looking at the unstacked pair, the edited photo was properly rotated, but the Original iPhoto RAW photo was not, despite having its orientation listed as portrait. When I rotated an Original iPhoto RAW photo to correct it, the orientation was changed to landscape, even though the photo was displayed in portrait mode.
    Upon further digging, it seems that all of the vertical photos that were imported into iPhoto v5 were affected, as were some of the vertical photos that were imported by iPhoto 6, but none of the photos imported be iPhoto v7. I don't remember the exact dates that I imported them and when I upgraded iPhoto, but the timeline seems close.
    I also noticed recently when using iPhoto that the info display panel listed some of the RAW photos as “Jpeg (From Nikon RAW)”. I have not spot checked these files yet, but I suspect that these are the photos that resulted the creation of the Original & Edited iPhoto stacked pair in Aperture.
    I do realize that iPhoto converts RAW files to Jpegs if you edit them, and I would expect that it does not when you don’t edit them. However, it would appear that iPhoto v5 always did when it rotated them, iPhoto v6 did some times, and iPhoto v7 doesn't.
    I’m puzzled as to how these RAW files got edited in the first place.
    I can only guess that it happened as either:
    1) The result rebuilding the cache at some point, or
    2) Possible as a result of a conversion process for the iPhoto updates, or
    3) How iPhoto handled RAW files, particularity with iPhoto v5.
    But as to why these particular files were affected, and most of the vertical shots were OK, I don't know. Any Ideas?
    As for the iPhoto edited RAW photos, I plan to delete them since there doesn't seem to be a point in keeping them, because I have the original RAW file, and the original and edited Jpeg versions. They were easy to find using keyword and Aperture excellent filtering tool.
    Incidently, I've also had some problems with the "Black Frames" problem and also the "Unsupported File Format" error with photos that were directly imported into Aperture. I hope that my photos are not corrupted.
    Other than that, I'm really liking Aperture2 a lot!

    My iPhoto library shrunk drastically when I switched to Aperture, from around 13.8GB it dropped to 9GB. I was worried at first but everything appears to have showed up.
    The only things that I can attribute it to is the fact that when an image is imported into iPhoto with auto rotate data, the image is duplicated before it is rotated in Aperture it is not.
    iPhoto duplicates the image on virtually every modification.
    My iPod photo cache was quite large as well, but Aperture does not import this.
    Videos you may have in iPhoto will not be imported into Aperture.
    I also get the feeling that preview and tumbnail data is more optimized in Aperture.
    If you are unsure backup your iPhoto library to an external drive as a read-only disk image, then if you find any problems with your photos in Aperture you can go to the disk image and get them.

  • Java.lang.NoSuchMethodError: java.lang.Float: method parseFloat(Ljava/lang/

    java.lang.NoSuchMethodError: java.lang.Float: method parseFloat(Ljava/lang/String;)F not found
    WHAT WOULD MAKE THIS HAPPEN. THIS SEEMS TO WORK IN JDK1.3.1 BUT NOT IN BLACKDOWN 1.1.3_V3
    THIS IS MY CODE:
    try{
    DecimalFormat dfp = new DecimalFormat( "0.00" );
    if(!Price.equals("")) {
    float MyFloat = Float.parseFloat(Price);
    Price = dfp.format(MyFloat);
    }catch(NumberFormatException nfe){
    PriceValid = 1;

    This error says:
    - There is no method called "parseFloat" that receives a String as parameter in class java.lang.Float.
    Look the method "parseFloat" at the class java.lang.Float!!

  • Display oddity- What in the world?

    A grand total of two times, the screen on my MacBook Pro (13' Mid 2010) has demonstrated the symptom in the picture below.  Both times, in conjunction with Mail.app being in use and the "frontmost" application.  Quitting Mail would remove part of the funky display, but any further opened windows would also show some of this oddity.  A reboot fixed both times. It has been nearly two months since the last occurence.
    Here is my GPU info:
    Chipset Model:          NVIDIA GeForce 320M
      Type:          GPU
      Bus:          PCI
      VRAM (Total):          256 MB
      Vendor:          NVIDIA (0x10de)
      Device ID:          0x08a0
      Revision ID:          0x00a2
      ROM Revision:          3533
    I do not have AppleCare.  Warranty expired back in January of this year.
    As I stated - has only happened twice - most recent was right about two weeks after the warranty expired!

    Yes, you are seeing that correctly - a perfectly clar right angle/rectangle.
    That's why I was pondering that it was a code glitch - the only affected area of the screen is the partial content of the mail.app window - and only the portion of the window where the mail message would be displayed (no message was selected at the time - so nothing was in that part of the window).
    And as I said - it happened both times on launch of Mail.app.  Never has affected any other apps - just the two times (separated by nearly 2 months) launching mail.app.

  • Different display oddity on New iMac i5 27" : moire/pixel shading

    Hello,
    I just got my iMac Core i5 and I'm happy to say I have not had any of the many display problems that others seem to have.
    However I have noticed a display oddity that's quite subtle but bugs my eye nonetheless:
    It's easier to see in the grey of the window tops, the Safari window is a perfect candidate. You have to be staring at the screen pretty closely. You can only see it while moving your head, oddly enough, or moving the window. On my display it's most prominent at the top of the display and it fades out completely by the bottom. It looks like the grey goes pixelated in mini checkerboard pattern, almost a moire. It's not just the grey or the window, I believe it is visible in any medium dark solid color.
    I've seen it on other, older, iMacs, and I had come to think of it a side effect of the cheap 6-bit TN panels they were using on the older iMacs but this panel is supposed to be a good IPS 8-bit. Also, I cannot see the effect on a 24" Core Duo 2 2.93Mhz. Could it a difference in video card, nVidia VS ATI perhaps ?
    It's not terrible for normal use but it keeps catching my eye as I move my head to look away from the screen so it's becoming a bit annoying.
    Anyone seeing what I'm seeing ?
    Is the problem with my eyes ?
    Can anyone shed any sort of technical explanation ?
    Should this be considered a defect ?
    Thanks

    Indeed it's not. But I don't think it qualifies as something to worry about either. While I suspect others may be able to see what I'm seeing it doesn't seem to be something that gets noticed. Indeed I wouldn't be surprised if most wouldn't notice at all.
    Don't let me spoil your expectations as one may be driven to look for minute imperfections once one knows where to find them. Those computers are nothing short of marvelous. I'm a pixel-peeping photographer and I had been looking forward to those LED lit displays for even and consistent colors. They deliver a great image, I might simply be starring to closely at it.
    I'm not about to send mine back with this a defect I was mostly curious as to how noticed of an effect it is, if it is apparent to most people and what it could be caused by.

  • Error Converting "1,300" to Float using Float.parseFloat()

    I need to convert a string in the format xx,xxx.00 to a float value.
    But I get java.lang.NumberFormatException by using Float.parseFloat("xx,xxx.00")
    What can I do?

    Look at java.text.NumberFormat to parse numbers in a specific format.
    regards,
    Owen

  • Issue with Float.parseFloat

    Hi,
    I tried the following code. Float.parseFloat("23d")does not throw an exception. The below code prints output as 23.0.
    String s = new String ("23d");
    try{
    System.out.println(Float.parseFloat(s));
    catch(Exception e){
    System.out.println("not a number..");
    How can I make Float.parseFloat("23d") throw an exception? Please help.
    thanks.
    roshni.

    here the d determine that this is double variable.
    so this is not give any exception.
    if you use any other character then it throw the exception.

  • Regarding Float.parseFloat method

    Hi,
    i have two text boxes,one is Quantity another Price i want Qantity as integer and Price as float while giving input.
    if user giv other than those i need to display error for that i done for Integer.parseInt(getparameter(..)) for this if the string is not int,it raising exception but for Float.parseFloat(getparameter(..)) for this it is not raising any exception.i am not getting what is the reason for that
    Thanks in advance
    nagaraju

    if(this.price==null||this.price.equals(""))
    ActionError ae1=new ActionError("pricereq");
    aes.add("price",ae1);
    else{
    *try{*
    *System.out.println(this.price);*
    *Float.parseFloat(this.price);*
    *catch(Exception e)*
    *ActionError ae2=new ActionError("pricenotfloat");*
    *aes.add("price",ae2);*
    }I wrote that code in struts to validate the form, in that when i entered the integer value in price text field it must show price must be float. but exception not raising in that highleted part
    i w

  • Float.parseFloat

    I am compiling my code with jdk 1.3 and getting this error when I compile:
    Error: cannot resolve symbol: method parseFloat (java.lang.String) in class java.lang.Float
    My class path:
    .;C:\jdk13\lib\;C:\jdk13\lib\tools.jar;C:\jdk13\lib\dt.jar;C:\jdk13\jre\lib\rt.jar;C:\jdk13\jre\lib\i18n.jar
    And here is my method...
    public void setGrandTotal(String f){
    this.grandTotal=Float.parseFloat(f.trim());
    Can some one tell me why can not resolve parseFloat
    Thank you

    When I compile your method in isolation, it compiles fine. Have you checked for syntax errors above it in the code? Does the code compile clean if you comment out that method?
    Normally you do not need to specify the java libraries in your classpath. Have you tried compiling with "javac -classpath . file" ?

  • Bug in parseFloat???

    Hi,
    Before submitting this as a bug I'd like some feedback from others. In a program I'm writing I'm reading floating point numbers (currency) as strings and convert them to floating point numbers.
    During a debugging session because one of the calculations gave an error I found out that the floating point parser (Float.parseFloat(String)) isn't working properly:
    System.err.println("Static parse 151647.99 " + Float.parseFloat("151647.99"));The output is:
    Static parse 151647.99 151647.98
    Now what the heck is going on here?! Can others confirm this or maybe even explain what this is?

    I'm already using doubles everywhere as I found out
    seconds after my post that they work fine :)
    BigDecimal is for the next project as this is an
    update of a 3 year old program but I'm surprised to
    hit the bit boundary of floats so fast.
    At least I'm switching to doubles or BigDecimals on
    my other projects because I don't want to spends
    hours of debugging to find out that the parsr itself
    is the cause of the calculation error.
    Thanks! :)Still, if you are using floats or doubles for currency you should read the article posted by JoachimSauer or you may still encounter some unexpected rounding issues.

  • GREP Oddity

    Every time a GREP doesn't work as expected, it is an oddity until someone who knows explains. Here's what I had set up for a GREP style. I have a character style named AllSmallCaps which simply assigns the AllSmallCaps Open Type feature to the text. I wanted to apply it to all words of two or more characters that are set in all caps (the paragraph styles in question use Minion Pro, so the style will work if the GREP works). I started out with:
    \<\u\u+\>
    and that works. But then I ran into cases where a word had a hyphen in it and another had an underscore, so I changed my GREP to:
    \<\u[\u_-]+\>
    and that also works. But next I realized that I wanted this to work for mixed number/letter combinations (as long as they start with a letter). So I tried:
    \<\u[\u_-\d]+\>
    And, not only did that not work, it caused "Pa" to be converted to small caps. I've changed my GREP to:
    \<\u[\d\u_-]+\>
    And that works properly. Perhaps there's some rule that says if you're including wildcards in a square bracketed list they must be at the head of the list, but I don't understand how that second to last version caused "Pa" to be changed. Any GREP experts handy?

    The hyphen causes the problem.
    >[\u_-\d]
    is "upper case and everything inbetween "_" and "\d", just as
    >[a-z]
    is "everything between "a" and "z". The common workaround is placing the hyphen either first or last in the selection braces.
    Granted, it does not explain why lower case characters fall between underscore and digits. Must 've something to do with the internal workings.

  • Login Oddity at New Forum

    Found an oddity with using two computers... home and work
    Logged in last night at home... software welcomes me BOTH at the very top left, just my name, AND just below the white letters on a black rectangle... with my avatar and name
    Came to the forum this morning at work... said "Welcome John T Smith" at the very top left
    My avatar and name did NOT show below the black rectangle, and I was not able to post a message
    I went to the login page... and now I show in both places and may post
    I will bet I won't be fully logged in when I get home
    There seems to be a process in the login/cookie that only allows ONE active login

    Ok... top and forum login being either different or not synchronized is exactly my point
    I'll check when I get home to see which of the two is still working on my home computer
    IF it is as I described here, that means that every time I start my "other" computer I'm going to have to login to the forum... and doing that will break the login on the other computer
    Oh well... one more thing about the new software

Maybe you are looking for