简体   繁体   English

我们如何在 Java 中编写浮点数的二进制、八进制和十六进制文字?

[英]How we can write binary, octal and hexadecimal literals of floating point numbers in Java?

How we can write binary, octal and hexadecimal literals of floating point numbers in Java?我们如何在 Java 中编写浮点数的二进制、八进制和十六进制文字? Either in scientific or in normal representation.无论是在科学上还是在正常表示中。

class First
{
        public static void main(String[] args)
        {
                double b = 0x12p3;
                System.out.println(b);
        }
}

The above program results 144.0以上程序结果 144.0

class First
{
        public static void main(String[] args)
        {
                double b = 0x12e3;
                System.out.println(b);
        }
}

The above program results 4835.0以上程序结果4835.0

class First
{
            public static void main(String[] args)
            {
                    double b = 0x12e3;
                    System.out.println(b);
            }
}

The above program results 4835.0以上程序结果4835.0

class First
{
            public static void main(String[] args)
            {
                    double b = 012e3;
                    System.out.println(b);
            }
}

The above program results 12000以上程序结果 12000

Please explain above outputs.请解释以上输出。

The first example is a HexadecimalFloatingPointLiteral, (16+2)*8 = 144.第一个例子是一个 HexadecimalFloatingPointLiteral,(16+2)*8 = 144。

The second and third example is actually a HexIntegerLiteral assigned to a double variable, 1*16*16*16 + 2*16*16 + 14*16 + 3 = 4835. Note that the 'e' is just the hex digit for 14.第二个和第三个例子实际上是一个 HexIntegerLiteral 分配给一个 double 变量,1*16*16*16 + 2*16*16 + 14*16 + 3 = 4835。请注意,'e' 只是 14 的十六进制数字.

The last example is a DecimalFloatingPointLiteral, 12 * 1000.最后一个例子是一个 DecimalFloatingPointLiteral,12 * 1000。

Java does not support an octal floating point literals. Java 不支持八进制浮点文字。

For all the details see the JLS: https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.2有关所有详细信息,请参阅 JLS: https : //docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM