Java BigDecimal Log log10AbsCeil(BigDecimal x)

Here you can find the source of log10AbsCeil(BigDecimal x)

Description

log Abs Ceil

License

Open Source License

Declaration

public static int log10AbsCeil(BigDecimal x) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.math.BigDecimal;
import java.math.BigInteger;

public class Main {
    public static int log10AbsCeil(BigInteger x) {
        if (x.signum() == 0) {
            throw new IllegalArgumentException("x == 0");
        }//from   w  w  w  .  ja  va2 s  .  co m
        if (x.signum() < 0) {
            x = x.negate();
        }
        if (x.equals(BigInteger.ONE)) {
            return 0;
        }
        return x.subtract(BigInteger.ONE).toString().length();
    }

    public static int log10AbsCeil(BigDecimal x) {
        int log10 = log10AbsCeil(x.unscaledValue());
        return subInts(log10, x.scale());
    }

    public static int subInts(int a, int b) {
        if ((b > 0 && a < Integer.MIN_VALUE + b) || (b < 0 && a > Integer.MAX_VALUE + b)) {
            throw new ArithmeticException("int \"-\" overflow");
        }
        return a - b;
    }
}

Related

  1. log(int base_int, BigDecimal x)
  2. log10(BigDecimal b)
  3. log2AbsCeil(BigDecimal x)