Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any replacement of long double in java?

Tags:

java

c

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

like image 313
Sunil Kumar Sahoo Avatar asked Jan 06 '12 06:01

Sunil Kumar Sahoo


People also ask

How do you represent a long double in Java?

Double longValue() in Java with ExamplesThe longValue() method of Double class is a built in method to return the value specified by the calling object as long after type casting. Parameters : It takes no parameters. Return type : It returns an long value i.e. the type casted value of the DoubleObject.

How do I make my doubles longer?

Let's check a straightforward way to cast the double to long using the cast operator: Assert. assertEquals(9999, (long) 9999.999); Applying the (long) cast operator on a double value 9999.999 results in 9999.

Is double bigger than Long Java?

In an article on MSDN, it states that the double data type has a range of "-1.79769313486232e308 .. 1.79769313486232e308". Whereas the long data type only has a range of "-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807".


1 Answers

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.

like image 92
Manikandan Sigamani Avatar answered Sep 28 '22 04:09

Manikandan Sigamani