精确度损失在浮点值使用Java [英] Precision lost in float value using java

查看:275
本文介绍了精确度损失在浮点值使用Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出下面的测试代码和它的输出。当我从数值中获得浮点值时,精度就会丢失。谁能告诉我为什么这个行为以及如何处理?

  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();




$输出:

 数字值:676543.21 
浮点值:676543.2
Float.MAX_VALUE:3.4028235E38
是floatVal> Float.MAX_VALUE? false

为什么float值小于Float.MAX_VALUE?

解决方案

我喜欢这些。但是我会快速和无痛的。



浮点数和小数点(也称为浮点数)并不保存在内存中。但是我不希望进入浮点精度与精度问题,我只是指向你的链接 - http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm



至于你的其他问题,这样说吧,因为浮游物以科学记数法存储。

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

MAX_VALUE是32个数量级更大比你的浮点数。请随时在这里看看 http://steve.hollasch.net/cgindex /coding/ieeefloat.html



几个月前,我花了很多时间比较和解决一些FP问题...



尝试比较浮点数时使用小的三角形。
也许这个链接将帮助你 http://introcs.cs.princeton.edu/ java / 91float /


Given below the test code and its output. When I get float value from number value, precision is lost.. Can anyone tell me why this behaviour and also how to handle this?

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 the float value lesser than Float.MAX_VALUE?

解决方案

I love these. But I'll make it quick and painless.

Floats and decimals (aka floating points) aren't kept in the memory precisely either. But I don't want to get into float accuracy vs precision issues here, i'll just point you to a link - http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

As far as your other question, lemme put it this way, since 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 bigger than your float number. Feel free to take a look at here as well http://steve.hollasch.net/cgindex/coding/ieeefloat.html

I've spent a great deal of time comparing and fixing some FP issues a few months ago...

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

这篇关于精确度损失在浮点值使用Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆