org.scalactic.anyvals.PosZFloat.scala Maven / Gradle / Ivy
/*
* Copyright 2001-2014 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalactic.anyvals
import scala.collection.immutable.NumericRange
/**
* An AnyVal
for non-negative Float
s.
*
*
* Because PosZFloat
is an AnyVal
it will usually be
* as efficient as an Float
, being boxed only when a
* Float
would have been boxed.
*
*
*
* The PosZFloat.apply
factory method is
* implemented in terms of a macro that checks literals for
* validity at compile time. Calling
* PosZFloat.apply
with a literal
* Float
value will either produce a valid
* PosZFloat
instance at run time or an error at
* compile time. Here's an example:
*
*
*
* scala> import anyvals._
* import anyvals._
*
* scala> PosZFloat(1.1F)
* res0: org.scalactic.anyvals.PosZFloat = PosZFloat(1.1)
*
* scala> PosZFloat(0.0F)
* res1: org.scalactic.anyvals.PosZFloat = PosZFloat(0.0)
*
* scala> PosZFloat(-1.1F)
* <console>:14: error: PosZFloat.apply can only be invoked on a non-negative (i >= 0.0F) floating point literal, like PosZFloat(42.0F).
* PosZFloat(-1.1F)
* ^
*
*
*
* PosZFloat.apply
cannot be used if the value
* being passed is a variable (i.e., not a literal),
* because the macro cannot determine the validity of variables
* at compile time (just literals). If you try to pass a
* variable to PosZFloat.apply
, you'll get a
* compiler error that suggests you use a different factor
* method, PosZFloat.from
, instead:
*
*
*
* scala> val x = 1.1F
* x: Float = 1.1
*
* scala> PosZFloat(x)
* <console>:15: error: PosZFloat.apply can only be invoked on a floating point literal, like PosZFloat(42.0F). Please use PosZFloat.from instead.
* PosZFloat(x)
* ^
*
*
*
* The PosZFloat.from
factory method will inspect
* the value at runtime and return an
* Option[PosZFloat]
. If the value is valid,
* PosZFloat.from
will return a
* Some[PosZFloat]
, else it will return a
* None
. Here's an example:
*
*
*
* scala> PosZFloat.from(x)
* res4: Option[org.scalactic.anyvals.PosZFloat] = Some(PosZFloat(1.1))
*
* scala> val y = -1.1F
* y: Float = -1.1
*
* scala> PosZFloat.from(y)
* res5: Option[org.scalactic.anyvals.PosZFloat] = None
*
*
*
* The PosZFloat.apply
factory method is marked implicit, so that
* you can pass literal Float
s into methods that require
* PosZFloat
, and get the same compile-time checking you get when
* calling PosZFloat.apply
explicitly. Here's an example:
*
*
*
* scala> def invert(pos: PosZFloat): Float = Float.MaxValue - pos
* invert: (pos: org.scalactic.anyvals.PosZFloat)Float
*
* scala> invert(0.0F)
* res6: Float = 3.4028235E38
*
* scala> invert(Float.MaxValue)
* res7: Float = 0.0
*
* scala> invert(-1.1F)
* <console>:15: error: PosZFloat.apply can only be invoked on a non-negative (i >= 0.0F) floating point literal, like PosZFloat(42.0F).
* invert(-1.1F)
* ^
*
*
*
* This example also demonstrates that the
* PosZFloat
companion object also defines
* implicit widening conversions when a similar conversion is
* provided in Scala. This makes it convenient to use a
* PosZFloat
where a Float
or wider
* type is needed. An example is the subtraction in the body of
* the invert
method defined above,
* Float.MaxValue - pos
. Although
* Float.MaxValue
is an Float
, which
* has no -
method that takes a
* PosZFloat
(the type of pos
), you
* can still subtract pos
, because the
* PosZFloat
will be implicitly widened to
* Float
.
*
*
* @param value The Float
value underlying this PosZFloat
.
*/
final class PosZFloat private (val value: Float) extends AnyVal {
/**
* A string representation of this PosZFloat
.
*/
override def toString: String = s"PosZFloat($value)"
/**
* Converts this PosZFloat
to a Byte
.
*/
def toByte: Byte = value.toByte
/**
* Converts this PosZFloat
to a Short
.
*/
def toShort: Short = value.toShort
/**
* Converts this PosZFloat
to a Char
.
*/
def toChar: Char = value.toChar
/**
* Converts this PosZFloat
to an Int
.
*/
def toInt: Int = value.toInt
/**
* Converts this PosZFloat
to a Long
.
*/
def toLong: Long = value.toLong
/**
* Converts this PosZFloat
to a Float
.
*/
def toFloat: Float = value.toFloat
/**
* Converts this PosZFloat
to a Double
.
*/
def toDouble: Double = value.toDouble
/** Returns this value, unmodified. */
def unary_+ : PosZFloat = this
/** Returns the negation of this value. */
def unary_- : Float = -value
/**
* Converts this PosZFloat
's value to a string then concatenates the given string.
*/
def +(x: String): String = value + x
/** Returns `true` if this value is less than x, `false` otherwise. */
def <(x: Byte): Boolean = value < x
/** Returns `true` if this value is less than x, `false` otherwise. */
def <(x: Short): Boolean = value < x
/** Returns `true` if this value is less than x, `false` otherwise. */
def <(x: Char): Boolean = value < x
/** Returns `true` if this value is less than x, `false` otherwise. */
def <(x: Int): Boolean = value < x
/** Returns `true` if this value is less than x, `false` otherwise. */
def <(x: Long): Boolean = value < x
/** Returns `true` if this value is less than x, `false` otherwise. */
def <(x: Float): Boolean = value < x
/** Returns `true` if this value is less than x, `false` otherwise. */
def <(x: Double): Boolean = value < x
/** Returns `true` if this value is less than or equal to x, `false` otherwise. */
def <=(x: Byte): Boolean = value <= x
/** Returns `true` if this value is less than or equal to x, `false` otherwise. */
def <=(x: Short): Boolean = value <= x
/** Returns `true` if this value is less than or equal to x, `false` otherwise. */
def <=(x: Char): Boolean = value <= x
/** Returns `true` if this value is less than or equal to x, `false` otherwise. */
def <=(x: Int): Boolean = value <= x
/** Returns `true` if this value is less than or equal to x, `false` otherwise. */
def <=(x: Long): Boolean = value <= x
/** Returns `true` if this value is less than or equal to x, `false` otherwise. */
def <=(x: Float): Boolean = value <= x
/** Returns `true` if this value is less than or equal to x, `false` otherwise. */
def <=(x: Double): Boolean = value <= x
/** Returns `true` if this value is greater than x, `false` otherwise. */
def >(x: Byte): Boolean = value > x
/** Returns `true` if this value is greater than x, `false` otherwise. */
def >(x: Short): Boolean = value > x
/** Returns `true` if this value is greater than x, `false` otherwise. */
def >(x: Char): Boolean = value > x
/** Returns `true` if this value is greater than x, `false` otherwise. */
def >(x: Int): Boolean = value > x
/** Returns `true` if this value is greater than x, `false` otherwise. */
def >(x: Long): Boolean = value > x
/** Returns `true` if this value is greater than x, `false` otherwise. */
def >(x: Float): Boolean = value > x
/** Returns `true` if this value is greater than x, `false` otherwise. */
def >(x: Double): Boolean = value > x
/** Returns `true` if this value is greater than or equal to x, `false` otherwise. */
def >=(x: Byte): Boolean = value >= x
/** Returns `true` if this value is greater than or equal to x, `false` otherwise. */
def >=(x: Short): Boolean = value >= x
/** Returns `true` if this value is greater than or equal to x, `false` otherwise. */
def >=(x: Char): Boolean = value >= x
/** Returns `true` if this value is greater than or equal to x, `false` otherwise. */
def >=(x: Int): Boolean = value >= x
/** Returns `true` if this value is greater than or equal to x, `false` otherwise. */
def >=(x: Long): Boolean = value >= x
/** Returns `true` if this value is greater than or equal to x, `false` otherwise. */
def >=(x: Float): Boolean = value >= x
/** Returns `true` if this value is greater than or equal to x, `false` otherwise. */
def >=(x: Double): Boolean = value >= x
/** Returns the sum of this value and `x`. */
def +(x: Byte): Float = value + x
/** Returns the sum of this value and `x`. */
def +(x: Short): Float = value + x
/** Returns the sum of this value and `x`. */
def +(x: Char): Float = value + x
/** Returns the sum of this value and `x`. */
def +(x: Int): Float = value + x
/** Returns the sum of this value and `x`. */
def +(x: Long): Float = value + x
/** Returns the sum of this value and `x`. */
def +(x: Float): Float = value + x
/** Returns the sum of this value and `x`. */
def +(x: Double): Double = value + x
/** Returns the difference of this value and `x`. */
def -(x: Byte): Float = value - x
/** Returns the difference of this value and `x`. */
def -(x: Short): Float = value - x
/** Returns the difference of this value and `x`. */
def -(x: Char): Float = value - x
/** Returns the difference of this value and `x`. */
def -(x: Int): Float = value - x
/** Returns the difference of this value and `x`. */
def -(x: Long): Float = value - x
/** Returns the difference of this value and `x`. */
def -(x: Float): Float = value - x
/** Returns the difference of this value and `x`. */
def -(x: Double): Double = value - x
/** Returns the product of this value and `x`. */
def *(x: Byte): Float = value * x
/** Returns the product of this value and `x`. */
def *(x: Short): Float = value * x
/** Returns the product of this value and `x`. */
def *(x: Char): Float = value * x
/** Returns the product of this value and `x`. */
def *(x: Int): Float = value * x
/** Returns the product of this value and `x`. */
def *(x: Long): Float = value * x
/** Returns the product of this value and `x`. */
def *(x: Float): Float = value * x
/** Returns the product of this value and `x`. */
def *(x: Double): Double = value * x
/** Returns the quotient of this value and `x`. */
def /(x: Byte): Float = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Short): Float = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Char): Float = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Int): Float = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Long): Float = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Float): Float = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Double): Double = value / x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Byte): Float = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Short): Float = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Char): Float = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Int): Float = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Long): Float = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Float): Float = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Double): Double = value % x
// Stuff from RichFloat
def isPosInfinity: Boolean = Float.PositiveInfinity == value
/**
* Returns this
if this > that
or that
otherwise.
*/
def max(that: PosZFloat): PosZFloat = if (math.max(value, that.value) == value) this else that
/**
* Returns this
if this < that
or that
otherwise.
*/
def min(that: PosZFloat): PosZFloat = if (math.min(value, that.value) == value) this else that
def isWhole = {
val longValue = value.toLong
longValue.toFloat == value || longValue == Long.MaxValue && value < Float.PositiveInfinity || longValue == Long.MinValue && value > Float.NegativeInfinity
}
def round: PosZInt = PosZInt.from(math.round(value)).get
def ceil: PosZFloat = PosZFloat.from(math.ceil(value).toFloat).get
def floor: PosZFloat = PosZFloat.from(math.floor(value).toFloat).get
/** Converts an angle measured in degrees to an approximately equivalent
* angle measured in radians.
*
* @return the measurement of the angle x in radians.
*/
def toRadians: PosZFloat = PosZFloat.from(math.toRadians(value).toFloat).get
/** Converts an angle measured in radians to an approximately equivalent
* angle measured in degrees.
* @return the measurement of the angle x in degrees.
*/
def toDegrees: PosZFloat = PosZFloat.from(math.toDegrees(value).toFloat).get
// adapted from RichInt:
/**
* Create a Range
from this PosZFloat
value
* until the specified end
(exclusive) with step value 1.
*
* @param end The final bound of the range to make.
* @return A [[scala.collection.immutable.Range.Partial[Float, NumericRange[Float]]]] from `this` up to but
* not including `end`.
*/
def until(end: Float): Range.Partial[Float, NumericRange[Float]] =
value.until(end)
/**
* Create a Range
from this PosZFloat
value
* until the specified end
(exclusive) with the specified step
value.
*
* @param end The final bound of the range to make.
* @param end The final bound of the range to make.
* @param step The number to increase by for each step of the range.
* @return A [[scala.collection.immutable.NumericRange.Exclusive[Float]]] from `this` up to but
* not including `end`.
*/
def until(end: Float, step: Float): NumericRange.Exclusive[Float] =
value.until(end, step)
/**
* Create an inclusive Range
from this PosZFloat
value
* to the specified end
with step value 1.
*
* @param end The final bound of the range to make.
* @return A [[scala.collection.immutable.Range.Partial[Float, NumericRange[Float]]]] from `'''this'''` up to
* and including `end`.
*/
def to(end: Float): Range.Partial[Float, NumericRange[Float]] =
value.to(end)
/**
* Create an inclusive Range
from this PosZFloat
value
* to the specified end
with the specified step
value.
*
* @param end The final bound of the range to make.
* @param step The number to increase by for each step of the range.
* @return A [[scala.collection.immutable.NumericRange.Inclusive[Float]]] from `'''this'''` up to
* and including `end`.
*/
def to(end: Float, step: Float): NumericRange.Inclusive[Float] =
value.to(end, step)
}
/**
* The companion object for PosZFloat
that offers
* factory methods that produce PosZFloat
s, implicit
* widening conversions from PosZFloat
to other
* numeric types, and maximum and minimum constant values for
* PosZFloat
.
*/
object PosZFloat {
/**
* The largest value representable as a non-negative Float
,
* which is PosZFloat(3.4028235E38)
.
*/
final val MaxValue: PosZFloat = PosZFloat.from(Float.MaxValue).get
/**
* The smallest value representable as a non-negative Float
,
* which is PosZFloat(0.0F)
.
*/
final val MinValue: PosZFloat = PosZFloat.from(0.0f).get // Can't use the macro here
/**
* A factory method that produces an Option[PosZFloat]
given a
* Float
value.
*
*
* This method will inspect the passed Float
value
* and if it is a non-negative Float
,
* i.e., a value greater than or equal to 0, it will
* return a PosZFloat
representing that value,
* wrapped in a Some
. Otherwise, the passed
* Float
value is negative, so this method
* will return None
.
*
*
*
* This factory method differs from the apply
* factory method in that apply
is implemented
* via a macro that inspects Float
literals at
* compile time, whereas from
inspects
* Float
values at run time.
*
*
* @param value the Float
to inspect, and if non-negative, return
* wrapped in a Some[PosZFloat]
.
* @return the specified Float
value wrapped
* in a Some[PosZFloat]
, if it is positive, else
* None
.
*/
def from(value: Float): Option[PosZFloat] =
if (value >= 0.0F) Some(new PosZFloat(value)) else None
import language.experimental.macros
import scala.language.implicitConversions
/**
* A factory method, implemented via a macro, that produces a
* PosZFloat
if passed a valid Float
* literal, otherwise a compile time error.
*
*
* The macro that implements this method will inspect the
* specified Float
expression at compile time. If
* the expression is a non-negative Float
literal,
* i.e., with a value greater than or equal to 0, it will return
* a PosZFloat
representing that value. Otherwise,
* the passed Float
expression is either a literal
* that is negative, or is not a literal, so this method
* will give a compiler error.
*
*
*
* This factory method differs from the from
* factory method in that this method is implemented via a
* macro that inspects Float
literals at compile
* time, whereas from
inspects Float
* values at run time.
*
*
* @param value the Float
literal expression to inspect at
* compile time, and if non-negative, to return wrapped in a
* PosZFloat
at run time.
* @return the specified, valid Float
literal
* value wrapped in a PosZFloat
. (If the
* specified expression is not a valid Float
* literal, the invocation of this method will not
* compile.)
*/
implicit def apply(value: Float): PosZFloat = macro PosZFloatMacro.apply
/**
* Implicit widening conversion from PosZFloat
to
* Float
.
*
* @param pos the PosZFloat
to widen
* @return the Float
value underlying the specified
* PosZFloat
.
*/
implicit def widenToFloat(poz: PosZFloat): Float = poz.value
/**
* Implicit widening conversion from PosZFloat
to
* Double
.
*
* @param pos the PosZFloat
to widen
* @return the Float
value underlying the specified
* PosZFloat
, widened to Double
.
*/
implicit def widenToDouble(poz: PosZFloat): Double = poz.value
/**
* Implicit widening conversion from PosZFloat
to
* PosZDouble
.
*
* @param pos the PosZFloat
to widen
* @return the Float
value underlying the specified
* PosZFloat
, widened to Double
* and wrapped in a PosZDouble
.
*/
implicit def widenToPosZDouble(poz: PosZFloat): PosZDouble = PosZDouble.from(poz.value).get
/**
* Implicit Ordering instance.
*/
implicit val posZFloatOrd: Ordering[PosZFloat] =
new Ordering[PosZFloat] {
def compare(x: PosZFloat, y: PosZFloat): Int = x.toFloat.compare(y)
}
}