001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.units;
006
007import static edu.wpi.first.units.Units.Value;
008
009import edu.wpi.first.units.measure.Acceleration;
010import edu.wpi.first.units.measure.Angle;
011import edu.wpi.first.units.measure.AngularAcceleration;
012import edu.wpi.first.units.measure.AngularMomentum;
013import edu.wpi.first.units.measure.AngularVelocity;
014import edu.wpi.first.units.measure.Current;
015import edu.wpi.first.units.measure.Dimensionless;
016import edu.wpi.first.units.measure.Distance;
017import edu.wpi.first.units.measure.Energy;
018import edu.wpi.first.units.measure.Force;
019import edu.wpi.first.units.measure.Frequency;
020import edu.wpi.first.units.measure.LinearAcceleration;
021import edu.wpi.first.units.measure.LinearMomentum;
022import edu.wpi.first.units.measure.LinearVelocity;
023import edu.wpi.first.units.measure.Mass;
024import edu.wpi.first.units.measure.MomentOfInertia;
025import edu.wpi.first.units.measure.Mult;
026import edu.wpi.first.units.measure.Per;
027import edu.wpi.first.units.measure.Power;
028import edu.wpi.first.units.measure.Resistance;
029import edu.wpi.first.units.measure.Temperature;
030import edu.wpi.first.units.measure.Time;
031import edu.wpi.first.units.measure.Torque;
032import edu.wpi.first.units.measure.Velocity;
033import edu.wpi.first.units.measure.Voltage;
034
035/**
036 * A measure holds the magnitude and unit of some dimension, such as distance, time, or speed. Two
037 * measures with the same <i>unit</i> and <i>magnitude</i> are effectively equivalent objects.
038 *
039 * @param <U> the unit type of the measure
040 */
041public interface Measure<U extends Unit> extends Comparable<Measure<U>> {
042  /**
043   * The threshold for two measures to be considered equivalent if converted to the same unit. This
044   * is only needed due to floating-point error.
045   */
046  double EQUIVALENCE_THRESHOLD = 1e-12;
047
048  /**
049   * Gets the unitless magnitude of this measure.
050   *
051   * @return the magnitude in terms of {@link #unit() the unit}.
052   */
053  double magnitude();
054
055  /**
056   * Gets the magnitude of this measure in terms of the base unit. If the unit is the base unit for
057   * its system of measure, then the value will be equivalent to {@link #magnitude()}.
058   *
059   * @return the magnitude in terms of the base unit
060   */
061  double baseUnitMagnitude();
062
063  /**
064   * Gets the units of this measure.
065   *
066   * @return the unit
067   */
068  U unit();
069
070  /**
071   * Converts this measure to a measure with a different unit of the same type, eg minutes to
072   * seconds. Converting to the same unit is equivalent to calling {@link #magnitude()}.
073   *
074   * <pre>
075   *   Meters.of(12).in(Feet) // 39.3701
076   *   Seconds.of(15).in(Minutes) // 0.25
077   * </pre>
078   *
079   * @param unit the unit to convert this measure to
080   * @return the value of this measure in the given unit
081   */
082  default double in(U unit) {
083    if (this.unit().equals(unit)) {
084      return magnitude();
085    } else {
086      return unit.fromBaseUnits(baseUnitMagnitude());
087    }
088  }
089
090  /**
091   * A convenience method to get the base unit of the measurement. Equivalent to {@code
092   * unit().getBaseUnit()}.
093   *
094   * @return the base unit of measure.
095   */
096  @SuppressWarnings("unchecked")
097  default U baseUnit() {
098    return (U) unit().getBaseUnit();
099  }
100
101  /**
102   * Absolute value of measure.
103   *
104   * @param unit unit to use
105   * @return the absolute value of this measure in the given unit
106   */
107  default double abs(U unit) {
108    return Math.abs(this.in(unit));
109  }
110
111  /**
112   * Take the sign of another measure.
113   *
114   * @param other measure from which to take sign
115   * @param unit unit to use
116   * @return the value of the measure in the given unit with the sign of the provided measure
117   */
118  default double copySign(Measure<U> other, U unit) {
119    return Math.copySign(this.in(unit), other.in(unit));
120  }
121
122  /**
123   * Returns an immutable copy of this measure. The copy can be used freely and is guaranteed never
124   * to change.
125   *
126   * @return the copied measure
127   */
128  Measure<U> copy();
129
130  /**
131   * Returns a mutable copy of this measure. It will be initialized to the current state of this
132   * measure, but can be changed over time without needing to allocate new measurement objects.
133   *
134   * @return the copied measure
135   */
136  MutableMeasure<U, ?, ?> mutableCopy();
137
138  /**
139   * Returns a measure equivalent to this one equal to zero minus its current value. For non-linear
140   * unit types like temperature, the zero point is treated as the zero value of the base unit (eg
141   * Kelvin). In effect, this means code like {@code Celsius.of(10).unaryMinus()} returns a value
142   * equivalent to -10 Kelvin, and <i>not</i> -10° Celsius.
143   *
144   * @return a measure equal to zero minus this measure
145   */
146  Measure<U> unaryMinus();
147
148  /**
149   * Returns a measure equivalent to this one equal to zero minus its current value. For non-linear
150   * unit types like temperature, the zero point is treated as the zero value of the base unit (eg
151   * Kelvin). In effect, this means code like {@code Celsius.of(10).negate()} returns a value
152   * equivalent to -10 Kelvin, and <i>not</i> -10° Celsius.
153   *
154   * @return a measure equal to zero minus this measure
155   * @deprecated use unaryMinus() instead. This was renamed for consistency with other WPILib
156   *     classes like Rotation2d
157   */
158  @Deprecated(since = "2025", forRemoval = true)
159  default Measure<U> negate() {
160    return unaryMinus();
161  }
162
163  /**
164   * Adds another measure of the same unit type to this one.
165   *
166   * @param other the measurement to add
167   * @return a measure of the sum of both measures
168   */
169  Measure<U> plus(Measure<? extends U> other);
170
171  /**
172   * Subtracts another measure of the same unit type from this one.
173   *
174   * @param other the measurement to subtract
175   * @return a measure of the difference between the measures
176   */
177  Measure<U> minus(Measure<? extends U> other);
178
179  /**
180   * Multiplies this measure by a scalar unitless multiplier.
181   *
182   * @param multiplier the scalar multiplication factor
183   * @return the scaled result
184   */
185  Measure<U> times(double multiplier);
186
187  /**
188   * Multiplies this measure by a scalar dimensionless multiplier.
189   *
190   * @param multiplier the scalar multiplication factor
191   * @return the scaled result
192   */
193  Measure<U> times(Dimensionless multiplier);
194
195  /**
196   * Generates a new measure that is equal to this measure multiplied by another. Some dimensional
197   * analysis is performed to reduce the units down somewhat; for example, multiplying a {@code
198   * Measure<Time>} by a {@code Measure<Velocity<Distance>>} will return just a {@code
199   * Measure<Distance>} instead of the naive {@code Measure<Mult<Time, Velocity<Distance>>}. This is
200   * not guaranteed to perform perfect dimensional analysis.
201   *
202   * @param multiplier the unit to multiply by
203   * @return the multiplicative unit
204   */
205  default Measure<?> times(Measure<?> multiplier) {
206    final double baseUnitResult = baseUnitMagnitude() * multiplier.baseUnitMagnitude();
207
208    // First try to eliminate any common units
209    if (unit() instanceof PerUnit<?, ?> ratio
210        && multiplier.baseUnit().equals(ratio.denominator().getBaseUnit())) {
211      // Per<N, D> * D -> yield N
212      // Case 1: denominator of the Per cancels out, return with just the units of the numerator
213      return ratio.numerator().ofBaseUnits(baseUnitResult);
214    } else if (multiplier.baseUnit() instanceof PerUnit<?, ?> ratio
215        && baseUnit().equals(ratio.denominator().getBaseUnit())) {
216      // D * Per<N, D) -> yield N
217      // Case 2: Same as Case 1, just flipped between this and the multiplier
218      return ratio.numerator().ofBaseUnits(baseUnitResult);
219    } else if (unit() instanceof PerUnit<?, ?> per
220        && multiplier.unit() instanceof PerUnit<?, ?> otherPer
221        && per.denominator().getBaseUnit().equals(otherPer.numerator().getBaseUnit())
222        && per.numerator().getBaseUnit().equals(otherPer.denominator().getBaseUnit())) {
223      // multiplying eg meters per second * milliseconds per foot
224      // return a scalar
225      return Value.of(baseUnitResult);
226    }
227
228    // No common units to eliminate, is one of them dimensionless?
229    // Note that this must come *after* the multiplier cases, otherwise
230    // Per<U, Dimensionless> * Dimensionless will not return a U
231    if (multiplier.unit() instanceof DimensionlessUnit) {
232      // scalar multiplication of this
233      return times(multiplier.baseUnitMagnitude());
234    } else if (unit() instanceof DimensionlessUnit) {
235      // scalar multiplication of multiplier
236      return multiplier.times(baseUnitMagnitude());
237    }
238
239    if (multiplier instanceof Acceleration<?> acceleration) {
240      return times(acceleration);
241    } else if (multiplier instanceof Angle angle) {
242      return times(angle);
243    } else if (multiplier instanceof AngularAcceleration angularAcceleration) {
244      return times(angularAcceleration);
245    } else if (multiplier instanceof AngularMomentum angularMomentum) {
246      return times(angularMomentum);
247    } else if (multiplier instanceof AngularVelocity angularVelocity) {
248      return times(angularVelocity);
249    } else if (multiplier instanceof Current current) {
250      return times(current);
251    } else if (multiplier instanceof Dimensionless dimensionless) {
252      // n.b. this case should already be covered
253      return times(dimensionless);
254    } else if (multiplier instanceof Distance distance) {
255      return times(distance);
256    } else if (multiplier instanceof Energy energy) {
257      return times(energy);
258    } else if (multiplier instanceof Force force) {
259      return times(force);
260    } else if (multiplier instanceof Frequency frequency) {
261      return times(frequency);
262    } else if (multiplier instanceof LinearAcceleration linearAcceleration) {
263      return times(linearAcceleration);
264    } else if (multiplier instanceof LinearVelocity linearVelocity) {
265      return times(linearVelocity);
266    } else if (multiplier instanceof Mass mass) {
267      return times(mass);
268    } else if (multiplier instanceof MomentOfInertia momentOfInertia) {
269      return times(momentOfInertia);
270    } else if (multiplier instanceof Mult<?, ?> mult) {
271      return times(mult);
272    } else if (multiplier instanceof Per<?, ?> per) {
273      return times(per);
274    } else if (multiplier instanceof Power power) {
275      return times(power);
276    } else if (multiplier instanceof Temperature temperature) {
277      return times(temperature);
278    } else if (multiplier instanceof Time time) {
279      return times(time);
280    } else if (multiplier instanceof Torque torque) {
281      return times(torque);
282    } else if (multiplier instanceof Velocity<?> velocity) {
283      return times(velocity);
284    } else if (multiplier instanceof Voltage voltage) {
285      return times(voltage);
286    } else if (multiplier instanceof Resistance resistance) {
287      return times(resistance);
288    } else {
289      // Dimensional analysis fallthrough or a generic input measure type
290      // Do a basic unit multiplication
291      return MultUnit.combine(unit(), multiplier.unit()).ofBaseUnits(baseUnitResult);
292    }
293  }
294
295  /**
296   * Multiplies this measure by an acceleration and returns the resulting measure in the most
297   * appropriate unit.
298   *
299   * @param multiplier the measurement to multiply by.
300   * @return the multiplication result
301   */
302  default Measure<?> times(Acceleration<?> multiplier) {
303    return MultUnit.combine(unit(), multiplier.unit())
304        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
305  }
306
307  /**
308   * Multiplies this measure by an angle and returns the resulting measure in the most appropriate
309   * unit.
310   *
311   * @param multiplier the measurement to multiply by.
312   * @return the multiplication result
313   */
314  default Measure<?> times(Angle multiplier) {
315    return MultUnit.combine(unit(), multiplier.unit())
316        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
317  }
318
319  /**
320   * Multiplies this measure by an angular acceleration and returns the resulting measure in the
321   * most appropriate unit.
322   *
323   * @param multiplier the measurement to multiply by.
324   * @return the multiplication result
325   */
326  default Measure<?> times(AngularAcceleration multiplier) {
327    return MultUnit.combine(unit(), multiplier.unit())
328        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
329  }
330
331  /**
332   * Multiplies this measure by an angular momentum and returns the resulting measure in the most
333   * appropriate unit.
334   *
335   * @param multiplier the measurement to multiply by.
336   * @return the multiplication result
337   */
338  default Measure<?> times(AngularMomentum multiplier) {
339    return MultUnit.combine(unit(), multiplier.unit())
340        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
341  }
342
343  /**
344   * Multiplies this measure by an angular velocity and returns the resulting measure in the most
345   * appropriate unit.
346   *
347   * @param multiplier the measurement to multiply by.
348   * @return the multiplication result
349   */
350  default Measure<?> times(AngularVelocity multiplier) {
351    return MultUnit.combine(unit(), multiplier.unit())
352        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
353  }
354
355  /**
356   * Multiplies this measure by an electric current and returns the resulting measure in the most
357   * appropriate unit.
358   *
359   * @param multiplier the measurement to multiply by.
360   * @return the multiplication result
361   */
362  default Measure<?> times(Current multiplier) {
363    return MultUnit.combine(unit(), multiplier.unit())
364        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
365  }
366
367  /**
368   * Multiplies this measure by a distance and returns the resulting measure in the most appropriate
369   * unit.
370   *
371   * @param multiplier the measurement to multiply by.
372   * @return the multiplication result
373   */
374  default Measure<?> times(Distance multiplier) {
375    return MultUnit.combine(unit(), multiplier.unit())
376        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
377  }
378
379  /**
380   * Multiplies this measure by an energy and returns the resulting measure in the most appropriate
381   * unit.
382   *
383   * @param multiplier the measurement to multiply by.
384   * @return the multiplication result
385   */
386  default Measure<?> times(Energy multiplier) {
387    return MultUnit.combine(unit(), multiplier.unit())
388        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
389  }
390
391  /**
392   * Multiplies this measure by a force and returns the resulting measure in the most appropriate
393   * unit.
394   *
395   * @param multiplier the measurement to multiply by.
396   * @return the multiplication result
397   */
398  default Measure<?> times(Force multiplier) {
399    return MultUnit.combine(unit(), multiplier.unit())
400        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
401  }
402
403  /**
404   * Multiplies this measure by a frequency and returns the resulting measure in the most
405   * appropriate unit. This often - but not always - means implementations return a variation of a
406   * {@link Per} measure.
407   *
408   * @param multiplier the measurement to multiply by.
409   * @return the multiplication result
410   */
411  default Measure<?> times(Frequency multiplier) {
412    return MultUnit.combine(unit(), multiplier.unit())
413        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
414  }
415
416  /**
417   * Multiplies this measure by a linear acceleration and returns the resulting measure in the most
418   * appropriate unit.
419   *
420   * @param multiplier the measurement to multiply by.
421   * @return the multiplication result
422   */
423  default Measure<?> times(LinearAcceleration multiplier) {
424    return MultUnit.combine(unit(), multiplier.unit())
425        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
426  }
427
428  /**
429   * Multiplies this measure by a linear momentum and returns the resulting measure in the most
430   * appropriate unit.
431   *
432   * @param multiplier the measurement to multiply by.
433   * @return the multiplication result
434   */
435  default Measure<?> times(LinearMomentum multiplier) {
436    return MultUnit.combine(unit(), multiplier.unit())
437        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
438  }
439
440  /**
441   * Multiplies this measure by a linear velocity and returns the resulting measure in the most
442   * appropriate unit.
443   *
444   * @param multiplier the measurement to multiply by.
445   * @return the multiplication result
446   */
447  default Measure<?> times(LinearVelocity multiplier) {
448    return MultUnit.combine(unit(), multiplier.unit())
449        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
450  }
451
452  /**
453   * Multiplies this measure by a mass and returns the resulting measure in the most appropriate
454   * unit.
455   *
456   * @param multiplier the measurement to multiply by.
457   * @return the multiplication result
458   */
459  default Measure<?> times(Mass multiplier) {
460    return MultUnit.combine(unit(), multiplier.unit())
461        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
462  }
463
464  /**
465   * Multiplies this measure by a moment of intertia and returns the resulting measure in the most
466   * appropriate unit.
467   *
468   * @param multiplier the measurement to multiply by.
469   * @return the multiplication result
470   */
471  default Measure<?> times(MomentOfInertia multiplier) {
472    return MultUnit.combine(unit(), multiplier.unit())
473        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
474  }
475
476  /**
477   * Multiplies this measure by a generic multiplied measure and returns the resulting measure in
478   * the most appropriate unit.
479   *
480   * @param multiplier the measurement to multiply by.
481   * @return the multiplication result
482   */
483  default Measure<?> times(Mult<?, ?> multiplier) {
484    return MultUnit.combine(unit(), multiplier.unit())
485        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
486  }
487
488  /**
489   * Multiplies this measure by a power and returns the resulting measure in the most appropriate
490   * unit.
491   *
492   * @param multiplier the measurement to multiply by.
493   * @return the multiplication result
494   */
495  default Measure<?> times(Power multiplier) {
496    return MultUnit.combine(unit(), multiplier.unit())
497        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
498  }
499
500  /**
501   * Multiplies this measure by a generic ratio measurement and returns the resulting measure in the
502   * most appropriate unit.
503   *
504   * @param multiplier the measurement to multiply by.
505   * @return the multiplication result
506   */
507  default Measure<?> times(Per<?, ?> multiplier) {
508    return MultUnit.combine(unit(), multiplier.unit())
509        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
510  }
511
512  /**
513   * Multiplies this measure by a temperature and returns the resulting measure in the most
514   * appropriate unit.
515   *
516   * @param multiplier the measurement to multiply by.
517   * @return the multiplication result
518   */
519  default Measure<?> times(Temperature multiplier) {
520    return MultUnit.combine(unit(), multiplier.unit())
521        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
522  }
523
524  /**
525   * Multiplies this measure by a time and returns the resulting measure in the most appropriate
526   * unit.
527   *
528   * @param multiplier the measurement to multiply by.
529   * @return the multiplication result
530   */
531  default Measure<?> times(Time multiplier) {
532    return MultUnit.combine(unit(), multiplier.unit())
533        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
534  }
535
536  /**
537   * Multiplies this measure by a torque and returns the resulting measure in the most appropriate
538   * unit.
539   *
540   * @param multiplier the measurement to multiply by.
541   * @return the multiplication result
542   */
543  default Measure<?> times(Torque multiplier) {
544    return MultUnit.combine(unit(), multiplier.unit())
545        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
546  }
547
548  /**
549   * Multiplies this measure by a generic velocity and returns the resulting measure in the most
550   * appropriate unit.
551   *
552   * @param multiplier the measurement to multiply by.
553   * @return the multiplication result
554   */
555  default Measure<?> times(Velocity<?> multiplier) {
556    return MultUnit.combine(unit(), multiplier.unit())
557        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
558  }
559
560  /**
561   * Multiplies this measure by a voltage and returns the resulting measure in the most appropriate
562   * unit.
563   *
564   * @param multiplier the measurement to multiply by.
565   * @return the multiplication result
566   */
567  default Measure<?> times(Voltage multiplier) {
568    return MultUnit.combine(unit(), multiplier.unit())
569        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
570  }
571
572  /**
573   * Multiplies this measure by a resistance and returns the resulting measure in the most
574   * appropriate unit.
575   *
576   * @param multiplier the measurement to multiply by.
577   * @return the multiplication result
578   */
579  default Measure<?> times(Resistance multiplier) {
580    return MultUnit.combine(unit(), multiplier.unit())
581        .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
582  }
583
584  /**
585   * Multiplies this measure by a conversion factor, returning the converted measurement. Unlike
586   * {@link #times(Per)}, this allows for basic unit cancellation to return measurements of a known
587   * dimension.
588   *
589   * @param conversionFactor the conversion factor by which to multiply
590   * @param <Other> the unit type to convert to
591   * @param <M> the concrete return unit type. <strong>Note: the conversion factor's numerator unit
592   *     must return instances of this type from {@link Unit#ofBaseUnits(double)}}</strong>
593   * @return the converted result
594   */
595  @SuppressWarnings("unchecked")
596  default <Other extends Unit, M extends Measure<Other>> M timesConversionFactor(
597      Measure<? extends PerUnit<Other, U>> conversionFactor) {
598    return (M)
599        conversionFactor
600            .unit()
601            .getBaseUnit()
602            .numerator()
603            .ofBaseUnits(baseUnitMagnitude() * conversionFactor.baseUnitMagnitude());
604  }
605
606  /**
607   * Multiplies this measure by another measurement of the inverse unit type (eg Time multiplied by
608   * Frequency) and returns the resulting dimensionless measure.
609   *
610   * @param multiplier the measurement to multiply by.
611   * @return the multiplication result
612   */
613  default Dimensionless timesInverse(
614      Measure<? extends PerUnit<DimensionlessUnit, ? extends U>> multiplier) {
615    return Value.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
616  }
617
618  /**
619   * Multiplies this measure by another measurement of something over the unit type (eg Time
620   * multiplied by LinearVelocity) and returns the resulting measure of the ratio's dividend unit.
621   *
622   * @param ratio the measurement to multiply by.
623   * @param <Other> other unit that the results are in terms of
624   * @return the multiplication result
625   */
626  default <Other extends Unit> Measure<Other> timesRatio(
627      Measure<? extends PerUnit<? extends Other, U>> ratio) {
628    return ImmutableMeasure.ofBaseUnits(
629        baseUnitMagnitude() * ratio.baseUnitMagnitude(), ratio.unit().numerator());
630  }
631
632  /**
633   * Divides this measure by a unitless scalar and returns the result.
634   *
635   * @param divisor the measurement to divide by.
636   * @return the division result
637   */
638  Measure<U> div(double divisor);
639
640  /**
641   * Divides this measure by a dimensionless scalar and returns the result.
642   *
643   * @param divisor the measurement to divide by.
644   * @return the division result
645   */
646  Measure<U> div(Dimensionless divisor);
647
648  /**
649   * Divides this measurement by another measure and performs some dimensional analysis to reduce
650   * the units.
651   *
652   * @param divisor the unit to divide by
653   * @return the resulting measure
654   */
655  default Measure<?> div(Measure<?> divisor) {
656    final double baseUnitResult = baseUnitMagnitude() / divisor.baseUnitMagnitude();
657
658    if (unit().getBaseUnit().equals(divisor.unit().getBaseUnit())) {
659      // These are the same unit, return a dimensionless
660      return Value.ofBaseUnits(baseUnitResult);
661    }
662
663    if (divisor instanceof Dimensionless) {
664      // Dividing by a dimensionless, do a scalar division
665      return unit().ofBaseUnits(baseUnitResult);
666    }
667
668    // Numerator is a dimensionless
669    if (unit() instanceof DimensionlessUnit && divisor.unit() instanceof PerUnit<?, ?> ratio) {
670      // Dividing by a ratio, return its reciprocal scaled by this
671      return ratio.reciprocal().ofBaseUnits(baseUnitResult);
672    }
673
674    if (divisor.unit() instanceof PerUnit<?, ?> ratio
675        && ratio.numerator().getBaseUnit().equals(baseUnit())) {
676      // Numerator of the ratio cancels out
677      // N / (N / D) -> N * D / N -> D
678      // Note: all Velocity and Acceleration units inherit from PerUnit
679      return ratio.denominator().ofBaseUnits(baseUnitResult);
680    }
681
682    if (divisor.unit() instanceof TimeUnit time) {
683      // Dividing by a time, return a Velocity
684      return VelocityUnit.combine(unit(), time).ofBaseUnits(baseUnitResult);
685    }
686
687    if (divisor instanceof Acceleration<?> acceleration) {
688      return div(acceleration);
689    } else if (divisor instanceof Angle angle) {
690      return div(angle);
691    } else if (divisor instanceof AngularAcceleration angularAcceleration) {
692      return div(angularAcceleration);
693    } else if (divisor instanceof AngularMomentum angularMomentum) {
694      return div(angularMomentum);
695    } else if (divisor instanceof AngularVelocity angularVelocity) {
696      return div(angularVelocity);
697    } else if (divisor instanceof Current current) {
698      return div(current);
699    } else if (divisor instanceof Dimensionless dimensionless) {
700      // n.b. this case should already be covered
701      return div(dimensionless);
702    } else if (divisor instanceof Distance distance) {
703      return div(distance);
704    } else if (divisor instanceof Energy energy) {
705      return div(energy);
706    } else if (divisor instanceof Force force) {
707      return div(force);
708    } else if (divisor instanceof Frequency frequency) {
709      return div(frequency);
710    } else if (divisor instanceof LinearAcceleration linearAcceleration) {
711      return div(linearAcceleration);
712    } else if (divisor instanceof LinearVelocity linearVelocity) {
713      return div(linearVelocity);
714    } else if (divisor instanceof Mass mass) {
715      return div(mass);
716    } else if (divisor instanceof MomentOfInertia momentOfInertia) {
717      return div(momentOfInertia);
718    } else if (divisor instanceof Mult<?, ?> mult) {
719      return div(mult);
720    } else if (divisor instanceof Per<?, ?> per) {
721      return div(per);
722    } else if (divisor instanceof Power power) {
723      return div(power);
724    } else if (divisor instanceof Temperature temperature) {
725      return div(temperature);
726    } else if (divisor instanceof Time time) {
727      return div(time);
728    } else if (divisor instanceof Torque torque) {
729      return div(torque);
730    } else if (divisor instanceof Velocity<?> velocity) {
731      return div(velocity);
732    } else if (divisor instanceof Voltage voltage) {
733      return div(voltage);
734    } else if (divisor instanceof Resistance resistance) {
735      return div(resistance);
736    } else {
737      // Dimensional analysis fallthrough or a generic input measure type
738      // Do a basic unit multiplication
739      return PerUnit.combine(unit(), divisor.unit()).ofBaseUnits(baseUnitResult);
740    }
741  }
742
743  /**
744   * Divides this measure by a generic acceleration and returns the result in the most appropriate
745   * unit.
746   *
747   * @param divisor the measurement to divide by.
748   * @return the division result
749   */
750  default Measure<?> div(Acceleration<?> divisor) {
751    return PerUnit.combine(unit(), divisor.unit())
752        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
753  }
754
755  /**
756   * Divides this measure by an angle and returns the result in the most appropriate unit.
757   *
758   * @param divisor the measurement to divide by.
759   * @return the division result
760   */
761  default Measure<?> div(Angle divisor) {
762    return PerUnit.combine(unit(), divisor.unit())
763        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
764  }
765
766  /**
767   * Divides this measure by an angular acceleration and returns the result in the most appropriate
768   * unit.
769   *
770   * @param divisor the measurement to divide by.
771   * @return the division result
772   */
773  default Measure<?> div(AngularAcceleration divisor) {
774    return PerUnit.combine(unit(), divisor.unit())
775        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
776  }
777
778  /**
779   * Divides this measure by an angular momentum and returns the result in the most appropriate
780   * unit.
781   *
782   * @param divisor the measurement to divide by.
783   * @return the division result
784   */
785  default Measure<?> div(AngularMomentum divisor) {
786    return PerUnit.combine(unit(), divisor.unit())
787        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
788  }
789
790  /**
791   * Divides this measure by an angular velocity and returns the result in the most appropriate
792   * unit.
793   *
794   * @param divisor the measurement to divide by.
795   * @return the division result
796   */
797  default Measure<?> div(AngularVelocity divisor) {
798    return PerUnit.combine(unit(), divisor.unit())
799        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
800  }
801
802  /**
803   * Divides this measure by an electric current and returns the result in the most appropriate
804   * unit.
805   *
806   * @param divisor the measurement to divide by.
807   * @return the division result
808   */
809  default Measure<?> div(Current divisor) {
810    return PerUnit.combine(unit(), divisor.unit())
811        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
812  }
813
814  /**
815   * Divides this measure by a distance and returns the result in the most appropriate unit.
816   *
817   * @param divisor the measurement to divide by.
818   * @return the division result
819   */
820  default Measure<?> div(Distance divisor) {
821    return PerUnit.combine(unit(), divisor.unit())
822        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
823  }
824
825  /**
826   * Divides this measure by an energy and returns the result in the most appropriate unit.
827   *
828   * @param divisor the measurement to divide by.
829   * @return the division result
830   */
831  default Measure<?> div(Energy divisor) {
832    return PerUnit.combine(unit(), divisor.unit())
833        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
834  }
835
836  /**
837   * Divides this measure by a force and returns the result in the most appropriate unit.
838   *
839   * @param divisor the measurement to divide by.
840   * @return the division result
841   */
842  default Measure<?> div(Force divisor) {
843    return PerUnit.combine(unit(), divisor.unit())
844        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
845  }
846
847  /**
848   * Divides this measure by a frequency and returns the result in the most appropriate unit.
849   *
850   * @param divisor the measurement to divide by.
851   * @return the division result
852   */
853  default Measure<?> div(Frequency divisor) {
854    return PerUnit.combine(unit(), divisor.unit())
855        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
856  }
857
858  /**
859   * Divides this measure by a linear acceleration and returns the result in the most appropriate
860   * unit.
861   *
862   * @param divisor the measurement to divide by.
863   * @return the division result
864   */
865  default Measure<?> div(LinearAcceleration divisor) {
866    return PerUnit.combine(unit(), divisor.unit())
867        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
868  }
869
870  /**
871   * Divides this measure by a linear momentum and returns the result in the most appropriate unit.
872   *
873   * @param divisor the measurement to divide by.
874   * @return the division result
875   */
876  default Measure<?> div(LinearMomentum divisor) {
877    return PerUnit.combine(unit(), divisor.unit())
878        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
879  }
880
881  /**
882   * Divides this measure by a linear velocity and returns the result in the most appropriate unit.
883   *
884   * @param divisor the measurement to divide by.
885   * @return the division result
886   */
887  default Measure<?> div(LinearVelocity divisor) {
888    return PerUnit.combine(unit(), divisor.unit())
889        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
890  }
891
892  /**
893   * Divides this measure by a mass and returns the result in the most appropriate unit.
894   *
895   * @param divisor the measurement to divide by.
896   * @return the division result
897   */
898  default Measure<?> div(Mass divisor) {
899    return PerUnit.combine(unit(), divisor.unit())
900        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
901  }
902
903  /**
904   * Divides this measure by a moment of inertia and returns the result in the most appropriate
905   * unit.
906   *
907   * @param divisor the measurement to divide by.
908   * @return the division result
909   */
910  default Measure<?> div(MomentOfInertia divisor) {
911    return PerUnit.combine(unit(), divisor.unit())
912        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
913  }
914
915  /**
916   * Divides this measure by a generic multiplication and returns the result in the most appropriate
917   * unit.
918   *
919   * @param divisor the measurement to divide by.
920   * @return the division result
921   */
922  default Measure<?> div(Mult<?, ?> divisor) {
923    return PerUnit.combine(unit(), divisor.unit())
924        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
925  }
926
927  /**
928   * Divides this measure by a power and returns the result in the most appropriate unit.
929   *
930   * @param divisor the measurement to divide by.
931   * @return the division result
932   */
933  default Measure<?> div(Power divisor) {
934    return PerUnit.combine(unit(), divisor.unit())
935        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
936  }
937
938  /**
939   * Divides this measure by a generic ratio and returns the result in the most appropriate unit.
940   *
941   * @param divisor the measurement to divide by.
942   * @return the division result
943   */
944  default Measure<?> div(Per<?, ?> divisor) {
945    return PerUnit.combine(unit(), divisor.unit())
946        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
947  }
948
949  /**
950   * Divides this measure by a temperature and returns the result in the most appropriate unit.
951   *
952   * @param divisor the measurement to divide by.
953   * @return the division result
954   */
955  default Measure<?> div(Temperature divisor) {
956    return PerUnit.combine(unit(), divisor.unit())
957        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
958  }
959
960  /**
961   * Divides this measure by a time and returns the result in the most appropriate unit. This will
962   * often - but not always - result in a {@link Per} type like {@link LinearVelocity} or {@link
963   * Acceleration}.
964   *
965   * @param divisor the measurement to divide by.
966   * @return the division result
967   */
968  default Measure<?> div(Time divisor) {
969    return VelocityUnit.combine(unit(), divisor.unit())
970        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
971  }
972
973  /**
974   * Divides this measure by a torque and returns the result in the most appropriate unit.
975   *
976   * @param divisor the measurement to divide by.
977   * @return the division result
978   */
979  default Measure<?> div(Torque divisor) {
980    return PerUnit.combine(unit(), divisor.unit())
981        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
982  }
983
984  /**
985   * Divides this measure by a generic velocity and returns the result in the most appropriate unit.
986   *
987   * @param divisor the measurement to divide by.
988   * @return the division result
989   */
990  default Measure<?> div(Velocity<?> divisor) {
991    return PerUnit.combine(unit(), divisor.unit())
992        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
993  }
994
995  /**
996   * Divides this measure by a voltage and returns the result in the most appropriate unit.
997   *
998   * @param divisor the measurement to divide by.
999   * @return the division result
1000   */
1001  default Measure<?> div(Voltage divisor) {
1002    return PerUnit.combine(unit(), divisor.unit())
1003        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
1004  }
1005
1006  /**
1007   * Divides this measure by a resistance and returns the result in the most appropriate unit.
1008   *
1009   * @param divisor the measurement to divide by.
1010   * @return the division result
1011   */
1012  default Measure<?> div(Resistance divisor) {
1013    return PerUnit.combine(unit(), divisor.unit())
1014        .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
1015  }
1016
1017  /**
1018   * Divides this measure by an acceleration unit and returns the result in the most appropriate
1019   * unit. This is equivalent to {@code div(divisorUnit.of(1))}.
1020   *
1021   * @param divisorUnit the unit to divide by.
1022   * @return the division result
1023   */
1024  default Measure<?> per(AccelerationUnit<?> divisorUnit) {
1025    return div(divisorUnit.one());
1026  }
1027
1028  /**
1029   * Divides this measure by an angle unit and returns the result in the most appropriate unit. This
1030   * is equivalent to {@code div(divisorUnit.of(1))}.
1031   *
1032   * @param divisorUnit the unit to divide by.
1033   * @return the division result
1034   */
1035  default Measure<?> per(AngleUnit divisorUnit) {
1036    return div(divisorUnit.one());
1037  }
1038
1039  /**
1040   * Divides this measure by an angular acceleration unit and returns the result in the most
1041   * appropriate unit. This is equivalent to {@code div(divisorUnit.of(1))}.
1042   *
1043   * @param divisorUnit the unit to divide by.
1044   * @return the division result
1045   */
1046  default Measure<?> per(AngularAccelerationUnit divisorUnit) {
1047    return div(divisorUnit.one());
1048  }
1049
1050  /**
1051   * Divides this measure by an angular momentum unit and returns the result in the most appropriate
1052   * unit. This is equivalent to {@code div(divisorUnit.of(1))}.
1053   *
1054   * @param divisorUnit the unit to divide by.
1055   * @return the division result
1056   */
1057  default Measure<?> per(AngularMomentumUnit divisorUnit) {
1058    return div(divisorUnit.one());
1059  }
1060
1061  /**
1062   * Divides this measure by an angular velocity unit and returns the result in the most appropriate
1063   * unit. This is equivalent to {@code div(divisorUnit.of(1))}.
1064   *
1065   * @param divisorUnit the unit to divide by.
1066   * @return the division result
1067   */
1068  default Measure<?> per(AngularVelocityUnit divisorUnit) {
1069    return div(divisorUnit.one());
1070  }
1071
1072  /**
1073   * Divides this measure by a unit of electric current and returns the result in the most
1074   * appropriate unit. This is equivalent to {@code div(divisorUnit.of(1))}.
1075   *
1076   * @param divisorUnit the unit to divide by.
1077   * @return the division result
1078   */
1079  default Measure<?> per(CurrentUnit divisorUnit) {
1080    return div(divisorUnit.one());
1081  }
1082
1083  /**
1084   * Divides this measure by a distance unit and returns the result in the most appropriate unit.
1085   * This is equivalent to {@code div(divisorUnit.of(1))}.
1086   *
1087   * @param divisorUnit the unit to divide by.
1088   * @return the division result
1089   */
1090  default Measure<?> per(DistanceUnit divisorUnit) {
1091    return div(divisorUnit.one());
1092  }
1093
1094  /**
1095   * Divides this measure by an energy unit and returns the result in the most appropriate unit.
1096   * This is equivalent to {@code div(divisorUnit.of(1))}.
1097   *
1098   * @param divisorUnit the unit to divide by.
1099   * @return the division result
1100   */
1101  default Measure<?> per(EnergyUnit divisorUnit) {
1102    return div(divisorUnit.one());
1103  }
1104
1105  /**
1106   * Divides this measure by a force unit and returns the result in the most appropriate unit. This
1107   * is equivalent to {@code div(divisorUnit.of(1))}.
1108   *
1109   * @param divisorUnit the unit to divide by.
1110   * @return the division result
1111   */
1112  default Measure<?> per(ForceUnit divisorUnit) {
1113    return div(divisorUnit.one());
1114  }
1115
1116  /**
1117   * Divides this measure by a frequency unit and returns the result in the most appropriate unit.
1118   * This is equivalent to {@code div(divisorUnit.of(1))}.
1119   *
1120   * @param divisorUnit the unit to divide by.
1121   * @return the division result
1122   */
1123  default Measure<?> per(FrequencyUnit divisorUnit) {
1124    return div(divisorUnit.one());
1125  }
1126
1127  /**
1128   * Divides this measure by a linear acceleration unit and returns the result in the most
1129   * appropriate unit. This is equivalent to {@code div(divisorUnit.of(1))}.
1130   *
1131   * @param divisorUnit the unit to divide by.
1132   * @return the division result
1133   */
1134  default Measure<?> per(LinearAccelerationUnit divisorUnit) {
1135    return div(divisorUnit.one());
1136  }
1137
1138  /**
1139   * Divides this measure by a linear momentum unit and returns the result in the most appropriate
1140   * unit. This is equivalent to {@code div(divisorUnit.of(1))}.
1141   *
1142   * @param divisorUnit the unit to divide by.
1143   * @return the division result
1144   */
1145  default Measure<?> per(LinearMomentumUnit divisorUnit) {
1146    return div(divisorUnit.one());
1147  }
1148
1149  /**
1150   * Divides this measure by a linear velocity unit and returns the result in the most appropriate
1151   * unit. This is equivalent to {@code div(divisorUnit.of(1))}.
1152   *
1153   * @param divisorUnit the unit to divide by.
1154   * @return the division result
1155   */
1156  default Measure<?> per(LinearVelocityUnit divisorUnit) {
1157    return div(divisorUnit.one());
1158  }
1159
1160  /**
1161   * Divides this measure by a mass unit and returns the result in the most appropriate unit. This
1162   * is equivalent to {@code div(divisorUnit.of(1))}.
1163   *
1164   * @param divisorUnit the unit to divide by.
1165   * @return the division result
1166   */
1167  default Measure<?> per(MassUnit divisorUnit) {
1168    return div(divisorUnit.one());
1169  }
1170
1171  /**
1172   * Divides this measure by a moment of inertia unit and returns the result in the most appropriate
1173   * unit. This is equivalent to {@code div(divisorUnit.of(1))}.
1174   *
1175   * @param divisorUnit the unit to divide by.
1176   * @return the division result
1177   */
1178  default Measure<?> per(MomentOfInertiaUnit divisorUnit) {
1179    return div(divisorUnit.one());
1180  }
1181
1182  /**
1183   * Divides this measure by a generic compound unit and returns the result in the most appropriate
1184   * unit. This is equivalent to {@code div(divisorUnit.of(1))}.
1185   *
1186   * @param divisorUnit the unit to divide by.
1187   * @return the division result
1188   */
1189  default Measure<?> per(MultUnit<?, ?> divisorUnit) {
1190    return div(divisorUnit.one());
1191  }
1192
1193  /**
1194   * Divides this measure by a power unit and returns the result in the most appropriate unit. This
1195   * is equivalent to {@code div(divisorUnit.of(1))}.
1196   *
1197   * @param divisorUnit the unit to divide by.
1198   * @return the division result
1199   */
1200  default Measure<?> per(PowerUnit divisorUnit) {
1201    return div(divisorUnit.one());
1202  }
1203
1204  /**
1205   * Divides this measure by a generic ratio unit and returns the result in the most appropriate
1206   * unit. This is equivalent to {@code div(divisorUnit.of(1))}.
1207   *
1208   * @param divisorUnit the unit to divide by.
1209   * @return the division result
1210   */
1211  default Measure<?> per(PerUnit<?, ?> divisorUnit) {
1212    return div(divisorUnit.one());
1213  }
1214
1215  /**
1216   * Divides this measure by a temperature unit and returns the result in the most appropriate unit.
1217   * This is equivalent to {@code div(divisorUnit.of(1))}.
1218   *
1219   * @param divisorUnit the unit to divide by.
1220   * @return the division result
1221   */
1222  default Measure<?> per(TemperatureUnit divisorUnit) {
1223    return div(divisorUnit.one());
1224  }
1225
1226  /**
1227   * Divides this measure by a time period and returns the result in the most appropriate unit. This
1228   * is equivalent to {@code div(period.of(1))}.
1229   *
1230   * @param period the time period measurement to divide by.
1231   * @return the division result
1232   */
1233  default Measure<?> per(TimeUnit period) {
1234    return div(period.of(1));
1235  }
1236
1237  /**
1238   * Divides this measure by a torque unit and returns the result in the most appropriate unit. This
1239   * is equivalent to {@code div(divisorUnit.of(1))}.
1240   *
1241   * @param divisorUnit the unit to divide by.
1242   * @return the division result
1243   */
1244  default Measure<?> per(TorqueUnit divisorUnit) {
1245    return div(divisorUnit.one());
1246  }
1247
1248  /**
1249   * Divides this measure by a velocity unit and returns the result in the most appropriate unit.
1250   * This is equivalent to {@code div(divisorUnit.of(1))}.
1251   *
1252   * @param divisorUnit the unit to divide by.
1253   * @return the division result
1254   */
1255  default Measure<?> per(VelocityUnit<?> divisorUnit) {
1256    return div(divisorUnit.one());
1257  }
1258
1259  /**
1260   * Divides this measure by a voltage unit and returns the result in the most appropriate unit.
1261   * This is equivalent to {@code div(divisorUnit.of(1))}.
1262   *
1263   * @param divisorUnit the unit to divide by.
1264   * @return the division result
1265   */
1266  default Measure<?> per(VoltageUnit divisorUnit) {
1267    return div(divisorUnit.one());
1268  }
1269
1270  /**
1271   * Divides this measure by a resistance unit and returns the result in the most appropriate unit.
1272   * This is equivalent to {@code div(divisorUnit.of(1))}.
1273   *
1274   * @param divisorUnit the unit to divide by.
1275   * @return the division result
1276   */
1277  default Measure<?> per(ResistanceUnit divisorUnit) {
1278    return div(divisorUnit.one());
1279  }
1280
1281  /**
1282   * Divides this measure by a unitless scalar and returns the result.
1283   *
1284   * @param divisor the measurement to divide by.
1285   * @return the division result
1286   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1287   */
1288  @Deprecated(since = "2025", forRemoval = true)
1289  default Measure<U> divide(double divisor) {
1290    return div(divisor);
1291  }
1292
1293  /**
1294   * Divides this measure by a dimensionless scalar and returns the result.
1295   *
1296   * @param divisor the measurement to divide by.
1297   * @return the division result
1298   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1299   */
1300  @Deprecated(since = "2025", forRemoval = true)
1301  default Measure<U> divide(Dimensionless divisor) {
1302    return div(divisor);
1303  }
1304
1305  /**
1306   * Divides this measurement by another measure and performs some dimensional analysis to reduce
1307   * the units.
1308   *
1309   * @param divisor the unit to divide by
1310   * @return the resulting measure
1311   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1312   */
1313  @Deprecated(since = "2025", forRemoval = true)
1314  default Measure<?> divide(Measure<?> divisor) {
1315    return div(divisor);
1316  }
1317
1318  /**
1319   * Divides this measure by a generic acceleration and returns the result in the most appropriate
1320   * unit.
1321   *
1322   * @param divisor the measurement to divide by.
1323   * @return the division result
1324   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1325   */
1326  @Deprecated(since = "2025", forRemoval = true)
1327  default Measure<?> divide(Acceleration<?> divisor) {
1328    return div(divisor);
1329  }
1330
1331  /**
1332   * Divides this measure by an angle and returns the result in the most appropriate unit.
1333   *
1334   * @param divisor the measurement to divide by.
1335   * @return the division result
1336   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1337   */
1338  @Deprecated(since = "2025", forRemoval = true)
1339  default Measure<?> divide(Angle divisor) {
1340    return div(divisor);
1341  }
1342
1343  /**
1344   * Divides this measure by an angular acceleration and returns the result in the most appropriate
1345   * unit.
1346   *
1347   * @param divisor the measurement to divide by.
1348   * @return the division result
1349   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1350   */
1351  @Deprecated(since = "2025", forRemoval = true)
1352  default Measure<?> divide(AngularAcceleration divisor) {
1353    return div(divisor);
1354  }
1355
1356  /**
1357   * Divides this measure by an angular momentum and returns the result in the most appropriate
1358   * unit.
1359   *
1360   * @param divisor the measurement to divide by.
1361   * @return the division result
1362   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1363   */
1364  @Deprecated(since = "2025", forRemoval = true)
1365  default Measure<?> divide(AngularMomentum divisor) {
1366    return div(divisor);
1367  }
1368
1369  /**
1370   * Divides this measure by an angular velocity and returns the result in the most appropriate
1371   * unit.
1372   *
1373   * @param divisor the measurement to divide by.
1374   * @return the division result
1375   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1376   */
1377  @Deprecated(since = "2025", forRemoval = true)
1378  default Measure<?> divide(AngularVelocity divisor) {
1379    return div(divisor);
1380  }
1381
1382  /**
1383   * Divides this measure by an electric current and returns the result in the most appropriate
1384   * unit.
1385   *
1386   * @param divisor the measurement to divide by.
1387   * @return the division result
1388   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1389   */
1390  @Deprecated(since = "2025", forRemoval = true)
1391  default Measure<?> divide(Current divisor) {
1392    return div(divisor);
1393  }
1394
1395  /**
1396   * Divides this measure by a distance and returns the result in the most appropriate unit.
1397   *
1398   * @param divisor the measurement to divide by.
1399   * @return the division result
1400   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1401   */
1402  @Deprecated(since = "2025", forRemoval = true)
1403  default Measure<?> divide(Distance divisor) {
1404    return div(divisor);
1405  }
1406
1407  /**
1408   * Divides this measure by an energy and returns the result in the most appropriate unit.
1409   *
1410   * @param divisor the measurement to divide by.
1411   * @return the division result
1412   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1413   */
1414  @Deprecated(since = "2025", forRemoval = true)
1415  default Measure<?> divide(Energy divisor) {
1416    return div(divisor);
1417  }
1418
1419  /**
1420   * Divides this measure by a force and returns the result in the most appropriate unit.
1421   *
1422   * @param divisor the measurement to divide by.
1423   * @return the division result
1424   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1425   */
1426  @Deprecated(since = "2025", forRemoval = true)
1427  default Measure<?> divide(Force divisor) {
1428    return div(divisor);
1429  }
1430
1431  /**
1432   * Divides this measure by a frequency and returns the result in the most appropriate unit.
1433   *
1434   * @param divisor the measurement to divide by.
1435   * @return the division result
1436   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1437   */
1438  @Deprecated(since = "2025", forRemoval = true)
1439  default Measure<?> divide(Frequency divisor) {
1440    return div(divisor);
1441  }
1442
1443  /**
1444   * Divides this measure by a linear acceleration and returns the result in the most appropriate
1445   * unit.
1446   *
1447   * @param divisor the measurement to divide by.
1448   * @return the division result
1449   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1450   */
1451  @Deprecated(since = "2025", forRemoval = true)
1452  default Measure<?> divide(LinearAcceleration divisor) {
1453    return div(divisor);
1454  }
1455
1456  /**
1457   * Divides this measure by a linear momentum and returns the result in the most appropriate unit.
1458   *
1459   * @param divisor the measurement to divide by.
1460   * @return the division result
1461   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1462   */
1463  @Deprecated(since = "2025", forRemoval = true)
1464  default Measure<?> divide(LinearMomentum divisor) {
1465    return div(divisor);
1466  }
1467
1468  /**
1469   * Divides this measure by a linear velocity and returns the result in the most appropriate unit.
1470   *
1471   * @param divisor the measurement to divide by.
1472   * @return the division result
1473   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1474   */
1475  @Deprecated(since = "2025", forRemoval = true)
1476  default Measure<?> divide(LinearVelocity divisor) {
1477    return div(divisor);
1478  }
1479
1480  /**
1481   * Divides this measure by a mass and returns the result in the most appropriate unit.
1482   *
1483   * @param divisor the measurement to divide by.
1484   * @return the division result
1485   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1486   */
1487  @Deprecated(since = "2025", forRemoval = true)
1488  default Measure<?> divide(Mass divisor) {
1489    return div(divisor);
1490  }
1491
1492  /**
1493   * Divides this measure by a moment of inertia and returns the result in the most appropriate
1494   * unit.
1495   *
1496   * @param divisor the measurement to divide by.
1497   * @return the division result
1498   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1499   */
1500  @Deprecated(since = "2025", forRemoval = true)
1501  default Measure<?> divide(MomentOfInertia divisor) {
1502    return div(divisor);
1503  }
1504
1505  /**
1506   * Divides this measure by a generic multiplication and returns the result in the most appropriate
1507   * unit.
1508   *
1509   * @param divisor the measurement to divide by.
1510   * @return the division result
1511   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1512   */
1513  @Deprecated(since = "2025", forRemoval = true)
1514  default Measure<?> divide(Mult<?, ?> divisor) {
1515    return div(divisor);
1516  }
1517
1518  /**
1519   * Divides this measure by a power and returns the result in the most appropriate unit.
1520   *
1521   * @param divisor the measurement to divide by.
1522   * @return the division result
1523   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1524   */
1525  @Deprecated(since = "2025", forRemoval = true)
1526  default Measure<?> divide(Power divisor) {
1527    return div(divisor);
1528  }
1529
1530  /**
1531   * Divides this measure by a generic ratio and returns the result in the most appropriate unit.
1532   *
1533   * @param divisor the measurement to divide by.
1534   * @return the division result
1535   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1536   */
1537  @Deprecated(since = "2025", forRemoval = true)
1538  default Measure<?> divide(Per<?, ?> divisor) {
1539    return div(divisor);
1540  }
1541
1542  /**
1543   * Divides this measure by a temperature and returns the result in the most appropriate unit.
1544   *
1545   * @param divisor the measurement to divide by.
1546   * @return the division result
1547   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1548   */
1549  @Deprecated(since = "2025", forRemoval = true)
1550  default Measure<?> divide(Temperature divisor) {
1551    return div(divisor);
1552  }
1553
1554  /**
1555   * Divides this measure by a time and returns the result in the most appropriate unit. This will
1556   * often - but not always - result in a {@link Per} type like {@link LinearVelocity} or {@link
1557   * Acceleration}.
1558   *
1559   * @param divisor the measurement to divide by.
1560   * @return the division result
1561   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1562   */
1563  @Deprecated(since = "2025", forRemoval = true)
1564  default Measure<?> divide(Time divisor) {
1565    return div(divisor);
1566  }
1567
1568  /**
1569   * Divides this measure by a torque and returns the result in the most appropriate unit.
1570   *
1571   * @param divisor the measurement to divide by.
1572   * @return the division result
1573   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1574   */
1575  @Deprecated(since = "2025", forRemoval = true)
1576  default Measure<?> divide(Torque divisor) {
1577    return div(divisor);
1578  }
1579
1580  /**
1581   * Divides this measure by a generic velocity and returns the result in the most appropriate unit.
1582   *
1583   * @param divisor the measurement to divide by.
1584   * @return the division result
1585   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1586   */
1587  @Deprecated(since = "2025", forRemoval = true)
1588  default Measure<?> divide(Velocity<?> divisor) {
1589    return div(divisor);
1590  }
1591
1592  /**
1593   * Divides this measure by a voltage and returns the result in the most appropriate unit.
1594   *
1595   * @param divisor the measurement to divide by.
1596   * @return the division result
1597   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1598   */
1599  @Deprecated(since = "2025", forRemoval = true)
1600  default Measure<?> divide(Voltage divisor) {
1601    return div(divisor);
1602  }
1603
1604  /**
1605   * Divides this measure by a resistance and returns the result in the most appropriate unit.
1606   *
1607   * @param divisor the measurement to divide by.
1608   * @return the division result
1609   * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin
1610   */
1611  @Deprecated(since = "2025", forRemoval = true)
1612  default Measure<?> divide(Resistance divisor) {
1613    return div(divisor);
1614  }
1615
1616  /**
1617   * Divides this measure by a ratio in terms of this measurement's unit to another unit, returning
1618   * a measurement in terms of the other unit.
1619   *
1620   * @param divisor the measurement to divide by.
1621   * @param <Other> the other unit that results are in terms of
1622   * @return the division result
1623   */
1624  // eg Dimensionless / (Dimensionless / Time) -> Time
1625  default <Other extends Unit> Measure<Other> divideRatio(
1626      Measure<? extends PerUnit<? extends U, Other>> divisor) {
1627    return ImmutableMeasure.ofBaseUnits(
1628        baseUnitMagnitude() / divisor.baseUnitMagnitude(), divisor.unit().denominator());
1629  }
1630
1631  /**
1632   * Checks if this measure is near another measure of the same unit. Provide a variance threshold
1633   * for use for a +/- scalar, such as 0.05 for +/- 5%.
1634   *
1635   * <pre>
1636   *   Inches.of(11).isNear(Inches.of(10), 0.1) // true
1637   *   Inches.of(12).isNear(Inches.of(10), 0.1) // false
1638   * </pre>
1639   *
1640   * @param other the other measurement to compare against
1641   * @param varianceThreshold the acceptable variance threshold, in terms of an acceptable +/- error
1642   *     range multiplier. Checking if a value is within 10% means a value of 0.1 should be passed;
1643   *     checking if a value is within 1% means a value of 0.01 should be passed, and so on.
1644   * @return true if this unit is near the other measure, otherwise false
1645   */
1646  default boolean isNear(Measure<?> other, double varianceThreshold) {
1647    if (!this.unit().getBaseUnit().equivalent(other.unit().getBaseUnit())) {
1648      return false; // Disjoint units, not compatible
1649    }
1650
1651    // abs so negative inputs are calculated correctly
1652    var tolerance = Math.abs(other.baseUnitMagnitude() * varianceThreshold);
1653
1654    return Math.abs(this.baseUnitMagnitude() - other.baseUnitMagnitude()) <= tolerance;
1655  }
1656
1657  /**
1658   * Checks if this measure is near another measure of the same unit, with a specified tolerance of
1659   * the same unit.
1660   *
1661   * <pre>
1662   *     Meters.of(1).isNear(Meters.of(1.2), Millimeters.of(300)) // true
1663   *     Degrees.of(90).isNear(Rotations.of(0.5), Degrees.of(45)) // false
1664   * </pre>
1665   *
1666   * @param other the other measure to compare against.
1667   * @param tolerance the tolerance allowed in which the two measures are defined as near each
1668   *     other.
1669   * @return true if this unit is near the other measure, otherwise false.
1670   */
1671  default boolean isNear(Measure<U> other, Measure<U> tolerance) {
1672    return Math.abs(this.baseUnitMagnitude() - other.baseUnitMagnitude())
1673        <= Math.abs(tolerance.baseUnitMagnitude());
1674  }
1675
1676  /**
1677   * Checks if this measure is equivalent to another measure of the same unit.
1678   *
1679   * @param other the measure to compare to
1680   * @return true if this measure is equivalent, false otherwise
1681   */
1682  default boolean isEquivalent(Measure<?> other) {
1683    // Return false for disjoint units that aren't compatible
1684    return this.unit().getBaseUnit().equals(other.unit().getBaseUnit())
1685        && Math.abs(baseUnitMagnitude() - other.baseUnitMagnitude()) <= EQUIVALENCE_THRESHOLD;
1686  }
1687
1688  /** {@inheritDoc} */
1689  @Override
1690  default int compareTo(Measure<U> o) {
1691    return Double.compare(this.baseUnitMagnitude(), o.baseUnitMagnitude());
1692  }
1693
1694  /**
1695   * Checks if this measure is greater than another measure of the same unit.
1696   *
1697   * @param o the other measure to compare to
1698   * @return true if this measure has a greater equivalent magnitude, false otherwise
1699   */
1700  default boolean gt(Measure<U> o) {
1701    return compareTo(o) > 0;
1702  }
1703
1704  /**
1705   * Checks if this measure is greater than or equivalent to another measure of the same unit.
1706   *
1707   * @param o the other measure to compare to
1708   * @return true if this measure has an equal or greater equivalent magnitude, false otherwise
1709   */
1710  default boolean gte(Measure<U> o) {
1711    return compareTo(o) > 0 || isEquivalent(o);
1712  }
1713
1714  /**
1715   * Checks if this measure is less than another measure of the same unit.
1716   *
1717   * @param o the other measure to compare to
1718   * @return true if this measure has a lesser equivalent magnitude, false otherwise
1719   */
1720  default boolean lt(Measure<U> o) {
1721    return compareTo(o) < 0;
1722  }
1723
1724  /**
1725   * Checks if this measure is less than or equivalent to another measure of the same unit.
1726   *
1727   * @param o the other measure to compare to
1728   * @return true if this measure has an equal or lesser equivalent magnitude, false otherwise
1729   */
1730  default boolean lte(Measure<U> o) {
1731    return compareTo(o) < 0 || isEquivalent(o);
1732  }
1733
1734  /**
1735   * Returns the measure with the absolute value closest to positive infinity.
1736   *
1737   * @param <U> the type of the units of the measures
1738   * @param measures the set of measures to compare
1739   * @return the measure with the greatest positive magnitude, or null if no measures were provided
1740   */
1741  @SafeVarargs
1742  static <U extends Unit> Measure<U> max(Measure<U>... measures) {
1743    if (measures.length == 0) {
1744      return null; // nothing to compare
1745    }
1746
1747    Measure<U> max = null;
1748    for (Measure<U> measure : measures) {
1749      if (max == null || measure.gt(max)) {
1750        max = measure;
1751      }
1752    }
1753
1754    return max;
1755  }
1756
1757  /**
1758   * Returns the measure with the absolute value closest to negative infinity.
1759   *
1760   * @param <U> the type of the units of the measures
1761   * @param measures the set of measures to compare
1762   * @return the measure with the greatest negative magnitude
1763   */
1764  @SafeVarargs
1765  static <U extends Unit> Measure<U> min(Measure<U>... measures) {
1766    if (measures.length == 0) {
1767      return null; // nothing to compare
1768    }
1769
1770    Measure<U> max = null;
1771    for (Measure<U> measure : measures) {
1772      if (max == null || measure.lt(max)) {
1773        max = measure;
1774      }
1775    }
1776
1777    return max;
1778  }
1779
1780  /**
1781   * Returns a string representation of this measurement in a shorthand form. The symbol of the
1782   * backing unit is used, rather than the full name, and the magnitude is represented in scientific
1783   * notation.
1784   *
1785   * @return the short form representation of this measurement
1786   */
1787  default String toShortString() {
1788    // eg 1.234e+04 V/m (1234 Volt per Meter in long form)
1789    return String.format("%.3e %s", magnitude(), unit().symbol());
1790  }
1791
1792  /**
1793   * Returns a string representation of this measurement in a longhand form. The name of the backing
1794   * unit is used, rather than its symbol, and the magnitude is represented in a full string, not
1795   * scientific notation. (Very large values may be represented in scientific notation, however)
1796   *
1797   * @return the long form representation of this measurement
1798   */
1799  default String toLongString() {
1800    // eg 1234 Volt per Meter (1.234e+04 V/m in short form)
1801    return String.format("%s %s", magnitude(), unit().name());
1802  }
1803}