/** * Try to remove at least {@code n} fraction digits in the string representation of * the specified value. This method try small changes to {@code value}, by adding or * substracting a maximum of 4 ulps. If there is no small change that remove at least * {@code n} fraction digits, then the value is returned unchanged. This method is * used for hiding rounding errors, like in conversions from radians to degrees. * * <P>Example: {@code XMath.fixRoundingError(-61.500000000000014, 12)} returns * {@code -61.5}. * * @param value The value to fix. * @param n The minimum amount of fraction digits. * @return The fixed value, or the unchanged {@code value} if there is no small change * that remove at least {@code n} fraction digits. */ public static double fixRoundingError(final double value, int n) { double lower = value; double upper = value; n = countFractionDigits(value) - n; if (n > 0) { for (int i=0; i<4; i++) { if (countFractionDigits(lower = previous(lower)) <= n) return lower; if (countFractionDigits(upper = next (upper)) <= n) return upper; } } return value; }
public double getPositionFromValue(final double _v) { return XMath.log10(_v) * scale_ + offset_; }
if (amount<0) { do { value = previous(value); } while (++amount != 0); } else if (amount!=0) { do { value = next(value); } while (--amount != 0); if (amount<0) { do { vf = previous(vf); } while (++amount != 0); } else if (amount!=0) { do { vf = next(vf); } while (--amount != 0); if (isInteger(type)) { return value + amount;
/** * Round the specified value, providing that the difference between the original value and * the rounded value is not greater than the specified amount of floating point units. This * method can be used for hiding floating point error likes 2.9999999996. * * @param value The value to round. * @param flu The amount of floating point units. * @return The rounded value, of {@code value} if it was not close enough to an integer. */ public static double round(final double value, int flu) { final double target = Math.rint(value); if (value != target) { final boolean pos = (value < target); double candidate = value; while (--flu >= 0) { candidate = pos ? next(candidate) : previous(candidate); if (candidate == target) { return target; } } } return value; }
/** * Finds the least float greater than <var>f</var>. * If {@code NaN}, returns same value. * * @todo Remove this method when we will be allowed to use J2SE 1.5. */ public static float next(final float f) { return next(f, true); }
/** * Finds the greatest float less than <var>f</var>. * If {@code NaN}, returns same value. * * @todo Remove this method when we will be allowed to use J2SE 1.5. */ public static float previous(final float f) { return next(f, false); }
public double getValueFromPosition(final double _v) { if (scale_ == 0) { return _v; } return XMath.pow10((_v - offset_) / scale_); }
/** * Initialise l'itrateur. * * @param _minimum Valeur minimale de la premire graduation. * @param _maximum Valeur limite des graduations. La dernire graduation n'aura pas ncessairement cette valeur. * @param _visualLength Longueur visuelle de l'axe sur laquelle tracer la graduation. Cette longueur doit tre * exprime en pixels ou en points. * @param _visualTickSpacing Espace laisser visuellement entre deux marques de graduation. Cet espace doit tre * exprim en pixels ou en points (1/72 de pouce). */ protected void init(final double _minimum, final double _maximum, final float _visualLength, final float _visualTickSpacing) { double mini = _minimum; if (_minimum == 0) { mini = 1E-15; } final double logMin = XMath.log10(mini); final double logMax = XMath.log10(_maximum); super.init(logMin, logMax, _visualLength, _visualTickSpacing); scale_ = (_maximum - mini) / (logMax - logMin); offset_ = mini - scale_ * logMin; }
/** * Compute 10 power <var>x</var>. */ public static double pow10(final double x) { final int ix = (int) x; if (ix == x) { return pow10(ix); } else { return Math.pow(10, x); } }
public String formatValue(final double _v) { if (!isValid()) { currentLabel(); } int log10 = (int) Math.floor(XMath.log10(_v)); if (log10 < 0) { log10 = -log10; final int oldr = format_.getMaximumFractionDigits(); if (oldr < log10) { format_.setMaximumFractionDigits(log10); final String r = format_.format(_v); format_.setMaximumFractionDigits(oldr); return r; } } return super.formatValue(_v); }
/** * Retourne la valeur de la graduation courante. Cette mthode peut tre appele pour une graduation majeure ou * mineure. */ public double currentValue() { return XMath.pow10(super.currentValue()); } }
public String formatSubValue(final double _v) { if (!isValid()) { currentLabel(); } if (subFormat_ == null) { buildSubFormat(); } int log10 = (int) Math.floor(XMath.log10(_v)); if (log10 < 0) { log10 = -log10 + 1; final int oldr = subFormat_.getMaximumFractionDigits(); if (oldr < log10) { subFormat_.setMaximumFractionDigits(log10); final String r = subFormat_.format(_v); subFormat_.setMaximumFractionDigits(oldr); return r; } } return super.formatValue(_v); }