List of usage examples for java.lang Math IEEEremainder
public static double IEEEremainder(double f1, double f2)
From source file:Main.java
public static void main(String[] args) { double x = 123456.7; double y = -123.45; // get the remainder when x/y System.out.println("Math.IEEEremainder(" + x + "," + y + ")=" + Math.IEEEremainder(x, y)); // get the remainder when y/x System.out.println("Math.IEEEremainder(" + y + "," + x + ")=" + Math.IEEEremainder(y, x)); }
From source file:Main.java
public static float wrapAngle(float angle) { angle = (float) Math.IEEEremainder((double) angle, 6.2831854820251465d); if (angle <= -3.141593f) { angle += 6.283185f;/* w w w .j a v a 2 s .co m*/ return angle; } if (angle > 3.141593f) { angle -= 6.283185f; } return angle; }
From source file:Main.java
public static double getAbsAngle(double y, double x) { double angle = 0.0F; // atan2 is positive in the lower right quadrant and negative in upper right quadrant and measured // from the horizontal (positive x-axis) angle = Math.IEEEremainder(360 + Math.toDegrees(Math.atan2(y, x)), 360); return angle; }
From source file:edu.toronto.cs.phenotips.measurements.internal.DefaultMeasurementsChartConfigurationsFactory.java
/** * Validate that the configured grid is valid. * //from ww w . ja v a2s. c o m * @param settings the settings read from the configuration file * @return {@code true} if the settings are valid, {@code false} otherwise; encountered problems are logged */ private boolean validateGrid(SimpleMeasurementsChartConfiguration settings) { boolean isValid = true; if ((settings.upperAgeLimit - settings.lowerAgeLimit) % settings.ageTickStep != 0) { this.logger.warn("Invalid chart settings for [{}]: age grid lines don't fit evenly: [{}] in [{}-{}]", settings.chartIdentifier, settings.ageTickStep, settings.lowerAgeLimit, settings.upperAgeLimit); isValid = false; } if (settings.ageLabelStep % settings.ageTickStep != 0) { this.logger.warn("Invalid chart settings for [{}]: major/minor age grid lines don't match: [{}]/[{}]", settings.chartIdentifier, settings.ageLabelStep, settings.ageTickStep); isValid = false; } if (Math.abs(Math.IEEEremainder(settings.upperValueLimit - settings.lowerValueLimit, settings.valueTickStep)) > 1.0E-10) { this.logger.warn("Invalid chart settings for [{}]: value grid lines don't fit evenly: [{}] in [{}-{}]", settings.chartIdentifier, settings.valueTickStep, settings.lowerValueLimit, settings.upperValueLimit); isValid = false; } if (Math.abs(Math.IEEEremainder(settings.valueLabelStep, settings.valueTickStep)) > 1.0E-10) { this.logger.warn("Invalid chart settings for [{}]: major/minor value grid lines don't match: [{}]/[{}]", settings.chartIdentifier, settings.valueLabelStep, settings.valueTickStep); isValid = false; } return isValid; }
From source file:org.wrml.runtime.schema.PropertyProtoSlot.java
public void validateNewValue(final Model model, final Object newValue) throws PrototypeException { final String name = getName(); if (_DisallowedValues != null && _DisallowedValues.contains(newValue)) { throw new PrototypeException("The " + name + " value: " + newValue + " is disallowed.", null, getPrototype(), name);/*from w w w . java 2 s . c om*/ } final ValueType valueType = getValueType(); switch (valueType) { case Boolean: { break; } case Date: { break; } case Double: { if (newValue == null) { break; } if (_MaximumValue != null) { final boolean lessThanMax; if (isExclusiveMaximum()) { lessThanMax = (double) newValue < (double) _MaximumValue; } else { lessThanMax = (double) newValue <= (double) _MaximumValue; } if (!lessThanMax) { throw new PrototypeException("The " + name + " value: " + newValue + " is greater than the maximum allowed value: " + _MaximumValue, null, getPrototype(), name); } } if (_MinimumValue != null) { final boolean greaterThanMin; if (isExclusiveMinimum()) { greaterThanMin = (double) newValue > (double) _MinimumValue; } else { greaterThanMin = (double) newValue >= (double) _MinimumValue; } if (!greaterThanMin) { throw new PrototypeException("The " + name + " value: " + newValue + " is less than the minimum allowed value: " + _MinimumValue, null, getPrototype(), name); } } if (_DivisibleByValue != null) { final double remainder = Math.IEEEremainder((double) newValue, (double) _DivisibleByValue); final boolean divisbleBy = (remainder == 0.0); if (!divisbleBy) { throw new PrototypeException( "The " + name + " value: " + newValue + " is not divisible by: " + _DivisibleByValue, null, getPrototype(), name); } } break; } case Integer: { if (newValue == null) { break; } if (_MaximumValue != null) { final boolean lessThanMax; if (isExclusiveMaximum()) { lessThanMax = (int) newValue < (int) _MaximumValue; } else { lessThanMax = (int) newValue <= (int) _MaximumValue; } if (!lessThanMax) { throw new PrototypeException("The " + name + " value: " + newValue + " is greater than the maximum allowed value: " + _MaximumValue, null, getPrototype(), name); } } if (_MinimumValue != null) { final boolean greaterThanMin; if (isExclusiveMinimum()) { greaterThanMin = (int) newValue > (int) _MinimumValue; } else { greaterThanMin = (int) newValue >= (int) _MinimumValue; } if (!greaterThanMin) { throw new PrototypeException("The " + name + " value: " + newValue + " is less than the minimum allowed value: " + _MinimumValue, null, getPrototype(), name); } } if (_DivisibleByValue != null) { final boolean divisbleBy = ((int) newValue % (int) _DivisibleByValue) == 0; if (!divisbleBy) { throw new PrototypeException( "The " + name + " value: " + newValue + " is not divisible by: " + _DivisibleByValue, null, getPrototype(), name); } } break; } case Link: { break; } case List: { break; } case Long: { if (newValue == null) { break; } if (_MaximumValue != null) { final boolean lessThanMax; if (isExclusiveMaximum()) { lessThanMax = (long) newValue < (long) _MaximumValue; } else { lessThanMax = (long) newValue <= (long) _MaximumValue; } if (!lessThanMax) { throw new PrototypeException("The " + name + " value: " + newValue + " is greater than the maximum allowed value: " + _MaximumValue, null, getPrototype(), name); } } if (_MinimumValue != null) { final boolean greaterThanMin; if (isExclusiveMinimum()) { greaterThanMin = (long) newValue > (long) _MinimumValue; } else { greaterThanMin = (long) newValue >= (long) _MinimumValue; } if (!greaterThanMin) { throw new PrototypeException("The " + name + " value: " + newValue + " is less than the minimum allowed value: " + _MinimumValue, null, getPrototype(), name); } } if (_DivisibleByValue != null) { final boolean divisbleBy = ((long) newValue % (long) _DivisibleByValue) == 0L; if (!divisbleBy) { throw new PrototypeException( "The " + name + " value: " + newValue + " is not divisible by: " + _DivisibleByValue, null, getPrototype(), name); } } break; } case Model: { break; } case Native: { break; } case SingleSelect: { break; } case Text: { if (newValue == null) { break; } if (_MaximumLength != null) { final boolean lessThanMax = ((String) newValue).length() < _MaximumLength; if (!lessThanMax) { throw new PrototypeException("The " + name + " value: " + newValue + " is longer than the maximum allowed length: " + _MaximumLength, null, getPrototype(), name); } } if (_MinimumLength != null) { final boolean greaterThanMin = ((String) newValue).length() > _MinimumLength; if (!greaterThanMin) { throw new PrototypeException("The " + name + " value: " + newValue + " is shorter than the minimum allowed length: " + _MinimumLength, null, getPrototype(), name); } } break; } default: { break; } } }
From source file:com.jogden.spunkycharts.pricebyvolumechart.PriceByVolumeChartFragmentAdapter.java
private Thread _createPriceAxisAndMainPanel() { Thread thisThread = new Thread() { public void run() { /////////////////////////////////// /* START PREEMPTION LOGIC */ ///////////////////////////////// boolean noWait; final int MAX_PREEMPT_COUNT = ApplicationPreferences.getMaxPreemptCount(); if (!(noWait = _priceAxisLck.tryLock())) { synchronized (_priceAxisThrdMntr) { if (_priceAxisPrmptCnt++ > MAX_PREEMPT_COUNT) return; if (_priceAxisThrd != null) _priceAxisThrd.interrupt(); }/*from w w w . j a v a2s .com*/ _priceAxisPrmptStck.offer(this); _priceAxisLck.lock(); } try { /* everything that follows should assume we own monitor */ if (!noWait) { if (this != _priceAxisPrmptStck.peekLast()) return; else { /* don't assume stack hasn't grown since the peek */ Iterator<Thread> dIter = _priceAxisPrmptStck.iterator(); do { _priceAxisPrmptStck.poll(); } while (dIter.next() != this); } } synchronized (_priceAxisThrdMntr) { _priceAxisThrd = this; } ///////////////////////////////// /* END PREEMPTION LOGIC */ ////////////////////////////// float rangeDiff = _highPrice - _lowPrice; /* deal with problematic high/low parameters */ if (rangeDiff <= 0 || _highPrice < 0 || _lowPrice < 0) if (rangeDiff == 0 && _highPrice > 0) { _highPrice *= 1.001; _lowPrice *= .999; rangeDiff = _highPrice - _lowPrice; } else throw new IllegalStateException("Invalid high and/or low price in the ChartAdapter"); /* if we haven't calculated the height of a price-label view */ if (_priceElemHght == 0) {/* can cached value doesn't go stale? */ final YAxisPriceLabel tv1 = (YAxisPriceLabel) _inflater.inflate(R.layout.y_axis_price_label, null); tv1.setText("X"); tv1.setTextSize(_axisFontSz); tv1.setVisibility(View.INVISIBLE); final ConditionVariable cond = new ConditionVariable(); _guiThrdHndlr.post(new Runnable() { public void run() { _priceAxis.removeAllViews(); _priceAxis.addView(tv1); cond.open(); } }); cond.block(); cond.close(); YAxisPriceLabel tv1b = (YAxisPriceLabel) _priceAxis.getChildAt(0); /* make sure a valid priceElemHeightt, or quit entirely */ /* just spin, a new thread preempts us anyway */ while ((_priceElemHght = tv1b.getHeight()) == 0) Thread.sleep(_axisTimeoutIncr); } _guiThrdHndlr.post(new Runnable() { public void run() { _priceAxis.removeAllViews(); } }); int totalHeight; /* make sure a valid totalHeight, or quit entirely */ /* just spin, a new thread preempts us anyway */ while ((totalHeight = _priceAxis.getHeight()) == 0 || totalHeight > _myContainer.getHeight()) Thread.sleep(_axisTimeoutIncr); float[] incrVals = new float[2]; try { int maxNodes = (int) (totalHeight / _priceElemHght); if (rangeDiff < 0 || maxNodes < 0) throw new PriceByVolumeChartLogicError("rangeDiff and maxNodes can't be negative."); incrVals = /* call down to our native sub to find increment values */ MainApplication.IncrementRegressNative(rangeDiff, maxNodes); if (incrVals[0] < 0 || incrVals[1] <= 0) throw new PriceByVolumeChartLogicError("IncrementRegressNative() sub-routine aborted. " + "retVals[0]: " + incrVals[0] + "retVals[1]: " + incrVals[1] + "adjRangeDiff: " + rangeDiff + "maxNodes" + maxNodes); _segCount = (int) incrVals[1]; _segSize = incrVals[0]; } catch (PriceByVolumeChartLogicError e) { Log.e("PriceByVolumeChartLogicError", e.getMessage()); return; /* just leave the axis empty*/ } /* adjust height to new increment values */ final int adjPriceElemHeight = (int) (totalHeight / _segCount); /* we'll need to account for any unused spaced in the axis */ final int vacantHeight = totalHeight - (int) (adjPriceElemHeight * _segCount); double distFromIncr = Math.IEEEremainder((double) _highPrice, (double) _segSize); /* distance from rounded incr value */ double adjTopNodeVal = (double) _highPrice - distFromIncr; if (distFromIncr > 0) /* be sure to round to the upper incr */ adjTopNodeVal += _segSize; DecimalFormat df = new DecimalFormat("0.00"); double lastNodeVal = adjTopNodeVal; int count = 0; do { /* loop through the increments */ final YAxisPriceLabel tv = (YAxisPriceLabel) _inflater.inflate(R.layout.y_axis_price_label, null); tv.setTextSize(_axisFontSz); tv.setText(df.format(lastNodeVal)); tv.setTextColor(_axisFontColor); tv.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 0, 1)); _guiThrdHndlr.post(new Runnable() { public void run() { _priceAxis.addView(tv); } }); /* for the loop-contingent side-effect */ } while (++count < (int) _segCount && (lastNodeVal -= _segSize) > 0); final float halfVacantHeight = vacantHeight / 2; _mainSgmnt.setVerticalBuffers((int) halfVacantHeight, (int) halfVacantHeight); _guiThrdHndlr.post(new Runnable() { public void run() { _priceAxis.setPadding( /* center price-views */ 0, (int) halfVacantHeight, 0, (int) halfVacantHeight); _priceAxis.setClipChildren(false); _priceAxis.setClipToPadding(false); _priceAxis.invalidate(); } }); synchronized (_priceAxisThrdMntr) { _priceAxisPrmptCnt = 0; } } catch (InterruptedException e) { } catch (IllegalStateException e) { Log.d("IllegalState(High, Low): ", "IllegalStateException caught in _createPriceAxisAndMainPanelY: " + Float.toString(_highPrice) + " - " + Float.toString(_lowPrice)); } catch (RuntimeException e) { e.printStackTrace(); throw e; } finally { synchronized (_priceAxisThrdMntr) { _priceAxisThrd = null; } _activeThreads.remove(this); _priceAxisLck.unlock(); } } }; _activeThreads.add(thisThread); thisThread.start(); return thisThread; }
From source file:com.jogden.spunkycharts.traditionalchart.TraditionalChartFragmentAdapter.java
private void _createPriceAxisY() { Thread thisThread = new Thread() { public void run() { /////////////////////////////////// /* START PREEMPTION LOGIC */ ///////////////////////////////// boolean noWait; final int MAX_PREEMPT_COUNT = ApplicationPreferences.getMaxPreemptCount(); if (!(noWait = _priceAxisLck.tryLock())) { synchronized (_priceAxisThrdMntr) { if (_priceAxisPrmptCnt++ > MAX_PREEMPT_COUNT) return; if (_priceAxisThrd != null) _priceAxisThrd.interrupt(); }/*w ww .j ava 2 s .c o m*/ _priceAxisPrmptStck.offer(this); _priceAxisLck.lock(); } try { /* everything that follows should assume we own monitor */ if (!noWait) { if (this != _priceAxisPrmptStck.peekLast()) return; else { /* don't assume stack hasn't grown since the peek */ Iterator<Thread> dIter = _priceAxisPrmptStck.iterator(); do { _priceAxisPrmptStck.poll(); } while (dIter.next() != this); } } synchronized (_priceAxisThrdMntr) { _priceAxisThrd = this; } ///////////////////////////////// /* END PREEMPTION LOGIC */ /////////////////////////////// _updateLtch.await(); /* wait for historic data to be inserted */ float rangeDiff = _highPriceDsply - _lowPriceDsply; /* deal with problematic high/low parameters */ if (rangeDiff <= 0 || _highPriceDsply < 0 || _lowPriceDsply < 0) if (rangeDiff == 0 && _highPriceDsply > 0) { _highPriceDsply *= 1.001; _lowPriceDsply *= .999; rangeDiff = _highPriceDsply - _lowPriceDsply; _priceSgmnt.setYRange(_highPriceDsply, _lowPriceDsply, _highPrice, _lowPrice); } else throw new IllegalStateException("Invalid high and/or low price in the ChartAdapter"); /* if we haven't calculated the height of a price-label view */ if (_priceElemHght == 0) {/* can cached value doesn't go stale? */ final YAxisPriceLabel tv1 = (YAxisPriceLabel) _inflater.inflate(R.layout.y_axis_price_label, null); tv1.setText("X"); tv1.setTextSize(_axisFontSz); tv1.setVisibility(View.INVISIBLE); final ConditionVariable cond = new ConditionVariable(); _guiThrdHndlr.post(new Runnable() { public void run() { _priceAxis.removeAllViews(); _priceAxis.addView(tv1); cond.open(); } }); cond.block(); cond.close(); YAxisPriceLabel tv1b = (YAxisPriceLabel) _priceAxis.getChildAt(0); /* make sure a valid priceElemHeightt, or quit entirely */ /* just spin, a new thread preempts us anyway */ while ((_priceElemHght = tv1b.getHeight()) == 0) Thread.sleep(_axisTimeoutIncr); } _guiThrdHndlr.post(new Runnable() { public void run() { _priceAxis.removeAllViews(); } }); int totalHeight; /* make sure a valid totalHeight, or quit entirely */ /* just spin, a new thread preempts us anyway */ while ((totalHeight = _priceAxis.getHeight()) == 0 || totalHeight > _myContainer.getHeight()) Thread.sleep(_axisTimeoutIncr); float[] incrVals = new float[2]; try { int maxNodes = (int) (totalHeight / _priceElemHght); if (rangeDiff < 0 || maxNodes < 0) throw new TraditionalChartLogicError("rangeDiff and maxNodes can't be negative."); /* call down to our native sub to find increment values */ incrVals = MainApplication.IncrementRegressNative(rangeDiff, maxNodes); if (incrVals[0] < 0 || incrVals[1] <= 0) throw new TraditionalChartLogicError("IncrementRegressNative() sub-routine aborted. " + "retVals[0]: " + incrVals[0] + "retVals[1]: " + incrVals[1] + "adjRangeDiff: " + rangeDiff + "maxNodes" + maxNodes); } catch (TraditionalChartLogicError e) { Log.e("TraditionalChartLogicError", e.getMessage()); return; /* just leave the axis empty*/ } /* adjust height to new increment values */ final int adjPriceElemHeight = (int) (totalHeight / (int) incrVals[1]); /* we'll need to account for any unused space in the axis */ final int vacantHeight = totalHeight - (int) (adjPriceElemHeight * (int) incrVals[1]); double distFromIncr = Math.IEEEremainder((double) _highPriceDsply, (double) incrVals[0]); /* distance from rounded incr */ double adjTopNodeVal = (double) _highPriceDsply - distFromIncr; if (distFromIncr > 0) /* be sure to round to the upper incr */ adjTopNodeVal += incrVals[0]; DecimalFormat df = new DecimalFormat("0.00"); double lastNodeVal = adjTopNodeVal; int count = 0; do { /* loop through the increments */ final YAxisPriceLabel tv = (YAxisPriceLabel) _inflater.inflate(R.layout.y_axis_price_label, null); tv.setTextSize(_axisFontSz); tv.setText(df.format(lastNodeVal)); tv.setTextColor(_axisFontColor); tv.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 0, 1)); _guiThrdHndlr.post(new Runnable() { public void run() { _priceAxis.addView(tv); } }); /* for the loop-contingent side-effect */ } while (++count < (int) incrVals[1] && (lastNodeVal -= incrVals[0]) > 0); /* values for padding the axis and adjusting the segments */ final double adjTopNodeOffset = ((adjTopNodeVal - _highPriceDsply) / rangeDiff) * totalHeight; final double adjBottomNodeOffset = ((_lowPriceDsply - lastNodeVal) / rangeDiff) * totalHeight; final float halfVacantHeight = vacantHeight / 2; final float halfPriceElemHeight = adjPriceElemHeight / 2; final int topBuf = (int) (adjTopNodeOffset + halfVacantHeight + halfPriceElemHeight); final int bottomBuf = (int) (adjBottomNodeOffset + halfVacantHeight + halfPriceElemHeight); /* adjust price segments so they align w/ axis vals */ _priceSgmnt.setVerticalBuffers(topBuf, bottomBuf); _guiThrdHndlr.post(new Runnable() { public void run() { _priceAxis.setPadding( /* center price-views */ 0, (int) halfVacantHeight, 0, (int) halfVacantHeight); _priceAxis.setClipChildren(false); _priceAxis.setClipToPadding(false); _priceAxis.invalidate(); _priceSgmnt.forceDraw(); } }); synchronized (_priceAxisThrdMntr) { _priceAxisPrmptCnt = 0; } } catch (InterruptedException e) { } catch (IllegalStateException e) { Log.d("IllegalState(High, Low): ", "IllegalStateException caught in _createPriceAxisY: " + Float.toString(_highPriceDsply) + " - " + Float.toString(_lowPriceDsply)); } catch (RuntimeException e) { e.printStackTrace(); throw e; } finally { synchronized (_priceAxisThrdMntr) { _priceAxisThrd = null; } _activeThreads.remove(this); _priceAxisLck.unlock(); } } }; _activeThreads.add(thisThread); thisThread.start(); }