pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

NaN 的位模式真的依賴于硬件嗎?

Are the bit patterns of NaNs really hardware-dependent?(NaN 的位模式真的依賴于硬件嗎?)
本文介紹了NaN 的位模式真的依賴于硬件嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在閱讀 Java 語言規范中的浮點 NaN 值(我很無聊).32 位 float 具有這種位格式:

I was reading about floating-point NaN values in the Java Language Specification (I'm boring). A 32-bit float has this bit format:

seee eeee emmm mmmm mmmm mmmm mmmm mmmm

s 是符號位,e 是指數位,m 是尾數位.NaN 值被編碼為全 1 的指數,并且尾數位不全為 0(這將是 +/- 無窮大).這意味著有許多不同的可能 NaN 值(具有不同的 sm 位值).

s is the sign bit, e are the exponent bits, and m are the mantissa bits. A NaN value is encoded as an exponent of all 1s, and the mantissa bits are not all 0 (which would be +/- infinity). This means that there are lots of different possible NaN values (having different s and m bit values).

對此,JLS§4.2.3 說:

IEEE 754 允許其單雙浮點格式中的每一種都有多個不同的 NaN 值.雖然每個硬件架構在生成新的 NaN 時都會返回特定的 NaN 位模式,但程序員也可以創建具有不同位模式的 NaN 來編碼,例如追溯診斷信息.

IEEE 754 allows multiple distinct NaN values for each of its single and double floating-point formats. While each hardware architecture returns a particular bit pattern for NaN when a new NaN is generated, a programmer can also create NaNs with different bit patterns to encode, for example, retrospective diagnostic information.

JLS 中的文本似乎暗示,例如,0.0/0.0 的結果具有與硬件相關的位模式,并且取決于該表達式是否被計算為編譯時間常量,它所依賴的硬件可能是編譯 Java 程序的硬件或運行程序的硬件.如果屬實,這一切似乎非常不穩定.

The text in the JLS seems to imply that the result of, for example, 0.0/0.0, has a hardware-dependent bit pattern, and depending on whether that expression was computed as a compile time constant, the hardware it is dependent on might be the hardware the Java program was compiled on or the hardware the program was run on. This all seems very flaky if true.

我進行了以下測試:

System.out.println(Integer.toHexString(Float.floatToRawIntBits(0.0f/0.0f)));
System.out.println(Integer.toHexString(Float.floatToRawIntBits(Float.NaN)));
System.out.println(Long.toHexString(Double.doubleToRawLongBits(0.0d/0.0d)));
System.out.println(Long.toHexString(Double.doubleToRawLongBits(Double.NaN)));

我機器上的輸出是:

7fc00000
7fc00000
7ff8000000000000
7ff8000000000000

輸出沒有顯示任何超出預期的內容.指數位都是 1.尾數的高位也是 1,這對于 NaN 顯然表示安靜的 NaN"而不是發信號的 NaN"(https://en.wikipedia.org/wiki/NaN#Floating_point).符號位和尾數位的其余部分為 0.輸出還顯示,在我的機器上生成的 NaN 與 Float 和 Double 類中的常量 NaN 沒有區別.

The output shows nothing out of the expected. The exponent bits are all 1. The upper bit of the mantissa is also 1, which for NaNs apparently indicates a "quiet NaN" as opposed to a "signalling NaN" (https://en.wikipedia.org/wiki/NaN#Floating_point). The sign bit and the rest of the mantissa bits are 0. The output also shows that there was no difference between the NaNs generated on my machine and the constant NaNs from the Float and Double classes.

我的問題是,無論編譯器或虛擬機的 CPU 是多少,Java 是否都能保證輸出,還是真的無法預測?JLS 對此很神秘.

My question is, is that output guaranteed in Java, regardless of the CPU of the compiler or VM, or is it all genuinely unpredictable? The JLS is mysterious about this.

如果 0.0/0.0 保證該輸出,是否有任何算術方法可以生成具有其他(可能與硬件相關?)位模式的 NaN?(我知道 intBitsToFloat/longBitsToDouble 可以編碼其他 NaN,但我想知道其他值是否可以從正常算術中產生.)

If that output is guaranteed for 0.0/0.0, are there any arithmetic ways of producing NaNs that do have other (possibly hardware-dependent?) bit patterns? (I know intBitsToFloat/longBitsToDouble can encode other NaNs, but I'd like to know if other values can occur from normal arithmetic.)

后續要點:我注意到 Float.NaN 和 雙倍.NaN 指定它們的確切位模式,但在源 (浮動, Double) 它們由 0.0/0.0 生成.如果該劃分的結果確實取決于編譯器的硬件,那么無論是規范還是實現似乎都存在缺陷.

A followup point: I've noticed that Float.NaN and Double.NaN specify their exact bit pattern, but in the source (Float, Double) they are generated by 0.0/0.0. If the result of that division is really dependent on the hardware of the compiler, it seems like there is a flaw there in either the spec or the implementation.

推薦答案

這就是 §2.3.2 of the JVM 7 spec 不得不說:

雙值集合的元素正是可以表示的值使用 IEEE 754 標準中定義的雙浮點格式,除了只有一個 NaN 值(IEEE 754 指定 253-2 個不同的 NaN 值).

The elements of the double value set are exactly the values that can be represented using the double floating-point format defined in the IEEE 754 standard, except that there is only one NaN value (IEEE 754 specifies 253-2 distinct NaN values).

和 §2.8.1:

Java 虛擬機沒有信號 NaN 值.

The Java Virtual Machine has no signaling NaN value.

所以從技術上講,只有一個 NaN.但是 §4.2.3JLS 還說(在您的報價之后):

So technically there is only one NaN. But §4.2.3 of the JLS also says (right after your quote):

在大多數情況下,Java SE 平臺將給定類型的 NaN 值視為折疊為單個規范值,因此本規范通常將任意 NaN 稱為規范值.

For the most part, the Java SE platform treats NaN values of a given type as though collapsed into a single canonical value, and hence this specification normally refers to an arbitrary NaN as though to a canonical value.

但是,Java SE 平臺 1.3 版引入了使程序員能夠區分 NaN 值的方法:Float.floatToRawIntBits 和 Double.doubleToRawLongBits 方法.感興趣的讀者可以參考 Float 和 Double 類的規范以獲取更多信息.

However, version 1.3 of the Java SE platform introduced methods enabling the programmer to distinguish between NaN values: the Float.floatToRawIntBits and Double.doubleToRawLongBits methods. The interested reader is referred to the specifications for the Float and Double classes for more information.

我認為這正是您和 CandiedOrange 建議的意思:它依賴于底層處理器,但 Java 對待它們都一樣.

Which I take to mean exactly what you and CandiedOrange propose: It is dependent on the underlying processor, but Java treats them all the same.

但它變得更好:顯然,您的 NaN 值完全有可能被靜默轉換為不同的 NaN,如 Double.longBitsToDouble():

But it gets better: Apparently, it is entirely possible that your NaN values are silently converted to different NaNs, as described in Double.longBitsToDouble():

請注意,此方法可能無法返回具有與 long 參數完全相同的位模式的雙 NaN.IEEE 754 區分了兩種 NaN,靜默 NaN 和信令 NaN.這兩種 NaN 之間的差異在 Java 中通常是不可見的.信號 NaN 的算術運算將它們變成安靜的 NaN,具有不同但通常相似的位模式.但是,在某些處理器上,僅復制信號 NaN 也會執行該轉換.特別是,復制一個信令 NaN 以將其返回給調用方法可以執行此轉換.因此 longBitsToDouble 可能無法返回帶有信號 NaN 位模式的雙精度數.因此,對于某些 long 值,doubleToRawLongBits(longBitsToDouble(start)) 可能不等于 start.此外,哪些特定的位模式代表信令 NaN 取決于平臺;盡管所有 NaN 位模式,安靜或信令,都必須在上面確定的 NaN 范圍內.

Note that this method may not be able to return a double NaN with exactly same bit pattern as the long argument. IEEE 754 distinguishes between two kinds of NaNs, quiet NaNs and signaling NaNs. The differences between the two kinds of NaN are generally not visible in Java. Arithmetic operations on signaling NaNs turn them into quiet NaNs with a different, but often similar, bit pattern. However, on some processors merely copying a signaling NaN also performs that conversion. In particular, copying a signaling NaN to return it to the calling method may perform this conversion. So longBitsToDouble may not be able to return a double with a signaling NaN bit pattern. Consequently, for some long values, doubleToRawLongBits(longBitsToDouble(start)) may not equal start. Moreover, which particular bit patterns represent signaling NaNs is platform dependent; although all NaN bit patterns, quiet or signaling, must be in the NaN range identified above.

作為參考,這里有一個與硬件相關的 NaN 表.總結:

For reference, there is a table of the hardware-dependant NaNs here. In summary:

- x86:     
   quiet:      Sign=0  Exp=0x7ff  Frac=0x80000
   signalling: Sign=0  Exp=0x7ff  Frac=0x40000
- PA-RISC:               
   quiet:      Sign=0  Exp=0x7ff  Frac=0x40000
   signalling: Sign=0  Exp=0x7ff  Frac=0x80000
- Power:
   quiet:      Sign=0  Exp=0x7ff  Frac=0x80000
   signalling: Sign=0  Exp=0x7ff  Frac=0x5555555500055555
- Alpha:
   quiet:      Sign=0  Exp=0      Frac=0xfff8000000000000
   signalling: Sign=1  Exp=0x2aa  Frac=0x7ff5555500055555

因此,要驗證這一點,您確實需要這些處理器之一并嘗試一下.此外,歡迎任何關于如何解釋 Power 和 Alpha 架構的較長值的見解.

So, to verify this you would really need one of these processors and go try it out. Also any insights on how to interpret the longer values for the Power and Alpha architectures are welcome.

這篇關于NaN 的位模式真的依賴于硬件嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | GEDORE扭力螺丝刀-GORDON防静电刷-CHEMTRONICS吸锡线-上海卓君电子有限公司 | 广东护栏厂家-广州护栏网厂家-广东省安麦斯交通设施有限公司 | 皮带式输送机械|链板式输送机|不锈钢输送机|网带输送机械设备——青岛鸿儒机械有限公司 | 高柔性拖链电缆-聚氨酯卷筒电缆-柔性屏蔽电缆厂家-玖泰电缆 | 精密钢管,冷拔精密无缝钢管,精密钢管厂,精密钢管制造厂家,精密钢管生产厂家,山东精密钢管厂家 | 管理会计网-PCMA初级管理会计,中级管理会计考试网站 | 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 北京中航时代-耐电压击穿试验仪厂家-电压击穿试验机 | 四合院设计_四合院装修_四合院会所设计-四合院古建设计与建造中心1 | 石家庄救护车出租_重症转院_跨省跨境医疗转送_活动赛事医疗保障_康复出院_放弃治疗_腾康26年医疗护送转诊团队 | 电子厂招聘_工厂招聘_普工招聘_小时工招聘信息平台-众立方招工网 | 紫外荧光硫分析仪-硫含量分析仪-红外光度测定仪-泰州美旭仪器 | 色谱柱-淋洗液罐-巴罗克试剂槽-巴氏吸管-5ml样品瓶-SBS液氮冻存管-上海希言科学仪器有限公司 | 美国PARKER齿轮泵,美国PARKER柱塞泵,美国PARKER叶片泵,美国PARKER电磁阀,美国PARKER比例阀-上海维特锐实业发展有限公司二部 | 温州食堂承包 - 温州市尚膳餐饮管理有限公司 | 淬火设备-钎焊机-熔炼炉-中频炉-锻造炉-感应加热电源-退火机-热处理设备-优造节能 | 中天寰创-内蒙古钢结构厂家|门式刚架|钢结构桁架|钢结构框架|包头钢结构煤棚 | 拉曼光谱仪_便携式|激光|显微共焦拉曼光谱仪-北京卓立汉光仪器有限公司 | 砖机托板价格|免烧砖托板|空心砖托板厂家_山东宏升砖机托板厂 | 内六角扳手「厂家」-温州市威豪五金工具有限公司 | 水质传感器_水质监测站_雨量监测站_水文监测站-山东水境传感科技有限公司 | 酒万铺-酒水招商-酒水代理 | 挤塑板-XPS挤塑板-挤塑板设备厂家[襄阳欧格] | 高中学习网-高考生信息学习必备平台 | BAUER减速机|ROSSI-MERSEN熔断器-APTECH调压阀-上海爱泽工业设备有限公司 | 广西绿桂涂料--承接隔热涂料、隔音涂料、真石漆、多彩仿石漆等涂料工程双包施工 | 建大仁科-温湿度变送器|温湿度传感器|温湿度记录仪_厂家_价格-山东仁科 | 便携式谷丙转氨酶检测仪|华图生物科技百科 | BESWICK球阀,BESWICK接头,BURKERT膜片阀,美国SEL继电器-东莞市广联自动化科技有限公司 | 液压中心架,数控中心架,自定心中心架-烟台恒阳机电设计有限公司 行星搅拌机,双行星搅拌机,动力混合机,无锡米克斯行星搅拌机生产厂家 | CCE素质教育博览会 | CCE素博会 | 教育展 | 美育展 | 科教展 | 素质教育展 | 高扬程排污泵_隔膜泵_磁力泵_节能自吸离心水泵厂家-【上海博洋】 | 杭州营业执照代办-公司变更价格-许可证办理流程_杭州福道财务管理咨询有限公司 | 碳钢法兰厂家,非标法兰,定制异型,法兰生产厂家-河北九瑞管道 | 杭州|上海贴标机-百科 | 上海平衡机-单面卧式动平衡机-万向节动平衡机-圈带动平衡机厂家-上海申岢动平衡机制造有限公司 | 连续密炼机_双转子连续密炼机_连续式密炼机-南京永睿机械制造有限公司 | 山东钢衬塑罐_管道_反应釜厂家-淄博富邦滚塑防腐设备科技有限公司 | 不锈钢复合板|钛复合板|金属复合板|南钢集团安徽金元素复合材料有限公司-官网 | 万家财经_财经新闻_在线财经资讯网|