Changeset 8132 in josm for trunk/src/com/drew/lang/Rational.java
- Timestamp:
- 2015-03-10T01:17:39+01:00 (11 years ago)
- File:
-
- 1 edited
-
trunk/src/com/drew/lang/Rational.java (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/drew/lang/Rational.java
r6127 r8132 1 1 /* 2 * Copyright 2002-201 2Drew Noakes2 * Copyright 2002-2015 Drew Noakes 3 3 * 4 4 * Licensed under the Apache License, Version 2.0 (the "License"); … … 16 16 * More information about this project is available at: 17 17 * 18 * http://drewnoakes.com/code/exif/ 19 * http ://code.google.com/p/metadata-extractor/18 * https://drewnoakes.com/code/exif/ 19 * https://github.com/drewnoakes/metadata-extractor 20 20 */ 21 21 … … 29 29 /** 30 30 * Immutable class for holding a rational number without loss of precision. Provides 31 * a familiar representation via toString() in form <code>numerator/denominator</code>. 32 * 33 * @author Drew Noakes http://drewnoakes.com 31 * a familiar representation via {@link Rational#toString} in form <code>numerator/denominator</code>. 32 * 33 * Note that any value with a numerator of zero will be treated as zero, even if the 34 * denominator is also zero. 35 * 36 * @author Drew Noakes https://drewnoakes.com 34 37 */ 35 38 public class Rational extends java.lang.Number implements Serializable … … 61 64 * to type <code>double</code>. 62 65 */ 66 @Override 63 67 public double doubleValue() 64 68 { 65 return (double) _numerator / (double) _denominator; 69 return _numerator == 0 70 ? 0.0 71 : (double) _numerator / (double) _denominator; 66 72 } 67 73 … … 73 79 * to type <code>float</code>. 74 80 */ 81 @Override 75 82 public float floatValue() 76 83 { 77 return (float) _numerator / (float) _denominator; 84 return _numerator == 0 85 ? 0.0f 86 : (float) _numerator / (float) _denominator; 78 87 } 79 88 … … 81 90 * Returns the value of the specified number as a <code>byte</code>. 82 91 * This may involve rounding or truncation. This implementation simply 83 * casts the result of <code>doubleValue()</code>to <code>byte</code>.92 * casts the result of {@link Rational#doubleValue} to <code>byte</code>. 84 93 * 85 94 * @return the numeric value represented by this object after conversion 86 95 * to type <code>byte</code>. 87 96 */ 97 @Override 88 98 public final byte byteValue() 89 99 { … … 94 104 * Returns the value of the specified number as an <code>int</code>. 95 105 * This may involve rounding or truncation. This implementation simply 96 * casts the result of <code>doubleValue()</code>to <code>int</code>.106 * casts the result of {@link Rational#doubleValue} to <code>int</code>. 97 107 * 98 108 * @return the numeric value represented by this object after conversion 99 109 * to type <code>int</code>. 100 110 */ 111 @Override 101 112 public final int intValue() 102 113 { … … 107 118 * Returns the value of the specified number as a <code>long</code>. 108 119 * This may involve rounding or truncation. This implementation simply 109 * casts the result of <code>doubleValue()</code>to <code>long</code>.120 * casts the result of {@link Rational#doubleValue} to <code>long</code>. 110 121 * 111 122 * @return the numeric value represented by this object after conversion 112 123 * to type <code>long</code>. 113 124 */ 125 @Override 114 126 public final long longValue() 115 127 { … … 120 132 * Returns the value of the specified number as a <code>short</code>. 121 133 * This may involve rounding or truncation. This implementation simply 122 * casts the result of <code>doubleValue()</code>to <code>short</code>.134 * casts the result of {@link Rational#doubleValue} to <code>short</code>. 123 135 * 124 136 * @return the numeric value represented by this object after conversion 125 137 * to type <code>short</code>. 126 138 */ 139 @Override 127 140 public final short shortValue() 128 141 { … … 154 167 } 155 168 156 /** Checks if this rationalnumber is an Integer, either positive or negative. */169 /** Checks if this {@link Rational} number is an Integer, either positive or negative. */ 157 170 public boolean isInteger() 158 171 { … … 167 180 * @return a string representation of the object. 168 181 */ 182 @Override 169 183 @NotNull 170 184 public String toString() … … 173 187 } 174 188 175 /** Returns the simplest representation of this Rational's value possible. */189 /** Returns the simplest representation of this {@link Rational}'s value possible. */ 176 190 @NotNull 177 191 public String toSimpleString(boolean allowDecimal) … … 211 225 212 226 /** 213 * Compares two <code>Rational</code>instances, returning true if they are mathematically227 * Compares two {@link Rational} instances, returning true if they are mathematically 214 228 * equivalent. 215 229 * 216 * @param obj the Rationalto compare this instance to.230 * @param obj the {@link Rational} to compare this instance to. 217 231 * @return true if instances are mathematically equivalent, otherwise false. Will also 218 * return false if <code>obj</code> is not an instance of <code>Rational</code>.232 * return false if <code>obj</code> is not an instance of {@link Rational}. 219 233 */ 220 234 @Override … … 235 249 /** 236 250 * <p> 237 * Simplifies the Rationalnumber.</p>251 * Simplifies the {@link Rational} number.</p> 238 252 * <p> 239 253 * Prime number series: 1, 2, 3, 5, 7, 9, 11, 13, 17</p> … … 244 258 * <p> 245 259 * However, generating the prime number series seems to be a hefty task. Perhaps 246 * it's simpler to check if both d & n are divisible by all numbers from 2 ->260 * it's simpler to check if both d & n are divisible by all numbers from 2 {@literal ->} 247 261 * (Math.min(denominator, numerator) / 2). In doing this, one can check for 2 248 262 * and 5 once, then ignore all even numbers, and all numbers ending in 0 or 5. … … 250 264 * <p> 251 265 * Therefore, the max number of pairs of modulus divisions required will be:</p> 252 * < code><pre>266 * <pre><code> 253 267 * 4 Math.min(denominator, numerator) - 1 254 268 * -- * ------------------------------------ + 2 255 269 * 10 2 256 * <p/>270 * 257 271 * Math.min(denominator, numerator) - 1 258 272 * = ------------------------------------ + 2 259 273 * 5 260 * </ pre></code>274 * </code></pre> 261 275 * 262 276 * @return a simplified instance, or if the Rational could not be simplified,
Note:
See TracChangeset
for help on using the changeset viewer.
