Precision lost in float value using java

The following is the test code and its output. When I get the float value from a number, accuracy is lost. Can someone tell me why this behavior, and also how to handle it?

public static void main(String[] args) { try { java.lang.Number numberVal = 676543.21; float floatVal = numberVal.floatValue(); System.out.println("Number value : " + numberVal); System.out.println("float value : " + floatVal); System.out.println("Float.MAX_VALUE : " + Float.MAX_VALUE); System.out.println("Is floatVal > Float.MAX_VALUE ? " + ( floatVal > Float.MAX_VALUE)); }catch(Exception e) { e.printStackTrace(); } } 

output:

  Number value : 676543.21 float value : 676543.2 Float.MAX_VALUE : 3.4028235E38 Is floatVal > Float.MAX_VALUE ? false 

also why is the float value less than Float.MAX_VALUE?

+4
source share
5 answers

I love them. But I will do it quickly and painlessly.

Floats and decimals (in other words, floating points) are also not stored in memory. But I do not want to go into the accuracy of the float and the accuracy of the questions here, I just point you to the link - http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm p>

As for your other question, lemme put it this way because floats are stored in scientific notation.

 floatVal = 6.765432E6 = 6.7 * 10^6 MAX_VALUE = 3.4E38 = 3.4 * 10^38 

MAX_VALUE is 32 orders of magnitude larger than your floating number. Feel free to also see here http://steve.hollasch.net/cgindex/coding/ieeefloat.html

I spent a lot of time comparing and fixing some FP issues a few months ago ...

Try using a small delta when comparing floats. Perhaps this link will help you http://introcs.cs.princeton.edu/java/91float/

+7
source

float are usually good up to 6 significant digits. Your number is 676543.21 , which has 8 significant digits. You will see errors in the last 6 digits, and these errors will easily spread to more significant digits, the more calculations you perform. If you value your sanity (or accuracy), use double s. Floats cannot even accurately calculate the past 10 million.

Now you have 2 significant numbers that tell me that there is a chance that you want to represent the currency - NOT. Use your own class that internally represents values ​​using fixed-point arithmetic.

Now, with regard to Float.MAX_VAL , which is 3.4028235E38, which means 3.4028235 * 10 ^ 38, which is about 10-32 times your value. Pay attention to the "E" there? This is an indicator.

+19
source

All data types have presentation limits, so the fact that there is a limit should not be surprising.

float uses 24-bit for its "mantissa", which contains all the significant digits. This means that it has about 7 digits of accuracy (since 2 ^^ 24 is about 16 million)

double uses a 53-bit mantissa for it, so it can contain about 16 digits.

+7
source

3.4028235E38 is greater than 676543.2. Float.MAX_VALUE is the largest float imaginable.

+2
source

Decimal literals, such as the one you typed by default, enter double , not float . java.lang.Number also uses default doubling. If you replaced this with java.lang.Number numberVal = 676543.21f; , I would expect the same level of accuracy of losses from both. Alternatively replace float floatVal = numberVal.floatValue(); on double doubleVal = numberVal.doubleValue(); In order not to lose accuracy.

As an example, try EDIT:

 Number num = 676543.21; System.out.println(num); //676543.21 System.out.println(num.doubleValue()); //676543.21 System.out.println(num.floatValue()); //676543.2 <= loses the precision 

to see the difference in type accuracy

Float.MAX_VALUE returns the largest value that a float can hold. any excess overflow. If you look closely, you will see an β€œE” in your text view. This is the standard form of the index, that "E" means "multiply by 10 by the power of any number following E" (or in java-speak * pow (10, numberAfterE))

0
source

All Articles