List of usage examples for java.lang Double NaN
double NaN
To view the source code for java.lang Double NaN.
Click Source Link
From source file:Main.java
public static void main(String[] args) { System.out.println(Double.NaN); }
From source file:Main.java
public static void main(String[] args) { System.out.printf("%.2e %n", Double.NaN); System.out.printf("%.2f %n", Double.POSITIVE_INFINITY); System.out.printf("%.2g %n", Double.NEGATIVE_INFINITY); System.out.printf("%(f %n", Double.POSITIVE_INFINITY); System.out.printf("%(f %n", Double.NEGATIVE_INFINITY); }
From source file:Calculator.java
public static void main(String[] args) { System.out.println("type something like: 1+3"); Scanner scanner = new Scanner(System.in); double n1 = Double.NaN; double n2 = Double.NaN; String operation = null;//from ww w. ja v a 2s. co m try { n1 = scanner.nextDouble(); operation = scanner.next(); n2 = scanner.nextDouble(); double result = calculate(n1, n2, operation); System.out.printf("%s %s %s = %.2f%n", n1, operation, n2, result); } catch (Exception e) { System.out.println("An invalid expression."); } }
From source file:InfNaN.java
public static void main(String[] argv) { double d = 123; double e = 0; if (d / e == Double.POSITIVE_INFINITY) System.out.println("Check for POSITIVE_INFINITY works"); double s = Math.sqrt(-1); if (s == Double.NaN) System.out.println("Comparison with NaN incorrectly returns true"); if (Double.isNaN(s)) System.out.println("Double.isNaN() correctly returns true"); }
From source file:FloatCmp.java
public static void main(String[] argv) { double da = 3 * .3333333333; double db = 0.99999992857; // Compare two numbers that are expected to be close. if (da == db) { System.out.println("Java considers " + da + "==" + db); // else compare with our own equals method } else if (equals(da, db, 0.0000001)) { System.out.println("True within epsilon " + EPSILON); } else {/*w w w. java 2 s . c o m*/ System.out.println(da + " != " + db); } // Show that comparing two NaNs is not a good idea: double d1 = Double.NaN; double d2 = Double.NaN; if (d1 == d2) System.err.println("Comparing two NaNs incorrectly returns true."); if (!new Double(d1).equals(new Double(d2))) System.err.println("Double(NaN).equal(NaN) incorrectly returns false."); }
From source file:ca.mcgill.networkdynamics.geoinference.evaluation.CrossValidationScorer.java
public static void main(String[] args) throws Exception { if (args.length != 4) { System.out.println("java CVS predictions-dir/ " + "cv-gold-dir/ results.txt error-sample.tsv"); return;//from www.j a va2 s . c om } File predDir = new File(args[0]); File cvDir = new File(args[1]); TDoubleList errors = new TDoubleArrayList(10_000_000); TLongSet locatedUsers = new TLongHashSet(10_000_000); TLongSet allUsers = new TLongHashSet(10_000_000); TLongObjectMap<TDoubleList> userToErrors = new TLongObjectHashMap<TDoubleList>(); TLongDoubleMap tweetIdToError = new TLongDoubleHashMap(10_000_000); TLongObjectMap<double[]> idToPredLoc = new TLongObjectHashMap<double[]>(); int tweetsSeen = 0; int tweetsLocated = 0; BufferedReader cvBr = new BufferedReader(new FileReader(new File(cvDir, "folds.info.tsv"))); for (String foldLine = null; (foldLine = cvBr.readLine()) != null;) { String[] cols = foldLine.split("\t"); String foldName = cols[0]; System.out.printf("Scoring results for fold %s%n", foldName); File foldPredictionsFile = new File(predDir, foldName + ".results.tsv.gz"); File goldLocFile = new File(cvDir, foldName + ".gold-locations.tsv"); if (foldPredictionsFile.exists()) { BufferedReader br = Files.openGz(foldPredictionsFile); for (String line = null; (line = br.readLine()) != null;) { String[] arr = line.split("\t"); long id = Long.parseLong(arr[0]); idToPredLoc.put(id, new double[] { Double.parseDouble(arr[1]), Double.parseDouble(arr[2]) }); } br.close(); } System.out.printf("loaded predictions for %d tweets; " + "scoring predictions%n", idToPredLoc.size()); BufferedReader br = new BufferedReader(new FileReader(goldLocFile)); for (String line = null; (line = br.readLine()) != null;) { String[] arr = line.split("\t"); long id = Long.parseLong(arr[0]); long userId = Long.parseLong(arr[1]); allUsers.add(userId); tweetsSeen++; double[] predLoc = idToPredLoc.get(id); if (predLoc == null) continue; tweetsLocated++; locatedUsers.add(userId); double[] goldLoc = new double[] { Double.parseDouble(arr[2]), Double.parseDouble(arr[3]) }; double dist = Geometry.getDistance(predLoc, goldLoc); errors.add(dist); tweetIdToError.put(id, dist); TDoubleList userErrors = userToErrors.get(userId); if (userErrors == null) { userErrors = new TDoubleArrayList(); userToErrors.put(userId, userErrors); } userErrors.add(dist); } br.close(); } errors.sort(); System.out.println("Num errors to score: " + errors.size()); double auc = 0; double userCoverage = 0; double tweetCoverage = tweetsLocated / (double) tweetsSeen; double medianMaxUserError = Double.NaN; double medianMedianUserError = Double.NaN; if (errors.size() > 0) { auc = computeAuc(errors); userCoverage = locatedUsers.size() / ((double) allUsers.size()); TDoubleList maxUserErrors = new TDoubleArrayList(locatedUsers.size()); TDoubleList medianUserErrors = new TDoubleArrayList(locatedUsers.size()); for (TDoubleList userErrors : userToErrors.valueCollection()) { userErrors.sort(); maxUserErrors.add(userErrors.get(userErrors.size() - 1)); medianUserErrors.add(userErrors.get(userErrors.size() / 2)); } maxUserErrors.sort(); medianMaxUserError = maxUserErrors.get(maxUserErrors.size() / 2); medianUserErrors.sort(); medianMedianUserError = medianUserErrors.get(medianUserErrors.size() / 2); // Compute CDF int[] errorsPerKm = new int[MAX_KM]; for (int i = 0; i < errors.size(); ++i) { int error = (int) (Math.round(errors.get(i))); errorsPerKm[error]++; } // The accumulated sum of errors per km int[] errorsBelowEachKm = new int[errorsPerKm.length]; for (int i = 0; i < errorsBelowEachKm.length; ++i) { errorsBelowEachKm[i] = errorsPerKm[i]; if (i > 0) errorsBelowEachKm[i] += errorsBelowEachKm[i - 1]; } final double[] cdf = new double[errorsBelowEachKm.length]; double dSize = errors.size(); // to avoid casting all the time for (int i = 0; i < cdf.length; ++i) cdf[i] = errorsBelowEachKm[i] / dSize; } PrintWriter pw = new PrintWriter(new File(args[2])); pw.println("AUC\t" + auc); pw.println("user coverage\t" + userCoverage); pw.println("tweet coverage\t" + tweetCoverage); pw.println("median-max error\t" + medianMaxUserError); pw.close(); // Choose a random sampling of 10K tweets to pass on to the authors // here. PrintWriter errorsPw = new PrintWriter(args[3]); TLongList idsWithErrors = new TLongArrayList(tweetIdToError.keySet()); idsWithErrors.shuffle(new Random()); // Choose the first 10K for (int i = 0, chosen = 0; i < idsWithErrors.size() && chosen < 10_000; ++i) { long id = idsWithErrors.get(i); double[] prediction = idToPredLoc.get(id); double error = tweetIdToError.get(id); errorsPw.println(id + "\t" + error + "\t" + prediction[0] + "\t" + prediction[1]); ++chosen; } errorsPw.close(); }
From source file:Main.java
public static double getDouble(Node aNode, String attr) { return getDouble(aNode, attr, Double.NaN); }
From source file:Main.java
public static Double quotient(Double d1, Double d2) { if (d1 == null) return Double.valueOf(0); if (d2 == null) return Double.NaN; return d1 / d2; }
From source file:Main.java
/** * @param nums Any descendant of Number (Integer, Short, Double, Float, etc) * @return The mean of the number, or Double.NaN if the list is empty. *//*w ww .j a va 2 s. co m*/ public static double mean(Collection<? extends Number> nums) { if (nums == null || nums.isEmpty()) { return Double.NaN; } double sum = 0.0; for (Number n : nums) { sum += n.doubleValue(); } return sum / nums.size(); }
From source file:Main.java
/** * DOC bZhou Comment method "computePercent". * /* w ww .j a v a 2 s . c o m*/ * @param userCount * @param count * @return */ private static String computePercent(double userCount, double count) { double result = userCount / count; return result != Double.NaN ? String.valueOf(result) : null; }