Skip to content

Commit

Permalink
Merge pull request #259 from RoboticsTeam4904/is-zero-update
Browse files Browse the repository at this point in the history
Update docblocks and formatting from #235
  • Loading branch information
Daniel-E-B authored Jan 16, 2020
2 parents 9df8255 + 408fd05 commit ddc7702
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions Util.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package org.usfirst.frc4904.standard;


import edu.wpi.first.hal.util.BoundaryException;

/**
* Common utilities
*/
public class Util {
/**
* A function for determining if floating point numbers are effectively zero.
* Floating point arithmetic tends to introduce very small errors.
* Returns true if {@code value} is less than {@code epsilon}. This is useful
* for floating point numbers, whose arithmetic operations tend to introduce
* small errors.
*
* @param var
* The floating point number you want to compare
* @return
* Whether or not it is within Double.MIN_VALUE of zero
* @param value The floating point number to be compared
* @param epsilon The maximum magnitude of var such that it can be considered
* zero
* @return Whether or not {@code value} is less than {@code epsilon}
*/
public static boolean isZero(double var, double epsilon) {
return Math.abs(var) < epsilon;
public static boolean isZero(double value, double epsilon) {
return Math.abs(value) < epsilon;
}
public static boolean isZero(double var) {
return isZero(var, Math.sqrt(Math.ulp(1.0)));

/**
* Returns true if {@code value} is less than {@code epsilon}. This is useful
* for floating point numbers, whose arithmetic operations tend to introduce
* small errors.
*
* @param value The floating point number to be compared
* @return Whether or not {@code value} is effectively zero
*/
public static boolean isZero(double value) {
return isZero(value, Math.sqrt(Math.ulp(1.0)));
}

public static class Range {
Expand Down Expand Up @@ -56,23 +65,20 @@ public double getCenter() {
}

/**
* Scales a value (between -1 and 1) to the range.
* Example: (new Range(0,6)).scaleValue(0.5) == 4.5
* Scales a value (between -1 and 1) to the range. Example: (new
* Range(0,6)).scaleValue(0.5) == 4.5
*
* @param value
* between -1 and 1 (will be limited to [-1, 1])
* @param value between -1 and 1 (will be limited to [-1, 1])
* @return the scaled value
*/
public double scaleValue(double value) {
return limitValue(getCenter() + value * (getRange() / 2.0));
}

/**
* Limits a value to the range.
* Example: (new Range(0,6)).limitValue(7) == 6
* Limits a value to the range. Example: (new Range(0,6)).limitValue(7) == 6
*
* @param value
* the value to be limited
* @param value the value to be limited
* @return the limited value
*/
public double limitValue(double value) {
Expand Down

0 comments on commit ddc7702

Please sign in to comment.