17

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

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • Bigdecimal is not a dataype we cannot do arithmetic operations directly using arithmetic operators. – Sunil Kumar Sahoo Jan 06 '12 at 07:13
  • `long double` is compiler specific in C, on most platforms it is the same size as `double`. – Ben Voigt Jan 06 '12 at 07:39
  • 1
    long double could also be 106, 107 or 128 bits in C. Java will not try to emulate all datatypes of all C implementations on all platforms. – Roger Lindsjö Jan 06 '12 at 07:45
  • I thought it was 80-bit on x86 platforms. – Peter Lawrey Jan 06 '12 at 08:04
  • 1
    @Peter: Go try it, on your favorite compiler. But prepare to be disappointed. Yes, some compilers implement 80-bit `long double` on x86. Most do not. For one thing, it requires use of the x87 co-processor, which hasn't been x86's fastest FPU for a loooooong time. – Ben Voigt Jan 06 '12 at 15:01
  • 2
    "long double" is [IEEE 754 double extended](http://en.wikipedia.org/wiki/Extended_precision), and see [How Java’s Floating-Point Hurts Everyone Everywhere](http://www.cs.berkeley.edu/~wkahan/JAVAhurt.pdf), page 67ff on why having it in a language is an Extremely Good Idea: It's not because of "very huge numbers", it's because of "more precise numbers". – David Tonhofer Dec 26 '14 at 12:55

5 Answers5

11

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.

eli
  • 8,571
  • 4
  • 30
  • 40
Manikandan Sigamani
  • 1,964
  • 1
  • 15
  • 28
3

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.

Artem
  • 4,347
  • 2
  • 22
  • 22
  • But I cannot use arithmetic operators for arithmetic calculation like addition, subtraction etc – Sunil Kumar Sahoo Jan 06 '12 at 07:14
  • You can. e.g. decimal.divide(new BigDecimal("11.11")); Please, read API by the link i have posted. I edited my post, follow the link and learn some examples. – Artem Jan 06 '12 at 07:16
  • decimal.divide() is not an operator it is a method? I mentioned in my comments that we cannot use arithmetic operators – Sunil Kumar Sahoo Jan 06 '12 at 07:21
  • It's a method, of cource, but it performs division operation. Java doesn't allow custom operator overriding/overloading, so, you cannot use +/-/* etc. operations with BigDecimals(and many other non-primitive types). – Artem Jan 06 '12 at 07:23
  • using an operator or calling a method is the same for the java compiler. its just more to write for you – Hachi Jan 06 '12 at 07:23
  • @SunilKumarSahoo Why can you not use methods instead of operators? – Roger Lindsjö Jan 06 '12 at 07:47
2

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.

HeavenAgain
  • 435
  • 1
  • 6
  • 14
  • But I cannot use arithmetic operators for arithmetic calculation like addition, subtraction etc – Sunil Kumar Sahoo Jan 06 '12 at 07:14
  • You cannot use normal arithmetic operators because this is an object. You will have to use methods in the API, like .add(), .subtract(), etc... It is a bit tedious, but in return, you will have more power and other fancy operations. But be careful with division, because it will throw exceptions if it's like 1/3, then it will be Non-terminating decimal expansion. – HeavenAgain Jan 06 '12 at 07:25
1

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);
      }
    } 
Manikandan Sigamani
  • 1,964
  • 1
  • 15
  • 28
0

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.

Anonymous
  • 161
  • 1
  • 2