Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53647

Is there any replacement of long double in java?

I was reading about data types in C. I found the range of long double is more than that of double in java. For very huge number we can use long double in C. If I want to store the same number in java what we have to do? which datatype we can use?

double in java takes 8 bytes(64 bits) IEEE 754. it Covers a range from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative).

longdouble in c takes 10 bytes (80 bits)

Can anyone tell me is there any replacement of longdouble in java

Upvotes: 17

Views: 39449

Answers (5)

Manikandan Sigamani
Manikandan Sigamani

Reputation: 1984

Though not a replacement, you can use java.lang.math.BigDecimal.You can roughly store billion of digits until you run out of memory. Its an arbitrary precision class, it will get as large as you'd like until your computer runs out of memory.

As per the documentation of BigDecimal:

Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a non-negative 32-bit integer scale, which represents the number of digits to the right of the decimal point. The number represented by the BigDecimal is (unscaledValue/10scale). BigDecimal provides operations for basic arithmetic, scale manipulation, comparison, hashing, and format conversion.

Upvotes: 11

Anonymous
Anonymous

Reputation: 171

No, but you can use the Java Native Interface to call a native method that performs the calculation using the long double type. You can then store it in 10 bytes or truncate it.

If a final truncation to double is acceptable, depending on the VM you use it's possible that intermediate values are stored in long double anyway. In this case you don't need the native method.

Upvotes: 1

Manikandan Sigamani
Manikandan Sigamani

Reputation: 1984

Documentation of BigDecimal states:

add(BigDecimal augend) Returns a BigDecimal whose value is (this + augend), and whose scale is max(this.scale(), augend.scale()).

import java.math.BigDecimal;

    public class AddTwoBigNumbers{
      public static void main(String[] args) {
      BigDecimal num1, num2;
      num1 = new BigDecimal(50.00035);
      num2 = new BigDecimal(100.0025);
      Sum(num1, num2);
      }

      public static void Sum(BigDecimal val1, BigDecimal val2){
      BigDecimal sum = val1.add(val2);
      System.out.println("Sum of two BigDecimal numbers: "+ sum);
      }
    } 

Upvotes: 1

Artem
Artem

Reputation: 4397

There is no straight replacement in Java.

You can use BigDecimal for this purpose.

You should understand that the bigger your double value is, the bigger lost of precision you will receive using it in your mathematical operations. BigDecimal helps you to aware this problem.

Here is code sample with BigDecimal:

String decimalString = "1423545135143513.523";
BigDecimal decimal = new BigDecimal(decimalString);

By this link you can find many examples with usage of BigDecimal class.

Upvotes: 3

HeavenAgain
HeavenAgain

Reputation: 435

As for primitive type, there are none asides from double and float, which handles floating point.

But, BigDecimal could be what you are looking for.

Upvotes: 2

Related Questions