|
Java example source code file (DfpField.java)
The DfpField.java Java example source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math3.dfp; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; /** Field for Decimal floating point instances. * @since 2.2 */ public class DfpField implements Field<Dfp> { /** Enumerate for rounding modes. */ public enum RoundingMode { /** Rounds toward zero (truncation). */ ROUND_DOWN, /** Rounds away from zero if discarded digit is non-zero. */ ROUND_UP, /** Rounds towards nearest unless both are equidistant in which case it rounds away from zero. */ ROUND_HALF_UP, /** Rounds towards nearest unless both are equidistant in which case it rounds toward zero. */ ROUND_HALF_DOWN, /** Rounds towards nearest unless both are equidistant in which case it rounds toward the even neighbor. * This is the default as specified by IEEE 854-1987 */ ROUND_HALF_EVEN, /** Rounds towards nearest unless both are equidistant in which case it rounds toward the odd neighbor. */ ROUND_HALF_ODD, /** Rounds towards positive infinity. */ ROUND_CEIL, /** Rounds towards negative infinity. */ ROUND_FLOOR; } /** IEEE 854-1987 flag for invalid operation. */ public static final int FLAG_INVALID = 1; /** IEEE 854-1987 flag for division by zero. */ public static final int FLAG_DIV_ZERO = 2; /** IEEE 854-1987 flag for overflow. */ public static final int FLAG_OVERFLOW = 4; /** IEEE 854-1987 flag for underflow. */ public static final int FLAG_UNDERFLOW = 8; /** IEEE 854-1987 flag for inexact result. */ public static final int FLAG_INEXACT = 16; /** High precision string representation of √2. */ private static String sqr2String; // Note: the static strings are set up (once) by the ctor and @GuardedBy("DfpField.class") /** High precision string representation of √2 / 2. */ private static String sqr2ReciprocalString; /** High precision string representation of √3. */ private static String sqr3String; /** High precision string representation of √3 / 3. */ private static String sqr3ReciprocalString; /** High precision string representation of ?. */ private static String piString; /** High precision string representation of e. */ private static String eString; /** High precision string representation of ln(2). */ private static String ln2String; /** High precision string representation of ln(5). */ private static String ln5String; /** High precision string representation of ln(10). */ private static String ln10String; /** The number of radix digits. * Note these depend on the radix which is 10000 digits, * so each one is equivalent to 4 decimal digits. */ private final int radixDigits; /** A {@link Dfp} with value 0. */ private final Dfp zero; /** A {@link Dfp} with value 1. */ private final Dfp one; /** A {@link Dfp} with value 2. */ private final Dfp two; /** A {@link Dfp} with value √2. */ private final Dfp sqr2; /** A two elements {@link Dfp} array with value √2 split in two pieces. */ private final Dfp[] sqr2Split; /** A {@link Dfp} with value √2 / 2. */ private final Dfp sqr2Reciprocal; /** A {@link Dfp} with value √3. */ private final Dfp sqr3; /** A {@link Dfp} with value √3 / 3. */ private final Dfp sqr3Reciprocal; /** A {@link Dfp} with value ?. */ private final Dfp pi; /** A two elements {@link Dfp} array with value ? split in two pieces. */ private final Dfp[] piSplit; /** A {@link Dfp} with value e. */ private final Dfp e; /** A two elements {@link Dfp} array with value e split in two pieces. */ private final Dfp[] eSplit; /** A {@link Dfp} with value ln(2). */ private final Dfp ln2; /** A two elements {@link Dfp} array with value ln(2) split in two pieces. */ private final Dfp[] ln2Split; /** A {@link Dfp} with value ln(5). */ private final Dfp ln5; /** A two elements {@link Dfp} array with value ln(5) split in two pieces. */ private final Dfp[] ln5Split; /** A {@link Dfp} with value ln(10). */ private final Dfp ln10; /** Current rounding mode. */ private RoundingMode rMode; /** IEEE 854-1987 signals. */ private int ieeeFlags; /** Create a factory for the specified number of radix digits. * <p> * Note that since the {@link Dfp} class uses 10000 as its radix, each radix * digit is equivalent to 4 decimal digits. This implies that asking for * 13, 14, 15 or 16 decimal digits will really lead to a 4 radix 10000 digits in * all cases. * </p> * @param decimalDigits minimal number of decimal digits. */ public DfpField(final int decimalDigits) { this(decimalDigits, true); } /** Create a factory for the specified number of radix digits. * <p> * Note that since the {@link Dfp} class uses 10000 as its radix, each radix * digit is equivalent to 4 decimal digits. This implies that asking for * 13, 14, 15 or 16 decimal digits will really lead to a 4 radix 10000 digits in * all cases. * </p> * @param decimalDigits minimal number of decimal digits * @param computeConstants if true, the transcendental constants for the given precision * must be computed (setting this flag to false is RESERVED for the internal recursive call) */ private DfpField(final int decimalDigits, final boolean computeConstants) { this.radixDigits = (decimalDigits < 13) ? 4 : (decimalDigits + 3) / 4; this.rMode = RoundingMode.ROUND_HALF_EVEN; this.ieeeFlags = 0; this.zero = new Dfp(this, 0); this.one = new Dfp(this, 1); this.two = new Dfp(this, 2); if (computeConstants) { // set up transcendental constants synchronized (DfpField.class) { // as a heuristic to circumvent Table-Maker's Dilemma, we set the string // representation of the constants to be at least 3 times larger than the // number of decimal digits, also as an attempt to really compute these // constants only once, we set a minimum number of digits computeStringConstants((decimalDigits < 67) ? 200 : (3 * decimalDigits)); // set up the constants at current field accuracy sqr2 = new Dfp(this, sqr2String); sqr2Split = split(sqr2String); sqr2Reciprocal = new Dfp(this, sqr2ReciprocalString); sqr3 = new Dfp(this, sqr3String); sqr3Reciprocal = new Dfp(this, sqr3ReciprocalString); pi = new Dfp(this, piString); piSplit = split(piString); e = new Dfp(this, eString); eSplit = split(eString); ln2 = new Dfp(this, ln2String); ln2Split = split(ln2String); ln5 = new Dfp(this, ln5String); ln5Split = split(ln5String); ln10 = new Dfp(this, ln10String); } } else { // dummy settings for unused constants sqr2 = null; sqr2Split = null; sqr2Reciprocal = null; sqr3 = null; sqr3Reciprocal = null; pi = null; piSplit = null; e = null; eSplit = null; ln2 = null; ln2Split = null; ln5 = null; ln5Split = null; ln10 = null; } } /** Get the number of radix digits of the {@link Dfp} instances built by this factory. * @return number of radix digits */ public int getRadixDigits() { return radixDigits; } /** Set the rounding mode. * If not set, the default value is {@link RoundingMode#ROUND_HALF_EVEN}. * @param mode desired rounding mode * Note that the rounding mode is common to all {@link Dfp} instances * belonging to the current {@link DfpField} in the system and will * affect all future calculations. */ public void setRoundingMode(final RoundingMode mode) { rMode = mode; } /** Get the current rounding mode. * @return current rounding mode */ public RoundingMode getRoundingMode() { return rMode; } /** Get the IEEE 854 status flags. * @return IEEE 854 status flags * @see #clearIEEEFlags() * @see #setIEEEFlags(int) * @see #setIEEEFlagsBits(int) * @see #FLAG_INVALID * @see #FLAG_DIV_ZERO * @see #FLAG_OVERFLOW * @see #FLAG_UNDERFLOW * @see #FLAG_INEXACT */ public int getIEEEFlags() { return ieeeFlags; } /** Clears the IEEE 854 status flags. * @see #getIEEEFlags() * @see #setIEEEFlags(int) * @see #setIEEEFlagsBits(int) * @see #FLAG_INVALID * @see #FLAG_DIV_ZERO * @see #FLAG_OVERFLOW * @see #FLAG_UNDERFLOW * @see #FLAG_INEXACT */ public void clearIEEEFlags() { ieeeFlags = 0; } /** Sets the IEEE 854 status flags. * @param flags desired value for the flags * @see #getIEEEFlags() * @see #clearIEEEFlags() * @see #setIEEEFlagsBits(int) * @see #FLAG_INVALID * @see #FLAG_DIV_ZERO * @see #FLAG_OVERFLOW * @see #FLAG_UNDERFLOW * @see #FLAG_INEXACT */ public void setIEEEFlags(final int flags) { ieeeFlags = flags & (FLAG_INVALID | FLAG_DIV_ZERO | FLAG_OVERFLOW | FLAG_UNDERFLOW | FLAG_INEXACT); } /** Sets some bits in the IEEE 854 status flags, without changing the already set bits. * <p> * Calling this method is equivalent to call {@code setIEEEFlags(getIEEEFlags() | bits)} * </p> * @param bits bits to set * @see #getIEEEFlags() * @see #clearIEEEFlags() * @see #setIEEEFlags(int) * @see #FLAG_INVALID * @see #FLAG_DIV_ZERO * @see #FLAG_OVERFLOW * @see #FLAG_UNDERFLOW * @see #FLAG_INEXACT */ public void setIEEEFlagsBits(final int bits) { ieeeFlags |= bits & (FLAG_INVALID | FLAG_DIV_ZERO | FLAG_OVERFLOW | FLAG_UNDERFLOW | FLAG_INEXACT); } /** Makes a {@link Dfp} with a value of 0. * @return a new {@link Dfp} with a value of 0 */ public Dfp newDfp() { return new Dfp(this); } /** Create an instance from a byte value. * @param x value to convert to an instance * @return a new {@link Dfp} with the same value as x */ public Dfp newDfp(final byte x) { return new Dfp(this, x); } /** Create an instance from an int value. * @param x value to convert to an instance * @return a new {@link Dfp} with the same value as x */ public Dfp newDfp(final int x) { return new Dfp(this, x); } /** Create an instance from a long value. * @param x value to convert to an instance * @return a new {@link Dfp} with the same value as x */ public Dfp newDfp(final long x) { return new Dfp(this, x); } /** Create an instance from a double value. * @param x value to convert to an instance * @return a new {@link Dfp} with the same value as x */ public Dfp newDfp(final double x) { return new Dfp(this, x); } /** Copy constructor. * @param d instance to copy * @return a new {@link Dfp} with the same value as d */ public Dfp newDfp(Dfp d) { return new Dfp(d); } /** Create a {@link Dfp} given a String representation. * @param s string representation of the instance * @return a new {@link Dfp} parsed from specified string */ public Dfp newDfp(final String s) { return new Dfp(this, s); } /** Creates a {@link Dfp} with a non-finite value. * @param sign sign of the Dfp to create * @param nans code of the value, must be one of {@link Dfp#INFINITE}, * {@link Dfp#SNAN}, {@link Dfp#QNAN} * @return a new {@link Dfp} with a non-finite value */ public Dfp newDfp(final byte sign, final byte nans) { return new Dfp(this, sign, nans); } /** Get the constant 0. * @return a {@link Dfp} with value 0 */ public Dfp getZero() { return zero; } /** Get the constant 1. * @return a {@link Dfp} with value 1 */ public Dfp getOne() { return one; } /** {@inheritDoc} */ public Class<? extends FieldElement Other Java examples (source code examples)Here is a short list of links related to this Java DfpField.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.