杰瑞科技汇

Java long与double精度转换有啥坑?

Of course! This is a very common point of confusion for Java developers because Java's numeric types don't directly map to C/C++'s long double.

Java long与double精度转换有啥坑?-图1
(图片来源网络,侵删)

Let's break it down.

The Short Answer

In Java, there is no long double type.

Java has two floating-point types:

  1. float: A 32-bit single-precision floating-point number.
  2. double: A 64-bit double-precision floating-point number.

The double type in Java is the highest-precision floating-point type available and is what you should use for almost all floating-point calculations.

Java long与double精度转换有啥坑?-图2
(图片来源网络,侵删)

Detailed Comparison

Here is a table comparing Java's types with C/C++'s types to make the distinction clear.

Feature / Type long (Java) double (Java) float (Java) long long (C/C++) double (C/C++) long double (C/C++)
Category Integer Floating-Point Floating-Point Integer Floating-Point Floating-Point
Size (bits) 64 64 32 64 (usually) 64 80, 128, or more (platform-dependent)
Precision N/A (exact) ~15-17 decimal digits ~6-9 decimal digits N/A (exact) ~15-17 decimal digits 18-21 decimal digits (or more)
Purpose Whole numbers, large counts. General-purpose decimal numbers. Scientific, financial. Memory-efficient decimal numbers. Rarely used. Whole numbers, very large counts. General-purpose decimal numbers. High-precision decimal numbers. Scientific, financial.
Standard Strictly defined by the JLS. Strictly defined by the JLS (IEEE 754). Strictly defined by the JLS (IEEE 754). Defined by C/C++ standard (e.g., C11). Defined by C/C++ standard (IEEE 754). Implementation-defined by the compiler/platform.

Key Takeaways and Implications

double is the "High-Precision" Type in Java

When you need floating-point arithmetic in Java, you default to double. It offers 64 bits of precision, which is more than enough for the vast majority of applications, including scientific calculations, financial modeling (when combined with BigDecimal for exact arithmetic), and graphics.

// This is the standard and recommended way for high precision in Java.
double highPrecisionValue = 3.141592653589793;

No Platform-Dependent Floating-Point Sizes

A major reason Java doesn't have a long double is its "write once, run anywhere" philosophy. The size and precision of long double vary wildly between systems:

  • On many Linux systems with GCC, long double is 80 bits (extended precision).
  • On many Windows systems with MSVC, long double is the same as double (64 bits).
  • On some modern systems, it can be 128 bits.

This variability would break Java's portability guarantee. By defining float and double strictly to 32 and 64 bits respectively, Java ensures that a calculation will produce the exact same result on any JVM, whether it's on Windows, Linux, macOS, or a server.

Java long与double精度转换有啥坑?-图3
(图片来源网络,侵删)

What if I Need More Precision than double?

If you find that double's ~15-17 decimal digits of precision are not sufficient for your specific use case (e.g., complex scientific simulations, high-fidelity financial calculations), Java provides a solution: the java.math.BigDecimal class.

BigDecimal is not a primitive type. It's a class that represents immutable decimal numbers with arbitrary precision. You control the precision and rounding scale, making it ideal for financial calculations where accuracy is paramount.

Example:

import java.math.BigDecimal;
import java.math.RoundingMode;
public class HighPrecisionExample {
    public static void main(String[] args) {
        // Using double for pi (limited precision)
        double piDouble = 3.141592653589793;
        System.out.println("Pi as a double: " + piDouble);
        // Using BigDecimal for high precision
        BigDecimal piBigDecimal = new BigDecimal("3.14159265358979323846264338327950288419716939937510");
        System.out.println("Pi as a BigDecimal: " + piBigDecimal);
        // A calculation where precision matters
        double a = 0.1;
        double b = 0.2;
        System.out.println("Double sum: " + (a + b)); // Often prints 0.30000000000000004
        BigDecimal bdA = new BigDecimal("0.1");
        BigDecimal bdB = new BigDecimal("0.2");
        BigDecimal sum = bdA.add(bdB);
        System.out.println("BigDecimal sum: " + sum); // Correctly prints 0.3
        // Example with rounding
        BigDecimal price = new BigDecimal("99.995");
        BigDecimal roundedPrice = price.setScale(2, RoundingMode.HALF_UP);
        System.out.println("Rounded price: " + roundedPrice); // Prints 100.00
    }
}

Summary

If you need... In C/C++ you might use... In Java you should use...
A 64-bit integer long long long
Standard high-precision decimal number double double
Maximum precision decimal number long double java.math.BigDecimal
A 32-bit decimal number float float (rarely)
分享:
扫描分享到社交APP
上一篇
下一篇