Example usage for java.lang Float isNaN

List of usage examples for java.lang Float isNaN

Introduction

In this page you can find the example usage for java.lang Float isNaN.

Prototype

public static boolean isNaN(float v) 

Source Link

Document

Returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.

Usage

From source file:Main.java

public static void main(String[] args) {
    float f = (float) Math.sqrt(-10);
    boolean b1 = Float.isNaN(f);
    System.out.println(b1);/*from   w ww . j  a  v  a  2 s.  c o  m*/

    Float fObj = new Float(f);
    boolean b2 = fObj.isNaN();
    System.out.println(b2);
}

From source file:Main.java

public static void main(String[] args) {

    Float f1 = new Float(-1.0 / 0.0);
    Float f2 = new Float(0.0 / 0.0);

    System.out.println(f1 + " = " + Float.isNaN(f1));
    System.out.println(f2 + " = " + Float.isNaN(f2));
}

From source file:Main.java

public static void main(String[] args) {
    Random random = new Random();
    for (int i = 0; i < 1000000000; i++) {
        float f = random.nextFloat() * 2 - 1;
        if (Float.isNaN(f)) {
            System.out.println("NaN!");
        }/* w  ww . j a v a2  s. com*/
    }
}

From source file:airnowgrib2tojson.AirNowGRIB2toJSON.java

/**
 * @param args the command line arguments
 *///from  w w w .  ja  va 2  s .  c o m
public static void main(String[] args) {

    SimpleDateFormat GMT = new SimpleDateFormat("yyMMddHH");
    GMT.setTimeZone(TimeZone.getTimeZone("GMT-2"));

    System.out.println(GMT.format(new Date()));

    FTPClient ftpClient = new FTPClient();
    FileOutputStream fos = null;

    try {
        //Connecting to AirNow FTP server to get the fresh AQI data  
        ftpClient.connect("ftp.airnowapi.org");
        ftpClient.login("pixelshade", "GZDN8uqduwvk");
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        //downloading .grib2 file
        File of = new File("US-" + GMT.format(new Date()) + "_combined.grib2");
        OutputStream outstr = new BufferedOutputStream(new FileOutputStream(of));
        InputStream instr = ftpClient
                .retrieveFileStream("GRIB2/US-" + GMT.format(new Date()) + "_combined.grib2");
        byte[] bytesArray = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = instr.read(bytesArray)) != -1) {
            outstr.write(bytesArray, 0, bytesRead);
        }

        //Close used resources
        ftpClient.completePendingCommand();
        outstr.close();
        instr.close();

        // logout the user 
        ftpClient.logout();

    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            //disconnect from AirNow server
            ftpClient.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    try {
        //Open .grib2 file
        final File AQIfile = new File("US-" + GMT.format(new Date()) + "_combined.grib2");
        final GridDataset gridDS = GridDataset.open(AQIfile.getAbsolutePath());

        //The data type needed - AQI; since it isn't defined in GRIB2 standard,
        //Aerosol type is used instead; look AirNow API documentation for details.
        GridDatatype AQI = gridDS.findGridDatatype("Aerosol_type_msl");

        //Get the coordinate system for selected data type;
        //cut the rectangle to work with - time and height axes aren't present in these files
        //and latitude/longitude go "-1", which means all the data provided.
        GridCoordSystem AQIGCS = AQI.getCoordinateSystem();
        List<CoordinateAxis> AQI_XY = AQIGCS.getCoordinateAxes();
        Array AQIslice = AQI.readDataSlice(0, 0, -1, -1);

        //Variables for iterating through coordinates
        VariableDS var = AQI.getVariable();
        Index index = AQIslice.getIndex();

        //Variables for counting lat/long from the indices provided
        double stepX = (AQI_XY.get(2).getMaxValue() - AQI_XY.get(2).getMinValue()) / index.getShape(1);
        double stepY = (AQI_XY.get(1).getMaxValue() - AQI_XY.get(1).getMinValue()) / index.getShape(0);
        double curX = AQI_XY.get(2).getMinValue();
        double curY = AQI_XY.get(1).getMinValue();

        //Output details
        OutputStream ValLog = new FileOutputStream("USA_AQI.json");
        Writer ValWriter = new OutputStreamWriter(ValLog);

        for (int j = 0; j < index.getShape(0); j++) {
            for (int i = 0; i < index.getShape(1); i++) {
                float val = AQIslice.getFloat(index.set(j, i));

                //Write the AQI value and its coordinates if it's present by i/j indices
                if (!Float.isNaN(val))
                    ValWriter.write("{\r\n\"lat\":" + curX + ",\r\n\"lng\":" + curY + ",\r\n\"AQI\":" + val
                            + ",\r\n},\r\n");

                curX += stepX;
            }
            curY += stepY;
            curX = AQI_XY.get(2).getMinValue();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.openscience.jvxl.Jvxl.java

public static void main(String[] args) {

    boolean blockData = false;
    int fileIndex = Integer.MAX_VALUE;
    String inputFile = null;/*from   ww w  .ja v a  2  s . c  o  m*/
    String mapFile = null;
    String outputFile = null;

    float cutoff = Float.NaN;
    boolean isPositiveOnly = false;

    P4 plane = null;

    boolean bicolor = false;
    boolean reverseColor = false;
    float min = Float.NaN;
    float max = Float.NaN;

    Options options = new Options();
    options.addOption("h", "help", false, "give this help page");

    /*
     *  examples: 
     *  
     *  jvxl ch3cl-density.cub --min=0.0 --max=0.2 --map ch3cl-esp.cub
     *  jvxl ethene-HOMO.cub --bicolor --output ethene.jvxl
     *  jvxl d_orbitals.jvxl --index 2 --phase yz
     *  jvxl d_orbitals.jvxl --map sets
     *  jvxl --plane xy  --min=0.0 --max=0.2 --map data/ch3cl-density.cub 
     */

    // file options
    options.addOption("B", "blockdata", false, "multiple cube data are in blocks, not interspersed");

    options.addOption("P", "progressive", false, "create JVXL+ progressive X low-to-high format");

    OptionBuilder.withLongOpt("file");
    OptionBuilder.withDescription("file containing surface data");
    OptionBuilder.withValueSeparator('=');
    OptionBuilder.hasArg();
    options.addOption(OptionBuilder.create("f"));

    OptionBuilder.withLongOpt("index");
    OptionBuilder.withDescription("index of surface in file (starting with 1)");
    OptionBuilder.withValueSeparator('=');
    OptionBuilder.hasArg();
    options.addOption(OptionBuilder.create("i"));

    OptionBuilder.withLongOpt("plane");
    OptionBuilder.withDescription("plane: x, y, z, xy, xz, yz, z2, x2-y2, or {a,b,c,d}");
    OptionBuilder.withValueSeparator('=');
    OptionBuilder.hasArg();
    options.addOption(OptionBuilder.create("p"));

    OptionBuilder.withLongOpt("map");
    OptionBuilder.withDescription("file containing data to map onto the surface or \"sets\"");
    OptionBuilder.withValueSeparator('=');
    OptionBuilder.hasArg();
    options.addOption(OptionBuilder.create("m"));

    OptionBuilder.withLongOpt("output");
    OptionBuilder.withDescription("JVXL output file");
    OptionBuilder.withValueSeparator('=');
    OptionBuilder.hasArg();
    options.addOption(OptionBuilder.create("o"));

    // surface options

    OptionBuilder.withLongOpt("cutoff");
    OptionBuilder.withDescription("isosurface cutoff value");
    OptionBuilder.withValueSeparator('=');
    OptionBuilder.hasArg();
    options.addOption(OptionBuilder.create("c"));

    // color mapping options

    options.addOption("b", "bicolor", false, "bicolor map (orbital)");
    options.addOption("r", "reversecolor", false, "reverse color");

    OptionBuilder.withLongOpt("colorScheme");
    OptionBuilder.withDescription("VRML color scheme: bw, wb, roygb, bgyor, rwb, bwr, low, high");
    OptionBuilder.withValueSeparator('=');
    OptionBuilder.hasArg();
    options.addOption(OptionBuilder.create("s"));

    OptionBuilder.withLongOpt("phase");
    OptionBuilder.withDescription("color by phase: x, y, z, xy, xz, yz, z2, x2-y2");
    OptionBuilder.withValueSeparator('=');
    OptionBuilder.hasArg();
    options.addOption(OptionBuilder.create("F"));

    OptionBuilder.withLongOpt("min");
    OptionBuilder.withDescription("color absolute minimum value");
    OptionBuilder.withValueSeparator('=');
    OptionBuilder.hasArg();
    options.addOption(OptionBuilder.create("n"));

    OptionBuilder.withLongOpt("max");
    OptionBuilder.withDescription("color absolute maximum value");
    OptionBuilder.withValueSeparator('=');
    OptionBuilder.hasArg();
    options.addOption(OptionBuilder.create("x"));

    CommandLine line = null;
    try {
        CommandLineParser parser = new PosixParser();
        line = parser.parse(options, args);
    } catch (ParseException exception) {
        Logger.error("Unexpected exception: " + exception.toString());
    }

    if (line.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("Jvxl", options);
        return;
    }

    args = line.getArgs();
    if (args.length > 0) {
        inputFile = args[0];
    }

    // files 

    blockData = (line.hasOption("B"));

    if (line.hasOption("i")) {
        fileIndex = PT.parseInt(line.getOptionValue("i"));
    }

    if (line.hasOption("f")) {
        inputFile = line.getOptionValue("f");
    }

    if (line.hasOption("m")) {
        mapFile = line.getOptionValue("m");
    }

    if (line.hasOption("p")) {
        plane = getPlane(line.getOptionValue("p"));
        if (plane == null) {
            Logger.error("invalid plane");
            return;
        }
        Logger.info("using plane " + plane);
        if (mapFile == null)
            mapFile = inputFile;
        if (inputFile == null)
            inputFile = mapFile;
    }

    if (line.hasOption("o")) {
        outputFile = line.getOptionValue("o");
    } else {
        outputFile = inputFile;
        if (outputFile.indexOf(".") < 0)
            outputFile += ".";
        String sIndex = (fileIndex == Integer.MAX_VALUE ? "" : "_" + fileIndex);
        if (sIndex.length() == 0 && outputFile.indexOf(".jvxl") >= 0)
            sIndex += "_new";
        outputFile = outputFile.substring(0, outputFile.lastIndexOf(".")) + sIndex + ".jvxl";
    }

    // Process more command line arguments
    // these are also passed to vwr

    bicolor = (line.hasOption("b"));
    reverseColor = (line.hasOption("r"));

    if (bicolor && mapFile != null) {
        Logger.warn("--map option ignored; incompatible with --bicolor");
        mapFile = null;
    }

    if (line.hasOption("c")) {
        String s = line.getOptionValue("c");
        if (s.indexOf("+") == 0) {
            isPositiveOnly = true;
            s = s.substring(1);
        }
        cutoff = PT.parseFloat(s);
    }

    if (line.hasOption("n")) {
        if (bicolor)
            Logger.warn("--min option ignored; incompatible with --bicolor");
        else
            min = PT.parseFloat(line.getOptionValue("n"));
    }

    if (line.hasOption("x")) {
        if (bicolor)
            Logger.warn("--max option ignored; incompatible with --bicolor");
        else
            max = PT.parseFloat(line.getOptionValue("x"));
    }

    //    if (line.hasOption("P")) {
    //      phase = line.getOptionValue("P");
    //    }

    boolean progressive = line.hasOption("P");

    // compose the surface

    SurfaceGenerator sg = new SurfaceGenerator(null, null, null, null);

    // input file

    sg.version = VERSION;
    if (blockData)
        sg.setProp("blockData", Boolean.TRUE, null);
    if (!Float.isNaN(cutoff))
        sg.setProp(isPositiveOnly ? "cutoffPositive" : "cutoff", Float.valueOf(cutoff), null);
    if (bicolor)
        sg.setProp("sign", null, null);
    if (reverseColor)
        sg.setProp("reverseColor", null, null);
    //if (phase != null)
    //sg.setProp("phase", phase);

    if (progressive)
        sg.setProp("progressive", null, null);

    if (plane != null)
        sg.setProp("plane", plane, null);
    else {
        if (fileIndex != Integer.MAX_VALUE)
            sg.setProp("fileIndex", Integer.valueOf(fileIndex), null);
        Object t = FileReader.getBufferedReaderOrErrorMessageFromName(inputFile);
        if (t instanceof String) {
            Logger.error((String) t);
            return;
        }
        BufferedReader br = (BufferedReader) t;
        sg.setProp("readFile", br, null);
        try {
            br.close();
        } catch (Exception e) {
            //
        }
    }

    sg.setProp("title", line.toString(), null);

    //color scheme is only for VMRL

    //if (colorScheme != null) {
    // ColorEncoder ce = new ColorEncoder(null);
    // ce.setColorScheme(colorScheme, false);
    // sg.setProp("colorScheme", ce);
    // }
    if (!Float.isNaN(min))
        sg.setProp("red", Float.valueOf(min), null);
    if (!Float.isNaN(max))
        sg.setProp("blue", Float.valueOf(max), null);
    if (mapFile != null) {
        Object t = FileReader.getBufferedReaderOrErrorMessageFromName(mapFile);
        if (t instanceof String) {
            Logger.error((String) t);
            return;
        }
        BufferedReader br2 = (BufferedReader) t;
        sg.setProp("mapColor", br2, null);
        try {
            br2.close();
        } catch (Exception e) {
            //
        }
    }

    writeFile(outputFile, (String) sg.getProperty("jvxlFileData", 0));
    Logger.info((String) sg.getProperty("jvxlFileInfo", 0));
    Logger.info("\ncreated " + outputFile);
}

From source file:Main.java

public static float precision(int decimalPlace, float val) {
    if (Float.isNaN(val)) {
        return 0.0f;
    }//from w w  w.j a v a  2s .  c  o m

    String str = String.format(Locale.ENGLISH, "%." + decimalPlace + 'f', val);
    return Float.valueOf(str);

}

From source file:Main.java

/**
 * Returns next bigger float value considering precision of the argument.
 * //from  w  w w .java2  s.co m
 */
public static float nextUpF(float f) {
    if (Float.isNaN(f) || f == Float.POSITIVE_INFINITY) {
        return f;
    } else {
        f += 0.0f;
        return Float.intBitsToFloat(Float.floatToRawIntBits(f) + ((f >= 0.0f) ? +1 : -1));
    }
}

From source file:Main.java

public static void writeFloat(String name, float value, XMLStreamWriter writer) throws XMLStreamException {
    if (!Float.isNaN(value)) {
        writer.writeAttribute(name, Float.toString(value));
    }//  ww w.j  av a  2s  . c om
}

From source file:Main.java

/**
 * For a float value x, this method returns +1.0F if x >= 0 and -1.0F if x <
 * 0. Returns <code>NaN</code> if <code>x</code> is <code>NaN</code>.
 * /*w  w  w.j av  a  2  s.c o m*/
 * @param x the value, a float
 * @return +1.0F or -1.0F, depending on the sign of x
 */
public static float indicator(final float x) {
    if (Float.isNaN(x)) {
        return Float.NaN;
    }
    return (x >= 0.0F) ? 1.0F : -1.0F;
}

From source file:Main.java

/**
 * Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a>
 * for float value <code>x</code>.
 * <p>//from  ww  w .j a v a  2  s . c o m
 * For a float value x, this method returns +1.0F if x > 0, 0.0F if x =
 * 0.0F, and -1.0F if x < 0. Returns <code>NaN</code> if <code>x</code>
 * is <code>NaN</code>.</p>
 * 
 * @param x the value, a float
 * @return +1.0F, 0.0F, or -1.0F, depending on the sign of x
 */
public static float sign(final float x) {
    if (Float.isNaN(x)) {
        return Float.NaN;
    }
    return (x == 0.0F) ? 0.0F : (x > 0.0F) ? 1.0F : -1.0F;
}