Inconsistent Floating Point Math and NaNs on Windows Laptops?

All -
I am seeing some very strange inconsistent floating point calculations on Windows Laptops, and I am wondering if anyone has any ideas. Read on, as (to me!) it's very interesting....
I have attached a segment of our code, along with some sample output. Looking at the code and the output, it seems like it's totally impossible.
With supposedly non-NaN and non-infinite double values, I am seeing unrepeatable and inconsistent math - the below example only illustrates one such case where we're seeing this behavior.
If you look at the code below, you will see that I do things like:
double rhoYo = ...  // some math
double rho = ...  // exact same mathStrangely enough, I might get rhoYo = 1.51231231 etc and rho = NaN.
If I reverse those lines (vertically), then again, rhoYo comes out good and rho comes out NaN; however, this is unpredictable and inconsistent. If I project a source point, get a destination point with NaNs as a result, and project the source again, the second destination point may be just fine. Matter of fact, i can put a loop in the code such as:
      double rho = Double.NaN;
      for( int i = 0; i < 10; i++ )
        rho = my_earthRad * my_F / Math.pow(Math.tan(Math.PI/4.0 + latRad/2.0), my_n);
        if( ! Double.isNaN( rho ) )
          break;
        System.out.println("NaN'ed rho");
      }and rho will eventually become non-NaN (random # of iterations)!!
How's that possible? Our code might be tromping on memory somewhere, but this sure seems crazy to me, especially considering that
we're only using local variables. Anyone know of floating point errors on Windows Laptops?
With the exact same codebase, this behavior ONLY happens on Windows Laptops, including brand new Dells, old Dells, IBM, Intel and AMD chips (I've tried several ;-). It does NOT happen on Mac or Linux, including the Linux side of a Linux/Windows dual-boot (where it does happen with the Windows side). Even more strangely, it does NOT happen with Windows desktops. I have tried several 1.5.x JVMs, webstart vs no webstart, etc, to no avail. Always and only on Windows Laptops.
Please help.... ;-) and thanks in advance.
Sample code:
public class Projection
  protected Point2D.Double _project(Point2D.Double srcPt, Point2D.Double dstPt) {
    final double my_degToRad = Math.PI / 180.0;
    final double my_originLon = -95.0;
    final double my_originLonRad = my_originLon * my_degToRad;
    final double my_originLat = 25.0;
    final double my_originLatRad = my_originLat * my_degToRad;;
    final double my_stdLat1 = 25.0;
    final double my_stdLat1Rad = my_stdLat1 * my_degToRad;
    final double my_earthRad = 6371.2;
    final double my_n = Math.sin( my_stdLat1Rad );
    final double my_F = Math.cos( my_stdLat1Rad ) * Math.pow( Math.tan( Math.PI / 4.0 + my_stdLat1Rad / 2.0 ), my_n ) / my_n;
    final double my_rhoZero = my_earthRad * my_F / Math.pow( Math.tan( Math.PI / 4.0 + my_originLatRad / 2.0 ), my_n );
    if ( Double.isNaN( my_n ) || Double.isNaN( my_F ) || Double.isNaN( my_rhoZero )) {
      return new Point2D.Double(Double.NaN, Double.NaN);
    if( Double.isNaN( srcPt.x ) || Double.isNaN( srcPt.y ) )
        System.out.println("======= _project received a srcPt with NaNs. Returning NaN point.");
        Point2D.Double nanPoint = new Point2D.Double();
        nanPoint.x = nanPoint.y = Double.NaN;
        return nanPoint;
    if( Double.isInfinite( srcPt.x ) || Double.isInfinite( srcPt.y ) )
        System.out.println("======= _project received a srcPt with isInfinite. Returning NaN point.");
        Point2D.Double nanPoint = new Point2D.Double();
        nanPoint.x = nanPoint.y = Double.NaN;
        return nanPoint;
    //  Inputs are lon, lat degrees.
    final double lonRad = srcPt.x * my_degToRad;
    final double latRad = srcPt.y * my_degToRad;
    final double theta = my_n * (lonRad - my_originLonRad);
    // One Std lat -- tangential cone.
    final double rhoYo = my_earthRad * my_F / Math.pow(Math.tan(Math.PI/4.0 + latRad/2.0), my_n);
    final double rho   = my_earthRad * my_F / Math.pow(Math.tan(Math.PI/4.0 + latRad/2.0), my_n);
    // Computes kilometers in lambert space.
    dstPt.x = rho * Math.sin(theta);
    dstPt.y = my_rhoZero - (rho * Math.cos(theta));
    // WANK - Here's the problem!  These values shouldnt be NaN!
    if( Double.isNaN( dstPt.x ) || Double.isNaN( dstPt.y ) )
        System.out.println("======= A _projected dstPt has NaNs. Dumping...vvvvvvvvvvvvvvvvvvvvvvvvvvvv");
        if( Double.isNaN( dstPt.x ) )
            System.out.println("======= dstPt.x is NaN");
        if( Double.isNaN( dstPt.y ) )
            System.out.println("======= dstPt.y is NaN");
        System.out.println("======= my_stdLat1 = " + my_stdLat1 );
        System.out.println("======= my_n = " + my_n );
        System.out.println("======= my_originLonRad = " + my_originLonRad );
        System.out.println("======= my_F = " + my_F );
        System.out.println("======= my_earthRad = " + my_earthRad );
        System.out.println("======= lonRad = " + lonRad );
        System.out.println("======= latRad = " + latRad );
        System.out.println("======= theta = " + theta );
        System.out.println("======= Math.tan(Math.PI/4.0 + latRad/2.0) = " + Math.tan(Math.PI/4.0 + latRad/2.0) );
        System.out.println("======= Math.pow(Math.tan(Math.PI/4.0 + latRad/2.0), my_n) = " + Math.pow(Math.tan(Math.PI/4.0 + latRad/2.0), my_n) );
        System.out.println("======= rho = " + rho );
        System.out.println("======= rhoYo = " + rhoYo );
        System.out.println("======= Math.sin(theta) = " + Math.sin(theta) );
        System.out.println("======= dstPt.x = " + dstPt.x );
        System.out.println("======= Math.cos(theta) = " + Math.cos(theta) );
        System.out.println("======= my_rhoZero = " + my_rhoZero );
        System.out.println("======= (rhoYo * Math.cos(theta)) = " + (rho * Math.cos(theta)) );
        System.out.println("======= my_rhoZero - (rhoYo * Math.cos(theta)) = " + (my_rhoZero - (rho * Math.cos(theta)) ));
        System.out.println("======= dstPt.y = " + dstPt.y );
        System.out.println("======= A _projected dstPt had NaNs. Done dumping. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
    return dstPt;
}And here's the sample output:
======= A _projected dstPt has NaNs. Dumping...vvvvvvvvvvvvvvvvvvvvvvvvvvvv
======= dstPt.x is NaN
======= dstPt.y is NaN
======= my_stdLat1 = 25.0
======= my_n = 0.42261826174069944
======= my_originLonRad = -1.6580627893946132
======= my_F = 2.5946660025799146
======= my_earthRad = 6371.2
======= lonRad = -2.7564670759053924
======= latRad = 0.3730758324037379
======= theta = -0.4642057102537187
======= Math.tan(Math.PI/4.0 + latRad/2.0) = 1.4652768116539785
======= Math.pow(Math.tan(Math.PI/4.0 + latRad/2.0), my_n) = 1.175224090766834
======= rho = NaN
======= rhoYo = 14066.369269924155
======= Math.sin(theta) = -0.44771270676160557
======= dstPt.x = NaN
======= Math.cos(theta) = 0.8941774612481554
======= my_rhoZero = 13663.082491950498
======= (rhoYo * Math.cos(theta)) = NaN
======= my_rhoZero - (rhoYo * Math.cos(theta)) = NaN
======= dstPt.y = NaN
======= A _projected dstPt had NaNs. Done dumping. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

HI JSchell (and others?) -
I have created a simple example attached below, that when run repeatedly, does indeed generate spurious NaNs. I have made it as simple as possible. In the code, I use my own lon/lat binary data file, though I am sure any will do. Let me know if anyone wants that file.
So the deal is that (with my data at least) this program should never generate NaN results. And if one runs it 25432 (eg: random #) times, it wont, but then the 25433th time, it will create NaNs, etc. ie: inconsistent NaN math results.
As I said before, I have run this on old and new Dell laptops under Windows XP, using java 1.5_02, 1.5_04 and 1.5_08. The latest run was on a brand new Dell with a Intel Core Duo T2600 processor, running XP. If this is a result of the Pentium bug, one would think that would be fixed by now. I have NOT yet tested on AMD, though I will do that this afternoon.
Remember, this ONLY happens with Windows Laptops.
Any ideas anyone? Thanks in advance ;-)
Here's the code that produces spurious NaNs:
import java.awt.geom.Point2D;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
public class FloatingPointTest2 implements Runnable
  private static final int NUM_ITERATIONS = 100000;
  private double _degToRad = Math.PI / 180.0;
  private double _originLon = -95.0;
  private double _originLat = 25.0;
  private double _originLonRad = _originLon * _degToRad;
  private double _originLatRad = _originLat * _degToRad;;
  private double _stdLat1 = 25.0;
  private double _stdLat1Rad = _stdLat1 * _degToRad;
  private double _earthRad = 6371.2;
  private double _n = _n = Math.sin( _stdLat1Rad );
  private double _F = Math.cos( _stdLat1Rad ) * Math.pow( Math.tan( Math.PI / 4.0 + _stdLat1Rad / 2.0 ), _n ) / _n;
  private double _rhoZero = _earthRad * _F / Math.pow( Math.tan( Math.PI / 4.0 + _originLatRad / 2.0 ), _n );
  private Point2D.Double _project( Point2D.Double srcPt, Point2D.Double dstPt )
    if( Double.isNaN( srcPt.x ) || Double.isNaN( srcPt.y ) )
      System.out.println( "FloatingPointTest2: received a NaN srcPt.  Skipping." );
      return new Point2D.Double( Double.NaN, Double.NaN );
    //  Inputs are lon, lat degrees.
    final double lonRad = srcPt.x * _degToRad;
    final double latRad = srcPt.y * _degToRad;
    final double theta = _n * ( lonRad - _originLonRad );
    double rho = _earthRad * _F / Math.pow( Math.tan( Math.PI / 4.0 + latRad / 2.0 ), _n );
    dstPt.x = rho * Math.sin( theta );
    dstPt.y = _rhoZero - ( rho * Math.cos( theta ) );
    return dstPt;
  public void doTest()
    DataInputStream instream = null;
    int thisRunNaNCount = 0;
    Point2D.Double tempPt = new Point2D.Double();
    Point2D.Double dstPt = new Point2D.Double();
    try
      instream = new DataInputStream( new FileInputStream( System.getProperty(
        "user.home" ) + File.separatorChar + "lonLatBinaryData.bin" ) );
      try
        while( true )
          double lon = instream.readDouble();
          double lat = instream.readDouble();
          if( Double.isNaN( lon ) || Double.isNaN( lat ) )
            continue;
          tempPt.x = lon;
          tempPt.y = lat;
          dstPt = _project( tempPt, dstPt );
          if( Double.isNaN( dstPt.x ) || Double.isNaN( dstPt.y ) )
            thisRunNaNCount++;
      catch( EOFException e )
//        System.out.println( "End of file" );
      if( thisRunNaNCount > 0 )
        System.out.println( "thisRunNaNCount = " + thisRunNaNCount );
      instream.close();
    catch( Exception e )
      e.printStackTrace();
      System.exit( 1 );
  public void run()
    doTest();
  public static void main( String args[] )
    System.out.println( "Executing FloatingPointTest2." );
    for( int i = 0; i < NUM_ITERATIONS; i++ )
      FloatingPointTest2 test = new FloatingPointTest2();
      test.doTest();
}

Similar Messages

  • So... if there's no floating point math, how do all those calculators work?

    I see there's no floating point math support in J2ME.
    So how do those calculators work? There's lots of 'em for download.
    I have an app that's almost done; all I need to do is finish the calculations, but I need to read Strings from TextArea's and compute values based on them.
    What am I missing?

    You can use third party libraries
    (MathFP http://www.jscience.net)
    or
    implement them with classical algorithms.
    Carlos Sanchez
    [Intesys]

  • Install one copy on Mac Desktop and another on Windows laptop?

    The Lightroom 3 license is multi-platform (i.e. Windows & Mac) and permits the installation both a desktop and a laptop.
    Can I install one copy on my iMac desktop and the other on my Windows laptop?
    I've hit the problem already that this is not on CS4 (much to my annoyance) but it appears Lightroom may be different - can anyone confirm?
    Thanks
    Scott

    Yes you are correct the license is multi platform and can be on two computers. Only one in use at any point in time.
    The catalogs created on one is compatable on the other platform.
    There is no activate / deactivate function like Photoshop.

  • Which format for external hard drive to use with time machine backup and connect to windows laptop?

    Hi!  I have an external hard drive on which I have transferred my iTunes library (just mine - not others).  I have also a folder containing just films (some but not all of which are in iTunes too)  Everything has been working just fine until yesterday I noticed that Time Machine was not including the external hard drive in back up as it wasn't formatted.  So I have moved everything back to the Mac hard drive and and ready to format the external hard drive - but understand that if I do the contents cannot be opened from a windows laptop ...  is that right?  Is there are format I could choose that would allow Time Machine to back up and allow Windows to open as well (the idea being that I take the external hard drive with me on holidays etc!!)  Many thanks for any advice. 

    ok - I understand.  I have managed so far to format the drive (on windows) to exFAT file system - which apparently works with both Windows and Mac - but you're  correct - it will not be included in time machine backup as I have checked the back up files and Samsung is not showing.  Reckon I'm onto having to buy yet another portable hard drive just for windows - or of course buy myself a new mac book!!  Can I ask you please - does the drive have to be clear of everything before I change the format - or can I change it with the files still in there?  Also, got any quick ways duplicating the files onto another External Hard drive (for use on the windows laptop)  At the moment the files are taking 2/3 hours + to copy over!  Thank you! 

  • Need to Share Keyboard, Video, and Mouse Between Windows Laptop and Mac

    I need a Mac that shares a keyboard, video, and mouse with my company issued Windows laptop. Usually this just requires a KVM switch but the current iMac has no video input. Subsequently, I can't feed video from the Windows laptop to the iMac's screen.
    _Are these the only solutions for my situation?_
    1) Buy a Mac Mini and share a separate LCD (Too underpowered for me)
    2) Buy a Mac Workstation and share a separate LCD (Too overpowered and pricey for me)
    3) Buy a MacBook Pro (Might work but I am wasting $ since I don't need portability)
    4) Wait for new iMac that MAY have an input (Like it should already!) Anybody have any info on this!?
    Many Thanks!

    I think what you are looking for is an open souce program called Synergy that works on both Mac and PC to give you a software KV(not M).
    You set one machine up as the server, and the other as a client. The server will then control the control the keyboard and mouse of the client machine.
    The alternate is Microsofts virtual screen sharing that allows you to see and control your windows machine in a window on your Mac. All of these require some set up, so you might want to check with your works IT dept.

  • Floating Point Math

    Hi,
    I have following code:
    float totalSpent;
    int intBudget;
    float moneyLeft;
    totalSpent += Amount;
    moneyLeft = intBudget - totalSpent;
    And this is how it looks in debugger: http://www.braginski.com/math.tiff
    Why would moneyLeft calculated by the code above is .02 different compared to the expression calculated by the debugger?
    Expression windows is correct, yet code above produces wrong by .02 result. It only happens for number very large numbers (yet way below int limit)
    thanks

    Thank you all for help!
    Could someone please point me out why first variable printed incorrect, while second is correct:
    NSDecimalNumber *intBalance;
    NSDecimalNumber *Amount;
    NSDecimalNumber *leftAmount;
    NSNumberFormatter *currencyStyle;
    NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain
    scale:2 raiseOnExactness:NO raiseOnOverflow:NO
    raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    currencyStyle = [[NSNumberFormatter alloc] init];
    [currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];
    intBalance = [NSDecimalNumber decimalNumberWithString:@"999999"];
    Amount = [NSDecimalNumber decimalNumberWithString:@"99999.59"];
    leftAmount = [intBalance decimalNumberBySubtracting: Amount withBehavior: handler];
    NSLog(@"Number is: %.2f, %@", [leftAmount floatValue], [currencyStyle stringFromNumber:leftAmount]);
    Number is: 899999.44, $899,999.41
    Message was edited by: leonbrag

  • Stumped on basic problem with floating point math

    I can't figure this out!  It should be sooooo simple. 
    Here is the challenge: 
    I have an incoming time array.  For example:     0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    I want to scale this array by a constant (e.g. multiply by 0.1).  So the resulting array should be:     0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9
    Then I want to calculate the difference between each and every subsequent element in the array.  In this example, the difference should 0.1 between every element.  But my comparison fails. 
    See the example below.  As far as I can see, the resulting boolean should always be TRUE.  But its not.
    But if I remove the scaling operation, then it works ok!
    Please help!
    Solved!
    Go to Solution.

    It has been awhile since smercurio has had a contribution to his retirement fund, but once again you have "discovered" that there is no exact binary representation for 0.1.
    I'd use one of the current "almost equals" comparisons described here:
    http://forums.ni.com/t5/LabVIEW/Darin-s-Weakly-Nugget-2-8-11/m-p/1444262
    And vote for this if you haven't already:
    http://forums.ni.com/t5/LabVIEW-Idea-Exchange/quot-Almost-Equal-quot-functions-for-Float-comparisons...

  • Older ipod and a new windows laptop...why can't I use my ipod on it?

    I have an old ipod, version 3.1.1 with the clickwheel and have always downloaded song on my mac. i just purchased a new pc laptop and tried to hook up my ipod but it said it won't work on a pc. is it because its so old?

    From the article I linked:
    "Using an iPod formatted for Macintosh on a Windows computer is not supported. Using an iPod formatted for Windows on a Macintosh computer is not supported. To determine your iPod's hard disk format see "iPod: How to determine iPod's hard disk format." To change the iPod format you will need to restore iPod using iTunes. For more information on the how to restore iPod with iTunes"

  • Passing floating point values, but being recognised as doubles and int's?

    Hi,
    I was wondering how to differentiate between floating point values and ints etc
    I am passing values into a public class with the parameters (float... values)
    Public Example (float... values){}new Example (1, 1e2, 3.0, 4.754);
    It accepts it if I just use 1,2,3,4 as the values being passed in, but doesn't like it if I use actual float values.
    How come?
    Cheers

    chokpa wrote:
    Public Example (float... values){}
    new Example (1, 1e2, 3.0, 4.754);It accepts it if I just use 1,2,3,4 as the values being passed in, but doesn't like it if I use actual float values.Those are double literals, try
    new Example (1f, 1e2f, 3.0f, 4.754f);

  • Floating Point Arithmatic Error

    Hi,
    I know actionscript represents numbers and double precision
    floating point values. I'm having a problem where double arithmatic
    in actionscript doesn't match the results of the same double
    arithmatic in C++ / C#.
    EXAMPLE:
    In C++ / C#:
    double x, y, x1, y1;
    x = 209.4;
    y = 148.8;
    x1 = 203.0;
    y1 = 145.0;
    double ddx = x - x1;
    double ddy = y - y1;
    RESULT
    ddx: 6.4000000000000057
    ddy: 3.8000000000000114
    In Flash ActionScipt 2:
    var x, y, x1, y1;
    x = 209.4;
    y = 148.8;
    x1 = 203.0;
    y1 = 145.0;
    var ddx = x - x1;
    var ddy = y - y1;
    RESULT
    ddx: 6.39999999999992
    ddy: 3.80000000000024
    After researching Flash / Actionscript "var" stores numerical
    values as doubles ( 8 bytes ) just like doubles are stored in C++ /
    C# ( 8 bytes ). Why would there be a difference between the results
    of ddx and ddy? Are there different implementations of double
    floating point math? If so, Is there a way I can mimic the Flash /
    Actionscript version in C++ / C#?
    Any help would be great!
    Thanks!

    Hmmm, so you're saying the actual binary representation is
    the same but they're just displayed differently?

  • 128-bit floating point numbers on new AMD quad-core Barcelona?

    There's quite a lot of buzz over at Slashdot about the new AMD quad core chips, announced yesterday:
    http://hardware.slashdot.org/article.pl?sid=07/02/10/0554208
    Much of the excitement is over the "new vector math unit referred to as SSE128", which is integrated into each [?!?] core; Tom Yager, of Infoworld, talks about it here:
    Quad-core Opteron? Nope. Barcelona is the completely redesigned x86, and it’s brilliant
    Now here's my question - does anyone know what the inputs and the outputs of this coprocessor look like? Can it perform arithmetic [or, God forbid, trigonometric] operations [in hardware] on 128-bit quad precision floats? And, if so, will LabVIEW be adding support for it? [Compare here versus here.]
    I found a little bit of marketing-speak blather at AMD about "SSE 128" in this old PDF Powerpoint-ish presentation, from June of 2006:
    http://www.amd.com/us-en/assets/content_type/DownloadableAssets/PhilHesterAMDAnalystDayV2.pdf
    WARNING: PDF DOCUMENT
    Page 13: "Dual 128-bit SSE dataflow, Dual 128-bit loads per cycle"
    Page 14: "128-bit SSE and 128-bit Loads, 128b FADD, 128 bit FMUL, 128b SSE, 128b SSE"
    etc etc etc
    While it's largely just gibberish to me, "FADD" looks like what might be a "floating point adder", and "FMUL" could be a "floating point multiplier", and God forbid that the two "SSE" units might be capable of computing some 128-bit cosines. But I don't know whether that old paper is even applicable to the chip that was released yesterday, and I'm just guessing as to what these things might mean anyway.
    Other than that, though, AMD's main website is strangely quiet about the Barcelona announcement. [Memo to AMD marketing - if you've just released the greatest thing since sliced bread, then you need to publicize the fact that you've just released the greatest thing since sliced bread...]

    I posted a query over at the AMD forums, and here's what I was told.
    I had hoped that e.g. "128b FADD" would be able to do something like the following:
    /* "quad" is a hypothetical 128-bit quad precision  */
    /* floating point number, similar to "long double"  */
    /* in recent versions of C++:                       */
    quad x, y, z;
    x = 1.000000000000000000000000000001;
    y = 1.000000000000000000000000000001;
    /* the hope was that "128b FADD" could perform the  */
    /* following 128-bit addition in hardware:          */
    z = x + y;
    However, the answer I'm getting is that "128b FADD" is just a set of two 64-bit adders running in parallel, which are capable of adding two vectors of 64-bit doubles more or less simultaneously:
    double x[2], y[2], z[2];
    x[0] = 1.000000000000000000000000000001;
    y[0] = 1.000000000000000000000000000001;
    x[1] = 2.000000000000000000000000000222;
    y[1] = 2.000000000000000000000000000222;
    /* Apparently the coordinates of the two "vectors" x & y       */
    /* can be sent to "128b FADD" in parallel, and the following   */
    /* two summations can be computed more or less simultaneously: */
    z[0] = x[0] + y[0];
    z[1] = x[1] + y[1];
    Thus e.g. "128b FADD", working in concert with "128b FMUL", will be able to [more or less] halve the amount of time it takes to compute a dot product of vectors whose coordinates are 64-bit doubles.
    So this "128-bit" circuitry is great if you're doing lots of linear algebra with 64-bit doubles, but it doesn't appear to offer anything in the way of greater precision for people who are interested in precision-sensitive calculations.
    By the way, if you're at all interested in questions of precision sensitivity & round-off error, I'd highly recommend Prof Kahan's page at Cal-Berzerkeley:
    http://www.cs.berkeley.edu/~wkahan/
    PDF DOCUMENT: How JAVA's Floating-Point Hurts Everyone Everywhere
    http://www.cs.berkeley.edu/~wkahan/JAVAhurt.pdf
    PDF DOCUMENT: Matlab's Loss is Nobody's Gain
    http://www.cs.berkeley.edu/~wkahan/MxMulEps.pdf

  • Single Precision Floating Point Numbers to Bytes

    Ok here is some code that i have written w hile back with some help from the support staff. It is designed to take in precision floating point numbers that are stored as 4 bytes and convert then to a decimal value. It works off of a udp input string and then also reformats the string. I have the ability to look at up to 4000 parameters from this one udp string. But now what i want to do is do the opposite of what i have written, and also perhaps get rid of the matlab i used in it as well. What i would like to be able to do is input a decimal value and then have it converted in to the 4 byte groupings that make up this decimal nd then have it inputed back in to a single long string witht hat grouping of bytes in the right order. A better explanation of what was done can be found on this website
    http://www.jefflewis.net/XPlaneUDP_8.html
    as the original code followed the "Single Precision Floating Point Numbers and Bytes" example on that site but what i want to do is "Going from Single Precision Floating Point Numbers to Bytes". The site also explains the udp string that is being represented. Also attached is the original code that i am trying to simply reverse.
    Attachments:
    x-plane_udp_master.vi ‏34 KB

    Perhaps what you are doing is an exercise in the programming of the math conversion of the bytes.
    But if you are just interested in getting the conversion done, why not use the typecast function?
    If the bytes happen to be in the wrong order for wherever you need to send the string, then you can use string functions to rearrange them.
    Message Edited by Ravens Fan on 10-02-2007 08:50 PM
    Attachments:
    Example_BD.png ‏3 KB

  • Designing for floating point error

    Hello,
    I am stuck with floating point errors and I'm not sure what to do. Specifically, to determine if a point is inside of a triangle, or if it is on the exact edge of the triangle. I use three cross products with the edge as one vector and the other vector is from the edge start to the query point.
    The theory says that if the cross product is 0 then the point is directly on the line. If the cross product is <0, then the point is inside the triangle. If >0, then the point is outside the triangle.
    To account for the floating point error I was running into, I changed it from =0 to abs(cross_product)<1e-6.
    The trouble is, I run into cases where the algorithm is wrong and fails because there is a point which is classified as being on the edge of the triangle which isn't.
    I'm not really sure how to handle this.
    Thanks,
    Eric

    So, I changed epsilon from 1e-6 to 1e-10 and it seems to work better (I am using doubles btw). However, that doesn't really solve the problem, it just buries it deeper. I'm interested in how actual commercial applications (such as video games or robots) deal with this issue. Obviously you don't see them giving you an error every time a floating point error messes something up. I think the issue here is that I am using data gathered from physical sensors, meaning the inputs can be arbitrarily close to each other. I am worried though that if I round the inputs, that I will get different data points with the exact same x and y value, and I'm not sure how the geometry algorithms will handle that. Also, I am creating a global navigation mesh of triangles with this data. Floating point errors that are not accounted for correctly lead to triangles inside one another (as opposed to adjacent to each other), which damages the integrity of the entire mesh, as its hard to get your program to fix its own mistake.
    FYI:
    I am running java 1.6.0_20 in Eclipse Helios with Ubuntu 10.04x64
    Here is some code that didn't work using 1e-6 for delta. The test point new Point(-294.18294451166435,-25.496614108304477), is outside the triangle, but because of the delta choice it is seen as on the edge:
    class Point
         double x,y;
    class Edge
         Point start, end;
    class Triangle
         Edge[] edges;
         public Point[] getOrderedPoints() throws Exception{
              Point[] points = new Point[3];
              points[0]=edges[0].getStart();
              points[1]=edges[0].getEnd();
              if (edges[1].getStart().equals(points[0]) || edges[1].getStart().equals(points[1]))
                   points[2]=edges[1].getEnd();
              else if (edges[1].getEnd().equals(points[0]) || edges[1].getEnd().equals(points[1]))
                   points[2]=edges[1].getStart();
              else
                   throw new Exception("MalformedTriangleException\n"+this.print());
              orderNodes(points);
              return points;
            /** Orders node1 node2 and node3 in clockwise order, more specifically
          * node1 is swapped with node2 if doing so will order the nodes clockwise
          * with respect to the other nodes.
          * Does not modify node1, node2, or node3; Modifies only the nodes reference
          * Note: "order" of nodes 1, 2, and 3 is clockwise when the path from point
          * 1 to 2 to 3 back to 1 travels clockwise on the circumcircle of points 1,
          * 2, and 3.
         private void orderNodes(Point[] points){
              //the K component (z axis) of the cross product a x b
              double xProductK = crossProduct(points[0],points[0], points[1], points[2]);
              /*        (3)
               *          +
               *        ^
               *      B
               * (1)+             + (2)
               *       ------A-->
               * Graphical representation of vector A and B. 1, 2, and 3 are not in
               * clockwise order, and the x product of A and B is positive.
              if(xProductK > 0)
                   //the cross product is positive so B is oriented as such with
                   //respect to A and 1, 2, 3 are not clockwise in order.
                   //swapping any 2 points in a triangle changes its "clockwise order"
                   Point temp = points[0];
                   points[0] = points[1];
                   points[1] = temp;
    class TriangleTest
         private double delta = 1e-6;
         public static void main(String[] args)  {
                    Point a = new Point(-294.183483785282, -25.498196740397056);
              Point b = new Point(-294.18345625812026, -25.49859505161433);
              Point c = new Point(-303.88217906116796, -63.04183512930035);
              Edge aa = new Edge (a, b);
              Edge bb = new Edge (c, a);
              Edge cc = new Edge (b, c);
              Triangle aaa = new Triangle(aa, bb, cc);
              Point point = new Point(-294.18294451166435,-25.496614108304477);
              System.out.println(aaa.enclosesPointDetailed(point));
          * Check if a point is inside this triangle
          * @param point The test point
          * @return     1 if the point is inside the triangle, 0 if the point is on a triangle, -1 if the point is not is the triangle
          * @throws MalformedTriangleException
         public int enclosesPointDetailed(LocalPose point, boolean verbose) throws Exception
              Point[] points = getOrderedPoints();          
              int cp1 = crossProduct(points[0], points[0], points[1], point);
              int cp2 = crossProduct(points[1], points[1], points[2], point);
              int cp3 = crossProduct(points[2], points[2], points[0], point);
              if (cp1 < 0 && cp2 <0  && cp3 <0)
                   return 1;
              else if (cp1 <=0 && cp2 <=0  && cp3 <=0)
                   return 0;
              else
                   return -1;
             public static int crossProduct(Point start1, Point start2, Point end1, POint end2){
              double crossProduct = (end1.getX()-start1.getX())*(end2.getY()-start2.getY())-(end1.getY()-start1.getY())*(end2.getX()-start2.getX());
              if (crossProduct>floatingPointDelta){
                   return 1;
              else if (Math.abs(crossProduct)<floatingPointDelta){
                   return 0;
              else{
                   return -1;
    }

  • Floating-point addition unit

    Hello All,
    I am very new to NI and I am looking for an example of floating-point addition unit. Basically, I am looking for some example to perform addition of two floating point number (single or double precision). -- just how to create floating-point addition unit using logical gates model (i.e. AND, OR, XOR, etc)
    Thank you!
    Leo
    Message Edited by smuStudent on 12-05-2005 11:51 PM

    Most (if not all) of us, when we want to do floating point math just put the Add function onto the block diagram. I would suggest you google for floating point gates or something similar. You can create subVIs that make up the basic functions such as a full adder and then link them together as needed.
    Attachments:
    Full Adder.JPG ‏9 KB

  • Floating Point Representations on SPARC (64-bit architecture)

    Hi Reader,
    I got hold of "Numerical Computation Guide -2005" by Sun while looking for Floating Point representations on 64 bit Architectures. It gives me nice illustrations of Single and Double formats and the solution for endianness with
    two 32-bit words. But it doesn't tell me how it is for 64-bit SPARC or 64-bit x86.
    I might be wrong here, but having all integers and pointers of 64-bit length, do we still need to break the floating point numbers and store them in lower / higher order addresses ??
    or is it as simple as having a Double Format consistent in the bit-pattern across all the architectures (Intel, SPARC, IBMpowerPC, AMD) with 1 + 11 + 52 bit pattern.
    I have tried hard to get hold of a documentation that explains a 64-bit architecture representation of a Floating Point Number. Any suggestion should be very helpful.
    Thanks for reading. Hope you have something useful to write back.
    Regards,
    Regmee

    The representation of floating-point numbers is specified by IEEE standard 754. This standard contains the specifications for single-precision (32-bit), and double-precision (64-bit) floating-point numbers (There is also a quad-precision (128-bit) format as well). OpenSPARC T1 supports both single and double precision numbers, and can support quad-precision numbers through emulation (not in hardware). The fact that this is a 64-bit machine does not affect how the numbers are stored in memory.
    The only thing that affects how the numbers are stored in memory is endianness. SPARC architecture is big-endian, while x86 is little-endian. But a double-precision floating-point numer in a SPARC register looks the same as a double-precision floating-point number in an x86 register.
    formalGuy

Maybe you are looking for

  • Camera drivers won't load , unless booting from OS 9

    I am trying to use a Fuji Luma2 digital back on my Hasselblad camera; the software that controls the camera thru the firewire only works by booting from OS 9. When running the application on OS 9 classic the main features (take picture) appear ghoste

  • Is it possible to copy text to the clipboard in Illustrator via Javascript ?

    I am working on a very basic script that exports coordinates from Illustrator. At the moment I am just outputing the values via $.writeln() I was wondering if I could copy the output values to the clipboard. If so, what am I looking for in the API ?

  • Program ID in RFC destination-XI

    Hi can any one tell me for RFC sender channel purpose i'm creating RFC destination in SM59. through sap help and some other weblogs i'm trying create Program iD,but i'm getting some doubts on this: 1. How do i know corrosponding programID for this re

  • Download Photo Albums on iWeb?

    The photos we have on our iWeb are able to be downloaded individually by people. I would like to be able to have the photo albums be downloaded as well, so that people do not have to download each photo individually. Is this possible and, if so, how

  • Priority in ECC Help menu

    HI Team, I am creating support message thro "Help" in ECC system. In "Help" menu i am getting the drop down of priority like 1 , 2, 3 etc. Is this priority coming from Sol Man system? Regards Senthil