Example usage for java.lang Float NaN

List of usage examples for java.lang Float NaN

Introduction

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

Prototype

float NaN

To view the source code for java.lang Float NaN.

Click Source Link

Document

A constant holding a Not-a-Number (NaN) value of type float .

Usage

From source file:juicebox.data.MatrixZoomData.java

/**
 * Dump observed matrix to text/* w ww  . ja v a 2  s .  co  m*/
 *
 * @param printWriter Text output stream
 * @param nv1         Normalization vector for X axis
 * @param nv2         Normalization vector for Y axis
 * @throws IOException If fail to write
 */
public void dump(PrintWriter printWriter, double[] nv1, double[] nv2) throws IOException {
    // Get the block index keys, and sort
    List<Integer> blockNumbers = reader.getBlockNumbers(this);
    if (blockNumbers != null) {
        Collections.sort(blockNumbers);

        for (int blockNumber : blockNumbers) {
            Block b = reader.readBlock(blockNumber, this);
            if (b != null) {
                for (ContactRecord rec : b.getContactRecords()) {
                    float counts = rec.getCounts();
                    int x = rec.getBinX();
                    int y = rec.getBinY();
                    if (nv1 != null && nv2 != null) {
                        if (nv1[x] != 0 && nv2[y] != 0 && !Double.isNaN(nv1[x]) && !Double.isNaN(nv2[y])) {
                            counts = (float) (counts / (nv1[x] * nv2[y]));
                        } else {
                            counts = Float.NaN;
                        }
                    }
                    printWriter.println(x * zoom.getBinSize() + "\t" + y * zoom.getBinSize() + "\t" + counts);
                }
            }
        }
    }
    printWriter.close();
}

From source file:org.apache.drill.exec.fn.impl.TestMathFunctionsWithNanInf.java

@Test
public void testCastfloat4Function() throws Exception {
    String table_name = "nan_test.json";
    String json = "{\"nan_col\":NaN, \"inf_col\":Infinity}";
    String query = String.format(
            "select castfloat4(nan_col) as nan_col, castfloat4(inf_col) as inf_col from dfs.`%s`", table_name);
    String[] columns = { "nan_col", "inf_col" };
    Object[] values = { Float.NaN, Float.POSITIVE_INFINITY };
    evalTest(table_name, json, query, columns, values);
}

From source file:jp.furplag.util.commons.NumberUtilsTest.java

/**
 * {@link jp.furplag.util.commons.NumberUtils#compareTo(java.lang.Number, java.lang.Number)}.
 *///from w  w  w . j a v a2 s  .com
@SuppressWarnings("unchecked")
@Test
public void testCompareToNumberNumber() {
    assertEquals("null", (Object) 0, compareTo(null, null));
    assertEquals("null", (Object) 1, compareTo(1, null));
    assertEquals("null", (Object) 1, compareTo(-1, null));
    assertEquals("null", (Object) 1, compareTo(Float.NEGATIVE_INFINITY, null));
    assertEquals("null", (Object) (-1), compareTo(null, 1));
    assertEquals("null", (Object) (-1), compareTo(null, -1));
    assertEquals("null", (Object) (-1), compareTo(null, Double.NEGATIVE_INFINITY));
    assertEquals("Infinity", (Object) 0, compareTo(Float.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY));
    assertEquals("Infinity", (Object) 0, compareTo(Double.POSITIVE_INFINITY, Float.POSITIVE_INFINITY));
    assertEquals("NaN", (Object) 1, compareTo(Float.NaN, null));
    assertEquals("NaN", (Object) 1, compareTo(Float.NaN, 1));
    assertEquals("NaN", (Object) 1, compareTo(Float.NaN, -1));
    assertEquals("NaN", (Object) 1, compareTo(Float.NaN, Double.POSITIVE_INFINITY));
    assertEquals("NaN", (Object) 0, compareTo(Float.NaN, Double.NaN));
    assertEquals("NaN", (Object) (-1), compareTo(null, Double.NaN));
    assertEquals("NaN", (Object) (-1), compareTo(1, Double.NaN));
    assertEquals("NaN", (Object) (-1), compareTo(-1, Double.NaN));
    assertEquals("NaN", (Object) (-1), compareTo(Float.NEGATIVE_INFINITY, Double.NaN));
    assertEquals("NaN", (Object) 0, compareTo(Double.NaN, Float.NaN));
    for (Class<?> type : NUMBERS) {
        Class<? extends Number> wrapper = (Class<? extends Number>) ClassUtils.primitiveToWrapper(type);
        try {
            Number n = null;
            if (ClassUtils.isPrimitiveWrapper(type)) {
                n = (Number) wrapper.getField("MAX_VALUE").get(null);
            } else {
                n = INFINITY_DOUBLE.pow(2);
                if (BigInteger.class.equals(type))
                    n = ((BigDecimal) n).toBigInteger();
            }
            assertEquals("equals: " + type.getSimpleName(), 0, compareTo(n, new BigDecimal(n.toString())));
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace()));
        }
    }
}

From source file:com.github.jonmarsh.waveform_processing_for_imagej.WaveformUtils.java

/**
 * Computes the median value of an array. If the array length is even, the
 * value returned is equal to the average of the middle two values of the
 * sorted array. The input array is left unchanged. {@code null} input
 * returns {@code Double.NaN}.//from w  ww .  j  a  v a 2s .  co m
 *
 * @param a   input array
 * @return median value in the specified range of input array; if array is
 *         zero length, method returns {@code NaN}
 */
public static final float median(float[] a) {
    if (a != null) {
        return median(a, 0, a.length);
    } else {
        return Float.NaN;
    }
}

From source file:juicebox.data.MatrixZoomData.java

/**
 * Dump observed matrix to binary./* w  ww  . j av a2  s .  co  m*/
 *
 * @param les Binary output stream
 * @param nv1 Normalization vector for X axis
 * @param nv2 Normalization vector for Y axis
 * @throws IOException If fail to write
 */
public void dump(LittleEndianOutputStream les, double[] nv1, double[] nv2) throws IOException {

    // Get the block index keys, and sort
    List<Integer> blockNumbers = reader.getBlockNumbers(this);
    if (blockNumbers != null) {
        Collections.sort(blockNumbers);

        for (int blockNumber : blockNumbers) {
            Block b = reader.readBlock(blockNumber, this);
            if (b != null) {
                for (ContactRecord rec : b.getContactRecords()) {
                    float counts = rec.getCounts();
                    int x = rec.getBinX();
                    int y = rec.getBinY();
                    if (nv1 != null && nv2 != null) {
                        if (nv1[x] != 0 && nv2[y] != 0 && !Double.isNaN(nv1[x]) && !Double.isNaN(nv2[y])) {
                            counts = (float) (counts / (nv1[x] * nv2[y]));
                        } else {
                            counts = Float.NaN;
                        }
                    }
                    les.writeInt(x);
                    les.writeInt(y);
                    les.writeFloat(counts);
                }

            }
        }
    }
}

From source file:dk.dma.ais.packet.AisPacketFiltersStateful.java

/**
 * Use target tracker to extract speed over ground based on mmsi lookup.
 * //from   w w  w.ja v a 2 s  . c  o m
 * @param mmsi
 * @return
 */
private float getSog(int mmsi) {
    float sog = Float.NaN;
    TargetInfo target = targetTracker.get(mmsi);
    if (target != null) {
        AisPacket positionPacket = target.getPositionPacket();
        if (positionPacket != null && positionPacket.tryGetAisMessage() instanceof AisPositionMessage) {
            sog = (float) (((AisPositionMessage) positionPacket.tryGetAisMessage()).getSog() / 10.0);
        }
    }
    return sog;
}

From source file:weave.servlets.GenericServlet.java

/**
 * Tries to convert value to the given type
 * @param value/*  ww  w  .  jav a 2s .co m*/
 * @param type
 * @return value which may have been cast as the new type
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object cast(Object value, Class<?> type) {
    if (value == null) {
        if (type == double.class || type == Double.class)
            value = Double.NaN;
        else if (type == float.class || type == Float.class)
            value = Float.NaN;
    } else if (value instanceof String) {
        try {
            if (type == int.class || type == Integer.class) {
                value = Integer.parseInt((String) value);
            } else if (type == float.class || type == Float.class) {
                value = Float.parseFloat((String) value);
            } else if (type == double.class || type == Double.class) {
                value = Double.parseDouble((String) value);
            } else if (type == boolean.class || type == Boolean.class) {
                //value = Boolean.parseBoolean((String)value);
                value = ((String) (value)).equalsIgnoreCase("true");
            } else if (type.isArray() || type == List.class) {
                String[][] table = CSVParser.defaultParser.parseCSV((String) value, true);
                if (table.length == 0)
                    value = new String[0]; // empty
                else
                    value = table[0]; // first row

                if (type == List.class)
                    value = Arrays.asList((String[]) value);
                else
                    value = cast(value, type);
            } else if (type == InputStream.class) {
                try {
                    String temp = (String) value;
                    value = (InputStream) new ByteArrayInputStream(temp.getBytes("UTF-8"));
                } catch (Exception e) {
                    return null;
                }
            }
        } catch (NumberFormatException e) {
            // if number parsing fails, leave the original value untouched
        }
    } else if (value instanceof Boolean && type == boolean.class) {
        value = (boolean) (Boolean) value;
    } else if (value.getClass() == ArrayList.class) {
        value = cast(((ArrayList) value).toArray(), type);
    } else if (value.getClass().isArray()) {
        Object[] valueArray = (Object[]) value;
        if (type == List.class) {
            value = ListUtils.copyArrayToList(valueArray, new Vector());
        } else if (type == Object[][].class) {
            Object[][] valueMatrix = new Object[valueArray.length][];
            for (int i = 0; i < valueArray.length; i++) {
                valueMatrix[i] = (Object[]) valueArray[i];
            }
            value = valueMatrix;
        } else if (type == String[][].class) {
            String[][] valueMatrix = new String[valueArray.length][];
            for (int i = 0; i < valueArray.length; i++) {
                // cast Objects to Strings
                Object[] objectArray = (Object[]) valueArray[i];
                valueMatrix[i] = ListUtils.copyStringArray(objectArray, new String[objectArray.length]);
            }
            value = valueMatrix;
        } else if (type == String[].class) {
            value = ListUtils.copyStringArray(valueArray, new String[valueArray.length]);
        } else if (type == double[][].class) {
            double[][] valueMatrix = new double[valueArray.length][];
            for (int i = 0; i < valueArray.length; i++) {
                // cast Objects to doubles
                Object[] objectArray = (Object[]) valueArray[i];
                valueMatrix[i] = ListUtils.copyDoubleArray(objectArray, new double[objectArray.length]);
            }
            value = valueMatrix;
        } else if (type == double[].class) {
            value = ListUtils.copyDoubleArray(valueArray, new double[valueArray.length]);
        } else if (type == int[][].class) {
            int[][] valueMatrix = new int[valueArray.length][];
            for (int i = 0; i < valueArray.length; i++) {
                // cast Objects to doubles
                Object[] objectArray = (Object[]) valueArray[i];
                valueMatrix[i] = ListUtils.copyIntegerArray(objectArray, new int[objectArray.length]);
            }
            value = valueMatrix;
        } else if (type == int[].class) {
            value = ListUtils.copyIntegerArray(valueArray, new int[valueArray.length]);
        }
    } else if (value instanceof Number) {
        if (type == int.class || type == Integer.class) {
            value = ((Number) value).intValue();
        } else if (type == double.class || type == Double.class) {
            value = ((Number) value).doubleValue();
        } else if (type == float.class || type == Float.class) {
            value = ((Number) value).floatValue();
        }
    }
    return value;
}

From source file:org.evosuite.junit.naming.variables.ExplanatoryNamingTestVisitor.java

/**
 * <p>/*from w  ww  .j a v  a2  s . com*/
 * getNumberString
 * </p>
 *
 * @param value
 *            a {@link java.lang.Object} object.
 * @return a {@link java.lang.String} object.
 */
public static String getStringForPrimitiveValue(Object value) {
    assert (value != null);
    if (value.getClass().equals(char.class) || value.getClass().equals(Character.class)) {
        if (CharUtils.isAsciiNumeric((Character) value))
            return "Numeric";
        else if (CharUtils.isAsciiAlpha((Character) value))
            return "Alpha";
        else
            return "NotAlphanumeric";
    } else if (value.getClass().equals(String.class)) {
        return ((String) value).isEmpty() ? "EmptyString" : "NonEmptyString";
    } else if (value.getClass().equals(float.class) || value.getClass().equals(Float.class)) {
        if (value.toString().equals("" + Float.NaN))
            return "NaN";
        else if (value.toString().equals("" + Float.NEGATIVE_INFINITY))
            return "NegativeInf";
        else if (value.toString().equals("" + Float.POSITIVE_INFINITY))
            return "PositiveInf";
        else
            return (((Float) value) < 0F) ? "Negative" : (((Float) value) == 0F) ? "Zero" : "Positive";
    } else if (value.getClass().equals(double.class) || value.getClass().equals(Double.class)) {
        if (value.toString().equals("" + Double.NaN))
            return "NaN";
        else if (value.toString().equals("" + Double.NEGATIVE_INFINITY))
            return "NegativeInf";
        else if (value.toString().equals("" + Double.POSITIVE_INFINITY))
            return "PositiveInf";
        else
            return (((Double) value) < 0.0) ? "Negative" : (((Double) value) == 0.0) ? "Zero" : "Positive";
    } else if (value.getClass().equals(long.class) || value.getClass().equals(Long.class)) {
        return (((Long) value) < 0) ? "Negative" : (((Long) value) == 0) ? "Zero" : "Positive";
    } else if (value.getClass().equals(byte.class) || value.getClass().equals(Byte.class)) {
        return (((Byte) value) < 0) ? "Negative" : (((Byte) value) == 0) ? "Zero" : "Positive";
    } else if (value.getClass().equals(short.class) || value.getClass().equals(Short.class)) {
        return (((Short) value) < 0) ? "Negative" : (((Short) value) == 0) ? "Zero" : "Positive";
    } else if (value.getClass().equals(int.class) || value.getClass().equals(Integer.class)) {
        int val = ((Integer) value).intValue();
        if (val == Integer.MAX_VALUE)
            return "MaxInt";
        else if (val == Integer.MIN_VALUE)
            return "MinInt";
        else
            return (((Integer) value) < 0) ? "Negative" : (((Integer) value) == 0) ? "Zero" : "Positive";
    } else if (value.getClass().isEnum() || value instanceof Enum) {
        return "EnumValue";
    } else if (value.getClass().equals(Boolean.class)) {
        return capitalize(Boolean.toString((Boolean) value));
    } else {
        // This should not happen
        assert (false);
        return value.toString();
    }
}

From source file:dk.dma.ais.packet.AisPacketFiltersStateful.java

/**
 * Use target tracker to extract course over ground based on mmsi lookup.
 * //from   ww w  . j  a va2s.com
 * @param mmsi
 * @return
 */
private float getCog(int mmsi) {
    float cog = Float.NaN;
    TargetInfo target = targetTracker.get(mmsi);
    if (target != null) {
        AisPacket positionPacket = target.getPositionPacket();
        if (positionPacket != null && positionPacket.tryGetAisMessage() instanceof AisPositionMessage) {
            cog = (float) (((AisPositionMessage) positionPacket.tryGetAisMessage()).getCog() / 10.0);
        }
    }
    return cog;
}