Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : double to float type conversion is giving 'infinity' for larger values

Tags:

java

Suppose if i have a variable with some random larger values of type double:

double d = 4786777867867868654674678346734763478673478654478967.77;

now if i try to convert this into float at some point in my program, the output shows 'infinity' (in eclipse IDE):

float f = (float)d;    // inifinty
byte b = (byte)d;      // some valid value
short s = (short)d;    // some valid value
int i = (int)d;        // some valid value

Can someone give me any valid answers, how it is not converting for only for float datatype ?

like image 603
Ishwar Mahawar Avatar asked Sep 19 '18 05:09

Ishwar Mahawar


People also ask

Can you convert double to float in Java?

The floatValue() method returns the double value converted to type float .

What is the difference between float and double in Java?

Size: Float is of size 32 bits while double is of size 64 bits. Hence, double can handle much bigger fractional numbers than float. They differ in the allocation of bits for the representation of the number. Both float and double use 1 bit for representing the sign of the number.

How do you convert double to long value?

round() method. Math. round() accepts a double value and converts it into the nearest long value by adding 0.5 and removing its decimal points.

Can we assign double to float?

Double floatValue() in Java with ExamplesThe floatValue() method of Double class is a built-in method to return the value specified by the calling object as float after typecasting. Return Type: It returns a float value i.e. the type casted value of the DoubleObject.


2 Answers

Let's take a look at the result of casting this large double to each of the other numeric primitive types:

    double d = 4786777867867868654674678346734763478673478654478967.77;
    System.out.printf("float  %f\n", (float)d);
    System.out.printf("long   %d\n", (long)d);
    System.out.printf("int    %d\n", (int)d);
    System.out.printf("short  %d\n", (short)d);
    System.out.printf("byte   %d\n", (byte)d);

Output:

float Infinity
long  9223372036854775807
int   2147483647
short -1
byte  -1

Float

From the JLS:

A narrowing primitive conversion from double to float is governed by the IEEE 754 rounding rules (§4.2.4). This conversion can lose precision, but also lose range, resulting in a float zero from a nonzero double and a float infinity from a finite double.

Basically, IEEE 754 mandates this behaviour. IEEE 754 sets aside a specific bit pattern to represent infinity, and all floating-point operations involving this value are defined.

Long & Int

The JLS states that in the case of a narrowing primitive conversion from double to long or int, where the value is too large to fit in the range (64 or 32 bit signed integer), the largest representable value should be used, Long.MAX_VALUE and Integer.MAX_VALUE;

Short & Byte

In casting a double to a short or byte, the number is first cast to an int, using the rule above. It is then cast from int to short or byte by discarding all but the n lowest bits (16 for short, 8 for byte). Since all but the high bit of Integer.MAX_VALUE are set to 1, the short and byte values have all bits set, which corresponds to -1 in signed two's complement.

like image 194
RaffleBuffle Avatar answered Nov 15 '22 07:11

RaffleBuffle


Float range is from 1.40129846432481707e-45 to 3.40282346638528860e+38.

The double value cannot fit in it. Java specs also say:

This conversion can lose precision, but also lose range, resulting in a float zero from a nonzero double and a float infinity from a finite double.

like image 33
S.K. Avatar answered Nov 15 '22 07:11

S.K.