日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java怎么表示正无穷大_java中怎样表示一个无穷大? 无穷小?

發(fā)布時(shí)間:2025/3/8 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java怎么表示正无穷大_java中怎样表示一个无穷大? 无穷小? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Java中提供了三個(gè)特殊的浮點(diǎn)數(shù)值:正無窮大、負(fù)無窮大、非數(shù),用于表示溢出和出錯(cuò)。

正無窮大:用一個(gè)正數(shù)除以0將得到一個(gè)正無窮大,通過Double或Float的POSITIVE_INFINITY表示。

負(fù)無窮大:用一個(gè)負(fù)數(shù)除以0將得到一個(gè)負(fù)無窮大,通過Double或Float的NEGATIVE_INFINITY表示。

非數(shù):0.0除以0.0或?qū)σ粋€(gè)負(fù)數(shù)開放將得到一個(gè)非數(shù),通過Double或Float的NaN表示。

可以點(diǎn)開jdk源碼,看看java都是如何表示的:

/**

* The {@code Double} class wraps a value of the primitive type

* {@code double} in an object. An object of type

* {@code Double} contains a single field whose type is

* {@code double}.

*

*

In addition, this class provides several methods for converting a

* {@code double} to a {@code String} and a

* {@code String} to a {@code double}, as well as other

* constants and methods useful when dealing with a

* {@code double}.

*

* @author Lee Boynton

* @author Arthur van Hoff

* @author Joseph D. Darcy

* @since JDK1.0

*/

public final class Double extends Number implements Comparable {

/**

* A constant holding the positive infinity of type

* {@code double}. It is equal to the value returned by

* {@code Double.longBitsToDouble(0x7ff0000000000000L)}.

*/

public static final double POSITIVE_INFINITY = 1.0 / 0.0;

/**

* A constant holding the negative infinity of type

* {@code double}. It is equal to the value returned by

* {@code Double.longBitsToDouble(0xfff0000000000000L)}.

*/

public static final double NEGATIVE_INFINITY = -1.0 / 0.0;

/**

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

* {@code double}. It is equivalent to the value returned by

* {@code Double.longBitsToDouble(0x7ff8000000000000L)}.

*/

public static final double NaN = 0.0d / 0.0;

...

}

(實(shí)際上在字節(jié)碼里正無窮和負(fù)無窮還有NaN都有一個(gè)特定的值用來表示

0x7f800000表示正無窮

0xff800000表示負(fù)無窮

在0x7f800001~0x7fffffff 和?0xff80001~0xffffffff兩個(gè)的范圍內(nèi)的值表示NaN.

)

所有的正無窮大的數(shù)值都是相等的,所有的負(fù)無窮大的數(shù)值都是相等;而NaN不與任何數(shù)值相等,甚至和NaN都不等。

舉例

public class javaLesson5

{

public static void main(String[] args)

{

float af = 5.2325556f;

//下面將看到af的值已經(jīng)發(fā)生改變,顯示結(jié)果為5.2325554.

System.out.println(af);

double a = 0.0;

double c = Double.NEGATIVE_INFINITY;

float d = Float.NEGATIVE_INFINITY;

//將看到float和double的負(fù)無窮大是相等的。顯示結(jié)果為:true。

System.out.println(c == d);

//0.0除以0.0將出現(xiàn)非數(shù)。顯示結(jié)果為:NaN。

System.out.println(a / a);

//兩個(gè)非數(shù)之間是不相等的。顯示結(jié)果為:false。

System.out.println(a == Float.NaN);

//所有正無窮大都是相等的。顯示結(jié)果為:true。

System.out.println(6.0 / 0 == 555.0/0);

//負(fù)數(shù)除以0.0將得到負(fù)無窮大。顯示結(jié)果為:-Infinity

System.out.println(-8 / a);

//下面代碼將拋出除以0的異常。

//System.out.pintln(0 / 0);

}

}

部分內(nèi)容轉(zhuǎn)自(http://blog.csdn.net/ml863606/article/details/50853555)

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的java怎么表示正无穷大_java中怎样表示一个无穷大? 无穷小?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。