d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Difference Between Float & Double In Java
Add Reply New Topic New Poll
Member
Posts: 31,292
Joined: Mar 25 2009
Gold: 0.00
Feb 13 2017 01:01am
Can someone explain the difference to me, i dont really understand it... I know they're both decimal values...
Also plz give examples of both
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Feb 13 2017 02:52am
https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=difference%20between%20float%20and%20double

google answers "difference between float and double" without even linking, you have been working on programming for like a year now i dont mean to be a dick but you gotta learn to use google if you want to keep growing

double can handle numbers with twice as many digits

This post was edited by Ideophobe on Feb 13 2017 02:53am
Member
Posts: 31,292
Joined: Mar 25 2009
Gold: 0.00
Feb 13 2017 03:13am
Quote (Ideophobe @ Feb 13 2017 04:52am)
https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=difference%20between%20float%20and%20double

google answers "difference between float and double" without even linking, you have been working on programming for like a year now i dont mean to be a dick but you gotta learn to use google if you want to keep growing

double can handle numbers with twice as many digits


everything in google says the same thing, float is 32bit, double is 64bit
that does not explain what i wanna know

what i wanna know are examples of both of these, so i can see the difference.

https://www.tutorialspoint.com/java/java_basic_datatypes.htm

float:
Example: float f1 = 234.5f

double:
Example: double d1 = 123.4



I really dont' see the difference between the two.. one is 234.5, the other is 123.4
Member
Posts: 36,123
Joined: Jul 18 2008
Gold: 2,407.00
Feb 13 2017 05:56am
Double can hold more decimal places than float. Iirc float is 7 and double is 15, but I might be off. So:

float x = 6.7481738

double y = 7.648264916491649

There's really no reason to use float over double imo.

This post was edited by Mastersam93 on Feb 13 2017 05:58am
Member
Posts: 31,292
Joined: Mar 25 2009
Gold: 0.00
Feb 13 2017 05:42pm
Quote (Mastersam93 @ Feb 13 2017 07:56am)
Double can hold more decimal places than float. Iirc float is 7 and double is 15, but I might be off. So:

float x = 6.7481738

double y = 7.648264916491649

There's really no reason to use float over double imo.


Thanks! :)
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 13 2017 06:45pm
Quote (Mastersam93 @ Feb 13 2017 06:56am)
Double can hold more decimal places than float. Iirc float is 7 and double is 15, but I might be off. So:

float x = 6.7481738

double y = 7.648264916491649

There's really no reason to use float over double imo.


it's not about the number of digits. they're binary numbers.

https://en.wikipedia.org/wiki/IEEE_floating_point#Basic_and_interchange_formats

Quote
To sum up:

float is represented in 32 bits, with 1 sign bit, 8 bits of exponent, and 23 bits of the mantissa (or what follows from a scientific-notation number: 2.33728*1012; 33728 is the mantissa).
double is represented in 64 bits, with 1 sign bit, 11 bits of exponent, and 52 bits of mantissa.
Member
Posts: 36,123
Joined: Jul 18 2008
Gold: 2,407.00
Feb 13 2017 06:52pm
Quote (carteblanche @ Feb 13 2017 07:45pm)
it's not about the number of digits. they're binary numbers.

https://en.wikipedia.org/wiki/IEEE_floating_point#Basic_and_interchange_formats


More bits = more decimal precision.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 13 2017 06:59pm
Quote (Mastersam93 @ Feb 13 2017 07:52pm)
More bits = more decimal precision.


Code
double d = 0.2+0.01;
float f = 0.2f+0.01f;
System.out.println(d);
System.out.println(f);


there are only two digits past the decimal in this example. what do you think will happen?

This post was edited by carteblanche on Feb 13 2017 07:02pm
Member
Posts: 36,123
Joined: Jul 18 2008
Gold: 2,407.00
Feb 13 2017 07:17pm
What it prints and the internal representation are different. Java conforms to the IEEE 754 standard. Doubles have more significant digits than floats. It is just a fact.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 13 2017 10:42pm
If anyone cares I did a write up on float to binary conversion years ago with azrad trying to figure out how decimal numbers were stored in assembly. No idea how if it is correct or not, but it seemed to line up with the few test numbers we calculated. Most of this was reverse engineering with very little google searching.

Code
Create a number in decimal, lets use:

3.125

Remove the whole value of 3 and save it for later on


Start multiplying the remainder decimal by 2 until it cleans up to 1.00 or you hit 23 bits of precision. If you hit 1.00 with an unclean decimal (ex 1.1) add a 1 to the binary record where that place resides and keep going until 23 bits of precision is reached. If you finish early (hit 1.00 exactly) pad the record until 23 bits are met.

In this example we reach 1.0 in 3 iterations so we add a 1 in the third place in the record.

0.125 * 2 = 0.25
0.25 * 2 = 0.5
0.5 * 2 = 1.0

00100000000000000000000

Now take the whole number we had previously taken off and convert it to binary

3 = 0011

Now add it to the decimal binary we had calculated previously.

0011.00100000000000000000000

Now shift this binary number left until we hit a normalized number (001.xxxx).

001.10010000000000000000000

The first bit is an assumed hidden bit in floating points and is knocked off the number.

Afterwards we calculate the number of times we shifted our whole value left to generate a normalized number. 0 times actually starts at 127 in decimal. We shifted left one time.

01111111 = 127 + 1 = 10000000

Add the sign bit (0 for positive, 1 for negative) to the rest of our record.

0 10000000 10010000000000000000000

Afterwards convert the value to hex.

0x40480000

Time to reverse it! (Convert hex to binary before hand)

0 10000000 10010000000000000000000

Store the hidden bit for signedness later. Also calculate how many times we shifted to the left by subtracting 10000000 from 01111111.

1.100100000000000000000000

Shift the value to the right the number of times calculated above (in this case once)

0011.00100000000000000000000

Remove the whole number and convert it. Store it for later.

0011 = 3

Start at 0.0 and apply the record going right to left adding 1 for every 1 and divide by 2 each iteration.

0.0 + 1 / 2 = 0.5
0.5 + 0 /2 = 0.25
0.25 + 0 /2 = 0.125

Combine our whole number with our decimal value.

3.125
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll