Skip to main content
added 146 characters in body
Source Link
assylias
  • 329.9k
  • 84
  • 680
  • 806

To see the exact value of your float, you can use a BigDecimal:

float x = 0.98123452f;
System.out.println(new BigDecimal(x));

which outputs: 0.981234490871429443359375

To see the exact value of your float, you can use a BigDecimal:

float x = 0.98123452f;
System.out.println(new BigDecimal(x));

which outputs: 0.981234490871429443359375

So technically, this float:

float x = 0.981234490871429443359375f;
System.out.println(new BigDecimal(x)); //prints 0.981234490871429443359375

has 24 digits precision (it was obviously cherry-picked)...

So technically, this floatForget what is above:

float x = 0.981234490871429443359375f;
System.out.println(new BigDecimal(x)); //prints 0.981234490871429443359375

BigDecimal only has 24 digits precision (ita double constructor so there was obviously cherry-picked)..a cast to double and the logic above is flawed.

Bottom line: not all numbers can be represented as a float and the gap between one float and the next one varies depending on the magnitude of the number.

To see the exact value of your float, you can use a BigDecimal:

float x = 0.98123452f;
System.out.println(new BigDecimal(x));

which outputs: 0.981234490871429443359375

So technically, this float:

float x = 0.981234490871429443359375f;
System.out.println(new BigDecimal(x)); //prints 0.981234490871429443359375

has 24 digits precision (it was obviously cherry-picked)...

Bottom line: not all numbers can be represented as a float and the gap between one float and the next one varies depending on the magnitude of the number.

To see the exact value of your float, you can use a BigDecimal:

float x = 0.98123452f;
System.out.println(new BigDecimal(x));

which outputs: 0.981234490871429443359375

So technically, this float:

float x = 0.981234490871429443359375f;
System.out.println(new BigDecimal(x)); //prints 0.981234490871429443359375

has 24 digits precision (it was obviously cherry-picked)...

Forget what is above: BigDecimal only has a double constructor so there was a cast to double and the logic above is flawed.

Bottom line: not all numbers can be represented as a float and the gap between one float and the next one varies depending on the magnitude of the number.

Source Link
assylias
  • 329.9k
  • 84
  • 680
  • 806

To see the exact value of your float, you can use a BigDecimal:

float x = 0.98123452f;
System.out.println(new BigDecimal(x));

which outputs: 0.981234490871429443359375

So technically, this float:

float x = 0.981234490871429443359375f;
System.out.println(new BigDecimal(x)); //prints 0.981234490871429443359375

has 24 digits precision (it was obviously cherry-picked)...

Bottom line: not all numbers can be represented as a float and the gap between one float and the next one varies depending on the magnitude of the number.