特殊浮点值 Java

// Finite +-0.0
System.out.println("Finite with +-0.0:");
System.out.println("Finite / +-0.0: " + 8.0 / 0.0);
System.out.println("Finite / +-0.0: " + 8.0 / -0.0);
System.out.println("Finite * +-0.0: " + 8.0 * 0.0);
System.out.println("Finite * +-0.0: " + 8.0 * -0.0);
System.out.println("Finite % +-0.0: " + 8.0 % 0.0);
System.out.println("Finite % +-0.0: " + 8.0 % -0.0);
System.out.println("Finite + +-0.0: " + 8.0 + 0.0);
System.out.println("Finite + +-0.0: " + 8.0 + -0.0);
System.out.println("Finite - +-0.0: " + (8.0 - 0.0));
System.out.println("Finite - +-0.0: " + (8.0 - -0.0));
System.out.println();

Finite with +-0.0:
Finite / +-0.0: Infinity
Finite / +-0.0: -Infinity
Finite * +-0.0: 0.0
Finite * +-0.0: -0.0
Finite % +-0.0: NaN
Finite % +-0.0: NaN
Finite + +-0.0: 8.00.0
Finite + +-0.0: 8.0-0.0
Finite - +-0.0: 8.0
Finite - +-0.0: 8.0

// Finite +-∞
System.out.println("Finite with +-∞:");
System.out.println(8.0 / (1.0 / 0));
System.out.println(8.0 / (-1.0 / 0));
System.out.println(8.0 * (1.0 / 0));
System.out.println(8.0 * (-1.0 / 0));
System.out.println(8.0 % (1.0 / 0));
System.out.println(8.0 % (-1.0 / 0));
System.out.println(8.0 + (1.0 / 0));
System.out.println(8.0 + (-1.0 / 0));
System.out.println(8.0 - (1.0 / 0));
System.out.println(8.0 - (-1.0 / 0));
System.out.println();

Finite with +-∞:
0.0
-0.0
Infinity
-Infinity
8.0
8.0
Infinity
-Infinity
-Infinity
Infinity

// +-0.0 +-0.0
System.out.println("+-0.0 with +-0.0:");
System.out.println(0.0 / 0.0);
System.out.println(0.0 / -0.0);
System.out.println(0.0 * 0.0);
System.out.println(0.0 * -0.0);
System.out.println(0.0 % 0.0);
System.out.println(0.0 % -0.0);
System.out.println(0.0 + 0.0);
System.out.println(0.0 + -0.0);
System.out.println(0.0 - 0.0);
System.out.println(0.0 - -0.0);
System.out.println();

System.out.println(-0.0 / 0.0);
System.out.println(-0.0 / -0.0);
System.out.println(-0.0 * 0.0);
System.out.println(-0.0 * -0.0);
System.out.println(-0.0 % 0.0);
System.out.println(-0.0 % -0.0);
System.out.println(-0.0 + 0.0);
System.out.println(-0.0 + -0.0);
System.out.println(-0.0 - 0.0);
System.out.println(-0.0 - -0.0);
System.out.println();

+-0.0 with +-0.0:
NaN
NaN
0.0
-0.0
NaN
NaN
0.0
0.0
0.0
0.0

NaN
NaN
-0.0
0.0
NaN
NaN
0.0
-0.0
-0.0
0.0

// +-∞ Finite
System.out.println("+-∞ with Finite:");
System.out.println((1.0 / 0) / 8.0);
System.out.println((-1.0 / 0) / 8.0);
System.out.println((1.0 / 0) * 8.0);
System.out.println((-1.0 / 0) * 8.0);
System.out.println((1.0 / 0) % 8.0);
System.out.println((-1.0 / 0) % 8.0);
System.out.println((1.0 / 0) + 8.0);
System.out.println((-1.0 / 0) + 8.0);
System.out.println((1.0 / 0) - 8.0);
System.out.println((-1.0 / 0) - 8.0);
System.out.println();

+-∞ with Finite:
Infinity
-Infinity
Infinity
-Infinity
NaN
NaN
Infinity
-Infinity
Infinity
-Infinity

// +-∞ +-∞
System.out.println("+-∞ with +-∞:");
System.out.println((1.0 / 0) / (1.0 / 0));
System.out.println((-1.0 / 0) / (-1.0 / 0));
System.out.println((1.0 / 0) * (1.0 / 0));
System.out.println((-1.0 / 0) * (-1.0 / 0));
System.out.println((1.0 / 0) % (1.0 / 0));
System.out.println((-1.0 / 0) % (-1.0 / 0));
System.out.println((1.0 / 0) + (1.0 / 0));
System.out.println((-1.0 / 0) + (-1.0 / 0));
System.out.println((1.0 / 0) - (1.0 / 0));
System.out.println((-1.0 / 0) - (-1.0 / 0));
System.out.println();

System.out.println((1.0 / 0) / (-1.0 / 0));
System.out.println((-1.0 / 0) / (1.0 / 0));
System.out.println((1.0 / 0) * (-1.0 / 0));
System.out.println((-1.0 / 0) * (1.0 / 0));
System.out.println((1.0 / 0) % (-1.0 / 0));
System.out.println((-1.0 / 0) % (1.0 / 0));
System.out.println((1.0 / 0) + (-1.0 / 0));
System.out.println((-1.0 / 0) + (1.0 / 0));
System.out.println((1.0 / 0) - (-1.0 / 0));
System.out.println((-1.0 / 0) - (1.0 / 0));
System.out.println();

+-∞ with +-∞:
NaN
NaN
Infinity
Infinity
NaN
NaN
Infinity
-Infinity
NaN
NaN

NaN
NaN
-Infinity
-Infinity
NaN
NaN
NaN
NaN
Infinity
-Infinity

// +-0 +-∞
System.out.println("+-0 with +-∞:");
System.out.println(0.0 / (1.0 / 0));
System.out.println(0.0 / (-1.0 / 0));
System.out.println(0.0 * (1.0 / 0));
System.out.println(0.0 * (-1.0 / 0));
System.out.println(0.0 % (1.0 / 0));
System.out.println(0.0 % (-1.0 / 0));
System.out.println(0.0 + (1.0 / 0));
System.out.println(0.0 + (-1.0 / 0));
System.out.println(0.0 - (1.0 / 0));
System.out.println(0.0 - (-1.0 / 0));
System.out.println();

System.out.println(-0.0 / (1.0 / 0));
System.out.println(-0.0 / (-1.0 / 0));
System.out.println(-0.0 * (1.0 / 0));
System.out.println(-0.0 * (-1.0 / 0));
System.out.println(-0.0 % (1.0 / 0));
System.out.println(-0.0 % (-1.0 / 0));
System.out.println(-0.0 + (1.0 / 0));
System.out.println(-0.0 + (-1.0 / 0));
System.out.println(-0.0 - (1.0 / 0));
System.out.println(-0.0 - (-1.0 / 0));
System.out.println();

+-0 with +-∞:
0.0
-0.0
NaN
NaN
0.0
0.0
Infinity
-Infinity
-Infinity
Infinity

-0.0
0.0
NaN
NaN
-0.0
-0.0
Infinity
-Infinity
-Infinity
Infinity

时间: 2024-10-05 04:44:42

特殊浮点值 Java的相关文章

表达式求值 - Java实现

本程序用于计算任意四则运算表达式.如 4 * ( 10 + 2 ) + 1 的结果应该为 49. 算法说明: 1. 首先定义运算符优先级.我们用一个 Map<String, Map<String, String>> 来保存优先级表.这样我们就可以通过下面的方式来计算两个运算符的优先级了: /** * 查表得到op1和op2的优先级 * @param op1 运算符1 * @param op2 运算符2 * @return ">", "<&q

javascript浮点值运算舍入误差

问题 在javascript中整数和浮点数都属于Number数据类型(简单数据类型中的一种),我们经常会发现在打印1.0这样的浮点数的结果是1而非1.0,这是由于保存浮点数的内存空间是保存整数值的两倍,所以ECMAScript会不失时机地将浮点数转换为整数. 上面这种情况虽然让强迫症患者有点不舒服,但是好歹也不是什么大错,接下来这种情况就很吓人了.例如我们在计算0.1加0.2时,它的输出结果不是0.3,而是0.3000000000000004.what the fuck?!第一次遇到这种情况的童

写一段程序,求1+2*2+3*3+n*n的值 Java

public static void main(String[] args) { // 1*1+2*2+3*3+n*n int n = 40; // 1 5 14 30 55 // 1 2 3 4 5 // 方式一 int c = 0; for (int i = 1; i <= n; i++) { int c1 = count(i); c = (c1 + i * i); } System.out.println(c); // 方式二 c = 0; c = n * (n + 1) * (2 * n

计算文件的MD5值(Java &amp; Rust)

Java public class TestFileMD5 { public final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b",

使用随机数计算pi值--java实现

1.先上一张图 2.原理分析 上图展示的是,在二维坐标图中,半径为1的圆的1/4, 随机n个点(x坐标在0~1之间,并且y坐标在0~1之间),落在半径为1的圆内部,当点的样本足够多的时候,形成1/4的饼图. 3.代码实现 /** * */ package com.sunny.www.interview; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Scanner; /** * 使用随机数

题目1096:日期差值 Java/C++

题目描述: 有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天 输入: 有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD 输出: 每组数据输出一行,即日期差值 样例输入: 20110412 20110422 样例输出: 11 Java AC 代码: import java.util.Scanner; public class Main { /** * @param args */ public static void main(String[]

CCF|最小差值|Java

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n=Integer.parseInt(in.nextLine().trim()); int[] a=new int[n]; int[] b=new int[n-1]; for(int i=0;i<n;i++) { a[i]=in.nextInt

写一个程序,乞讨1+2*2+3*3+n*n值 Java

public static void main(String[] args) { // 1*1+2*2+3*3+n*n int n = 40; // 1 5 14 30 55 // 1 2 3 4 5 // 方式一 int c = 0; for (int i = 1; i <= n; i++) { int c1 = count(i); c = (c1 + i * i); } System.out.println(c); // 方式二 c = 0; c = n * (n + 1) * (2 * n

Delphi float浮点值转换整型int 方法

Uses Math; var ff:Extended; begin ff:=5.16; ShowMessage(IntToStr(Trunc(aa))); //截取整数,小数不要 ShowMessage(IntToStr(Floor(aa))); //往小取整 ShowMessage(IntToStr(Ceil(aa))); //往大取整 ShowMessage(IntToStr(Round(aa))); //四舍五入取整 end; 其他 Delphi数学函数 参考:https://www.cn