计算十进制值的双重邻居 [英] Calculating double neighbours of a decimal value

查看:172
本文介绍了计算十进制值的双重邻居的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

  import java.math。*; 

public class x
{
public static void main(String [] args)
{
BigDecimal a = new BigDecimal(0.1);
BigDecimal b = new BigDecimal(0.7);
System.out.println(a);
System.out.println(b);




$ b $输出:

0.1000000000000000055511151231257827021181583404541015625
0.6999999999999999555910790149937383830547332763671875



这很好,因为它让我找到了最接近给定值的 double
但是至于 0.1 值越大, 0.7 的值越小于实际值。 >

我怎么能得到任何十进制数的两个值(最接近的大和最小)?假设我开始 BigDecimal ,然后将其转换为 double ,然后返回十进制。我会变得更大或更小的价值。我怎么能得到另一个?

解决方案

使用 nextAfter(double start,double direction)

  import java.math。*; 

public class a
{
public static void printNeighbours(BigDecimal start)
{
double result1 = start.doubleValue();
double result2;

BigDecimal r1 = new BigDecimal(result1);

int com = start.compareTo(r1);

if(com!= 0)
result2 = Math.nextAfter(result1,com * Double.MAX_VALUE);
else
{
result2 = Math.nextAfter(result1,Double.MAX_VALUE);
result1 = Math.nextAfter(result1,-Double.MAX_VALUE);
r1 = new BigDecimal(result1);
}

BigDecimal r2 = new BigDecimal(result2);

System.out.println(starting:\t+ start);

if(com< 0)
{
System.out.println(smaller:\t+ r2);
System.out.println(greater:\t\t+ r1);
}
else
{
System.out.println(smaller:\t+ r1);
System.out.println(greater:\t\t+ r2);
}

System.out.println();


public static void main(String [] args)
{
printNeighbours(new BigDecimal(0.25));
printNeighbours(new BigDecimal(0.1));
printNeighbours(new BigDecimal(0.7));




$ b

打印输出:

 首发:0.25 
更小:0.2499999999999999722444243843710864894092082977294921875
更大:0.250000000000000055511151231257827021181583404541015625

首发:0.1
更小:0.09999999999999999167332731531132594682276248931884765625
更大:0.1000000000000000055511151231257827021181583404541015625

开始:0.7
更小:0.6999999999999999555910790149937383830547332763671875
更大:0.70000000000000006661338147750939242541790008544921875


The code:

import java.math.*; 

public class x
{
  public static void main(String[] args)
  {
    BigDecimal a = new BigDecimal(0.1);
    BigDecimal b = new BigDecimal(0.7);
    System.out.println(a);
    System.out.println(b);
  }
}

The output:

0.1000000000000000055511151231257827021181583404541015625 0.6999999999999999555910790149937383830547332763671875

This is nice because it lets me find a double that is the closest to given value. But as for 0.1 a value is bigger and for 0.7 value is smaller than real value.

How can I get both values (closest bigger and closest smaller) for any decimal number?

Assume I start with BigDecimal, then convert it do a double and then back to decimal. I'll get bigger or smaller value. How could I got the other?

解决方案

Use nextAfter(double start, double direction):

import java.math.*;

public class a
{
  public static void printNeighbours(BigDecimal start)
  {
    double result1 = start.doubleValue();
    double result2;

    BigDecimal r1 = new BigDecimal(result1);

    int com = start.compareTo(r1);

    if(com != 0)
      result2 = Math.nextAfter(result1, com * Double.MAX_VALUE);
    else
    {
      result2 = Math.nextAfter(result1, Double.MAX_VALUE);
      result1 = Math.nextAfter(result1, -Double.MAX_VALUE);
      r1 = new BigDecimal(result1);
    }

    BigDecimal r2 = new BigDecimal(result2);

    System.out.println("starting:\t"+start);

    if(com<0)
    {
      System.out.println("smaller:\t" + r2);        
      System.out.println("bigger:\t\t"+r1);
    }
    else
    {
      System.out.println("smaller:\t" + r1);        
      System.out.println("bigger:\t\t"+r2);
    }

    System.out.println();
  }

  public static void main(String[] args)
  {
    printNeighbours(new BigDecimal("0.25"));
    printNeighbours(new BigDecimal("0.1"));
    printNeighbours(new BigDecimal("0.7"));
  }
}

Printout:

starting:   0.25
smaller:    0.2499999999999999722444243843710864894092082977294921875
bigger:     0.250000000000000055511151231257827021181583404541015625

starting:   0.1
smaller:    0.09999999999999999167332731531132594682276248931884765625
bigger:     0.1000000000000000055511151231257827021181583404541015625

starting:   0.7
smaller:    0.6999999999999999555910790149937383830547332763671875
bigger:     0.70000000000000006661338147750939242541790008544921875

这篇关于计算十进制值的双重邻居的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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