org.scalactic.anyvals.NegZFiniteDouble.scala Maven / Gradle / Ivy
/*
* Copyright 2001-2016 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
import scala.language.implicitConversions
import scala.util.{Try, Success, Failure}
import org.scalactic.{Validation, Pass, Fail}
import org.scalactic.{Or, Good, Bad}
/**
* An AnyVal
for finite non-positive Double
s.
*
*
*
*
*
*
* Because NegZFiniteDouble
is an AnyVal
it
* will usually be as efficient as an Double
, being
* boxed only when a Double
would have been boxed.
*
*
*
* The NegZFiniteDouble.apply
factory method is
* implemented in terms of a macro that checks literals for
* validity at compile time. Calling
* NegZFiniteDouble.apply
with a literal
* Double
value will either produce a valid
* NegZFiniteDouble
instance at run time or an error at
* compile time. Here's an example:
*
*
*
* scala> import anyvals._
* import anyvals._
*
* scala> NegZFiniteDouble(-1.1)
* res1: org.scalactic.anyvals.NegZFiniteDouble = NegZFiniteDouble(-1.1)
*
* scala> NegZFiniteDouble(1.1)
* <console>:14: error: NegZFiniteDouble.apply can only be invoked on a finite non-positive (i <= 0.0 && i != Double.NegativeInfinity) floating point literal, like NegZFiniteDouble(-1.1).
* NegZFiniteDouble(1.1)
* ^
*
*
*
* NegZFiniteDouble.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 NegZFiniteDouble.apply
, you'll get a
* compiler error that suggests you use a different factor
* method, NegZFiniteDouble.from
, instead:
*
*
*
* scala> val x = -1.1
* x: Double = -1.1
*
* scala> NegZFiniteDouble(x)
* <console>:15: error: NegZFiniteDouble.apply can only be invoked on a floating point literal, like NegZFiniteDouble(-1.1). Please use NegZFiniteDouble.from instead.
* NegZFiniteDouble(x)
* ^
*
*
*
* The NegZFiniteDouble.from
factory method will inspect
* the value at runtime and return an
* Option[NegZFiniteDouble]
. If the value is valid,
* NegZFiniteDouble.from
will return a
* Some[NegZFiniteDouble]
, else it will return a
* None
. Here's an example:
*
*
*
* scala> NegZFiniteDouble.from(x)
* res4: Option[org.scalactic.anyvals.NegZFiniteDouble] = Some(NegZFiniteDouble(-1.1))
*
* scala> val y = 1.1
* y: Double = 1.1
*
* scala> NegZFiniteDouble.from(y)
* res5: Option[org.scalactic.anyvals.NegZFiniteDouble] = None
*
*
*
* The NegZFiniteDouble.apply
factory method is marked
* implicit, so that you can pass literal Double
s
* into methods that require NegZFiniteDouble
, and get the
* same compile-time checking you get when calling
* NegZFiniteDouble.apply
explicitly. Here's an example:
*
*
*
* scala> def invert(pos: NegZFiniteDouble): Double = Double.MaxValue - pos
* invert: (pos: org.scalactic.anyvals.NegZFiniteDouble)Double
*
* scala> invert(1.1)
* res6: Double = 1.7976931348623157E308
*
* scala> invert(Double.MaxValue)
* res8: Double = 0.0
*
* scala> invert(1.1)
* <console>:15: error: NegZFiniteDouble.apply can only be invoked on a finite non-positive (i <= 0.0 && i != Double.NegativeInfinity) floating point literal, like NegZFiniteDouble(-1.1).
* invert(1.1)
* ^
*
*
*
*
* This example also demonstrates that the
* NegZFiniteDouble
companion object also defines implicit
* widening conversions when a similar conversion is provided in
* Scala. This makes it convenient to use a
* NegZFiniteDouble
where a Double
is
* needed. An example is the subtraction in the body of the
* invert
method defined above,
* Double.MaxValue - pos
. Although
* Double.MaxValue
is a Double
, which
* has no -
method that takes a
* NegZFiniteDouble
(the type of pos
), you
* can still subtract pos
, because the
* NegZFiniteDouble
will be implicitly widened to
* Double
.
*
*
* @param value The Double
value underlying this NegZFiniteDouble
.
*/
final class NegZFiniteDouble private (val value: Double) extends AnyVal {
/**
* A string representation of this NegZFiniteDouble
.
*/
override def toString: String = s"NegZFiniteDouble(${value.toString()})"
/**
* Converts this NegZFiniteDouble
to a Byte
.
*/
def toByte: Byte = value.toByte
/**
* Converts this NegZFiniteDouble
to a Short
.
*/
def toShort: Short = value.toShort
/**
* Converts this NegZFiniteDouble
to a Char
.
*/
def toChar: Char = value.toChar
/**
* Converts this NegZFiniteDouble
to an Int
.
*/
def toInt: Int = value.toInt
/**
* Converts this NegZFiniteDouble
to a Long
.
*/
def toLong: Long = value.toLong
/**
* Converts this NegZFiniteDouble
to a Float
.
*/
def toFloat: Float = value.toFloat
/**
* Converts this NegZFiniteDouble
to a Double
.
*/
def toDouble: Double = value.toDouble
/** Returns this value, unmodified. */
def unary_+ : NegZFiniteDouble = this
/** Returns the negation of this value. */
def unary_- : PosZFiniteDouble = PosZFiniteDouble.ensuringValid(-value)
/**
* Converts this NegZFiniteDouble
's value to a string then concatenates the given string.
*/
def +(x: String): String = s"${value.toString()}${x.toString()}"
/** 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): Double = value + x
/** Returns the sum of this value and `x`. */
def +(x: Short): Double = value + x
/** Returns the sum of this value and `x`. */
def +(x: Char): Double = value + x
/** Returns the sum of this value and `x`. */
def +(x: Int): Double = value + x
/** Returns the sum of this value and `x`. */
def +(x: Long): Double = value + x
/** Returns the sum of this value and `x`. */
def +(x: Float): Double = 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): Double = value - x
/** Returns the difference of this value and `x`. */
def -(x: Short): Double = value - x
/** Returns the difference of this value and `x`. */
def -(x: Char): Double = value - x
/** Returns the difference of this value and `x`. */
def -(x: Int): Double = value - x
/** Returns the difference of this value and `x`. */
def -(x: Long): Double = value - x
/** Returns the difference of this value and `x`. */
def -(x: Float): Double = 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): Double = value * x
/** Returns the product of this value and `x`. */
def *(x: Short): Double = value * x
/** Returns the product of this value and `x`. */
def *(x: Char): Double = value * x
/** Returns the product of this value and `x`. */
def *(x: Int): Double = value * x
/** Returns the product of this value and `x`. */
def *(x: Long): Double = value * x
/** Returns the product of this value and `x`. */
def *(x: Float): Double = 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): Double = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Short): Double = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Char): Double = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Int): Double = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Long): Double = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Float): Double = 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): Double = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Short): Double = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Char): Double = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Int): Double = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Long): Double = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Float): Double = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Double): Double = value % x
// TODO: Need Scaladoc
// Stuff from RichDouble
/**
* Returns this
if this > that
or that
otherwise.
*/
def max(that: NegZFiniteDouble): NegZFiniteDouble = if (math.max(value, that.value) == value) this else that
/**
* Returns this
if this < that
or that
otherwise.
*/
def min(that: NegZFiniteDouble): NegZFiniteDouble = if (math.min(value, that.value) == value) this else that
/**
* Indicates whether this `NegZFiniteDouble` has a value that is a whole number: it is finite and it has no fraction part.
*/
def isWhole = {
val longValue = value.toLong
longValue.toDouble == value || longValue == Long.MaxValue && value < Double.PositiveInfinity || longValue == Long.MinValue && value > Double.NegativeInfinity
}
/** 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: Double = math.toRadians(value)
/** 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: Double = math.toDegrees(value)
/**
* Applies the passed Double => Double
function to the underlying Double
* value, and if the result is positive, returns the result wrapped in a NegZFiniteDouble
,
* else throws AssertionError
.
*
*
* This method will inspect the result of applying the given function to this
* NegZFiniteDouble
's underlying Double
value and if the result
* is greater than 0.0
, it will return a NegZFiniteDouble
representing that value.
* Otherwise, the Double
value returned by the given function is
* 0.0
or negative, so this method will throw AssertionError
.
*
*
*
* This method differs from a vanilla assert
or ensuring
* call in that you get something you didn't already have if the assertion
* succeeds: a type that promises an Double
is positive.
* With this method, you are asserting that you are convinced the result of
* the computation represented by applying the given function to this NegZFiniteDouble
's
* value will not produce zero, a negative number, including Double.NegativeInfinity
, or Double.NaN
.
* Instead of producing such invalid values, this method will throw AssertionError
.
*
*
* @param f the Double => Double
function to apply to this NegZFiniteDouble
's
* underlying Double
value.
* @return the result of applying this NegZFiniteDouble
's underlying Double
value to
* to the passed function, wrapped in a NegZFiniteDouble
if it is positive (else throws AssertionError
).
* @throws AssertionError if the result of applying this NegZFiniteDouble
's underlying Double
value to
* to the passed function is not positive.
*/
def ensuringValid(f: Double => Double): NegZFiniteDouble = {
val candidateResult: Double = f(value)
if (NegZFiniteDoubleMacro.isValid(candidateResult)) new NegZFiniteDouble(candidateResult)
else throw new AssertionError(s"${candidateResult.toString()}, the result of applying the passed function to ${value.toString()}, was not a valid NegZFiniteDouble")
}
/**
* Rounds this `NegZFiniteDouble` value to the nearest whole number value that can be expressed as an `NegZLong`, returning the result as a `NegZLong`.
*/
def round: NegZLong = NegZLong.ensuringValid(math.round(value))
/**
* Returns the smallest (closest to 0) `NegZFiniteDouble` that is greater than or equal to this `NegZFiniteDouble`
* and represents a mathematical integer.
*/
def ceil: NegZFiniteDouble = NegZFiniteDouble.ensuringValid(math.ceil(value).toDouble)
/**
* Returns the greatest (closest to infinity) `NegZFiniteDouble` that is less than or equal to
* this `NegZFiniteDouble` and represents a mathematical integer.
*/
def floor: NegZFiniteDouble = NegZFiniteDouble.ensuringValid(math.floor(value).toDouble)
}
/**
* The companion object for NegZFiniteDouble
that offers
* factory methods that produce NegZFiniteDouble
s,
* implicit widening conversions from NegZFiniteDouble
to
* other numeric types, and maximum and minimum constant values
* for NegZFiniteDouble
.
*/
object NegZFiniteDouble {
/**
* The largest value representable as a finite non-positive Double
,
* which is NegZFiniteDouble(0.0)
.
*/
final val MaxValue: NegZFiniteDouble = NegZFiniteDouble.ensuringValid(0.0)
/**
* The smallest value representable as a finite non-positive
* Double
, which is NegZFiniteDouble(-1.7976931348623157E308)
.
*/
final val MinValue: NegZFiniteDouble = NegZFiniteDouble.ensuringValid(Double.MinValue) // Can't use the macro here
/**
* A factory method that produces an Option[NegZFiniteDouble]
given a
* Double
value.
*
*
* This method will inspect the passed Double
value and if
* it is a finite non-positive Double
, it will return a NegZFiniteDouble
* representing that value, wrapped in a Some
. Otherwise, the passed Double
* value is not finite non-positive, 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 Double
literals at
* compile time, whereas from
inspects
* Double
values at run time.
*
*
* @param value the Double
to inspect, and if finite non-positive, return
* wrapped in a Some[NegZFiniteDouble]
.
* @return the specified Double
value wrapped in a
* Some[NegZFiniteDouble]
, if it is NegZFiniteDouble, else
* None
.
*/
def from(value: Double): Option[NegZFiniteDouble] =
if (NegZFiniteDoubleMacro.isValid(value)) Some(new NegZFiniteDouble(value)) else None
/**
* A factory/assertion method that produces a NegZFiniteDouble
given a
* valid Double
value, or throws AssertionError
,
* if given an invalid Double
value.
*
*
* This method will inspect the passed Double
value and if
* it is a finite non-positive Double
, it will return a NegZFiniteDouble
* representing that value. Otherwise, the passed Double
value is not finite non-positive,
* so this method will throw AssertionError
.
*
*
*
* This factory method differs from the apply
* factory method in that apply
is implemented
* via a macro that inspects Double
literals at
* compile time, whereas from
inspects
* Double
values at run time.
* It differs from a vanilla assert
or ensuring
* call in that you get something you didn't already have if the assertion
* succeeds: a type that promises a Double
is finite non-positive.
*
*
* @param value the Double
to inspect, and if finite non-positive, return
* wrapped in a NegZFiniteDouble
.
* @return the specified Double
value wrapped in a
* NegZFiniteDouble
, if it is finite non-positive, else
* throws AssertionError
.
* @throws AssertionError if the passed value is not finite non-positive
*/
def ensuringValid(value: Double): NegZFiniteDouble =
if (NegZFiniteDoubleMacro.isValid(value)) new NegZFiniteDouble(value) else {
throw new AssertionError(s"${value.toString()} was not a valid NegZFiniteDouble")
}
/**
* A factory/validation method that produces a NegZFiniteDouble
, wrapped
* in a Success
, given a valid Float
value, or if the
* given Float
is invalid, an AssertionError
, wrapped
* in a Failure
.
*
* Note: you should use this method only when you are convinced that it will
* always succeed, i.e., never throw an exception. It is good practice to
* add a comment near the invocation of this method indicating ''why'' you think
* it will always succeed to document your reasoning. If you are not sure an
* `ensuringValid` call will always succeed, you should use one of the other
* factory or validation methods provided on this object instead: `isValid`,
* `tryingValid`, `passOrElse`, `goodOrElse`, or `rightOrElse`.
*
*
* This method will inspect the passed Float
value and if
* it is a finite non-positive Float
, it will return a NegZFiniteDouble
* representing that value, wrapped in a Success
.
* Otherwise, the passed Float
value is not finite non-positive, so this
* method will return an AssertionError
, wrapped in a Failure
.
*
*
*
* 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 this method inspects
* Float
values at run time.
*
*
* @param value the Float
to inspect, and if finite non-positive, return
* wrapped in a Success(NegZFiniteDouble)
.
* @return the specified Float
value wrapped
* in a Success(NegZFiniteDouble)
, if it is finite non-positive, else a Failure(AssertionError)
.
*/
def tryingValid(value: Double): Try[NegZFiniteDouble] =
if (NegZFiniteDoubleMacro.isValid(value))
Success(new NegZFiniteDouble(value))
else
Failure(new AssertionError(s"${value.toString()} was not a valid NegZFiniteDouble"))
/**
* A validation method that produces a Pass
* given a valid Double
value, or
* an error value of type E
produced by passing the
* given invalid Int
value
* to the given function f
, wrapped in a Fail
.
*
*
* This method will inspect the passed Double
value and if
* it is a finite non-positive Double
, it will return a Pass
.
* Otherwise, the passed Double
value is finite non-positive, so this
* method will return a result of type E
obtained by passing
* the invalid Double
value to the given function f
,
* wrapped in a `Fail`.
*
*
*
* This factory method differs from the apply
factory method
* in that apply
is implemented via a macro that inspects
* Double
literals at compile time, whereas this method inspects
* Double
values at run time.
*
*
* @param value the `Int` to validate that it is finite non-positive.
* @return a `Pass` if the specified `Int` value is finite non-positive,
* else a `Fail` containing an error value produced by passing the
* specified `Double` to the given function `f`.
*/
def passOrElse[E](value: Double)(f: Double => E): Validation[E] =
if (NegZFiniteDoubleMacro.isValid(value)) Pass else Fail(f(value))
/**
* A factory/validation method that produces a NegZFiniteDouble
, wrapped
* in a Good
, given a valid Double
value, or if the
* given Double
is invalid, an error value of type B
* produced by passing the given invalid Double
value
* to the given function f
, wrapped in a Bad
.
*
*
* This method will inspect the passed Double
value and if
* it is a finite non-positive Double
, it will return a NegZFiniteDouble
* representing that value, wrapped in a Good
.
* Otherwise, the passed Double
value is not finite non-positive, so this
* method will return a result of type B
obtained by passing
* the invalid Double
value to the given function f
,
* wrapped in a `Bad`.
*
*
*
* This factory method differs from the apply
factory method
* in that apply
is implemented via a macro that inspects
* Double
literals at compile time, whereas this method inspects
* Double
values at run time.
*
*
* @param value the Double
to inspect, and if finite non-positive, return
* wrapped in a Good(NegZFiniteDouble)
.
* @return the specified Double
value wrapped
* in a Good(NegZFiniteDouble)
, if it is finite non-positive, else a Bad(f(value))
.
*/
def goodOrElse[B](value: Double)(f: Double => B): NegZFiniteDouble Or B =
if (NegZFiniteDoubleMacro.isValid(value)) Good(NegZFiniteDouble.ensuringValid(value)) else Bad(f(value))
/**
* A factory/validation method that produces a NegZFiniteDouble
, wrapped
* in a Right
, given a valid Double
value, or if the
* given Double
is invalid, an error value of type L
* produced by passing the given invalid Double
value
* to the given function f
, wrapped in a Left
.
*
*
* This method will inspect the passed Double
value and if
* it is a finite non-positive Double
, it will return a NegZFiniteDouble
* representing that value, wrapped in a Right
.
* Otherwise, the passed Double
value is not finite non-positive, so this
* method will return a result of type L
obtained by passing
* the invalid Double
value to the given function f
,
* wrapped in a `Left`.
*
*
*
* This factory method differs from the apply
factory method
* in that apply
is implemented via a macro that inspects
* Double
literals at compile time, whereas this method inspects
* Double
values at run time.
*
*
* @param value the Double
to inspect, and if finite non-positive, return
* wrapped in a Right(NegZFiniteDouble)
.
* @return the specified Double
value wrapped
* in a Right(NegZFiniteDouble)
, if it is finite non-positive, else a Left(f(value))
.
*/
def rightOrElse[L](value: Double)(f: Double => L): Either[L, NegZFiniteDouble] =
if (NegZFiniteDoubleMacro.isValid(value)) Right(NegZFiniteDouble.ensuringValid(value)) else Left(f(value))
/**
* A predicate method that returns true if a given
* Double
value is finite non-positive.
*
* @param value the Double
to inspect, and if finite non-positive, return true.
* @return true if the specified Double
is positive, else false.
*/
def isValid(value: Double): Boolean = NegZFiniteDoubleMacro.isValid(value)
/**
* A factory method that produces a NegZFiniteDouble
given a
* Double
value and a default NegZFiniteDouble
.
*
*
* This method will inspect the passed Double
value and if
* it is a finite non-positive Double
, it will return a NegZFiniteDouble
* representing that value. Otherwise, the passed Double
value is finite non-positive,
* so this method will return the passed default
value.
*
*
*
* This factory method differs from the apply
* factory method in that apply
is implemented
* via a macro that inspects Double
literals at
* compile time, whereas from
inspects
* Double
values at run time.
*
*
* @param value the Double
to inspect, and if finite non-positive, return.
* @param default the NegZFiniteDouble
to return if the passed
* Double
value is not finite non-positive.
* @return the specified Double
value wrapped in a
* NegZFiniteDouble
, if it is finite non-positive, else the
* default
NegZFiniteDouble
value.
*/
def fromOrElse(value: Double, default: => NegZFiniteDouble): NegZFiniteDouble =
if (NegZFiniteDoubleMacro.isValid(value)) new NegZFiniteDouble(value) else default
import language.experimental.macros
import scala.language.implicitConversions
/**
* A factory method, implemented via a macro, that produces a
* NegZFiniteDouble
if passed a valid Double
* literal, otherwise a compile time error.
*
*
* The macro that implements this method will inspect the
* specified Double
expression at compile time. If
* the expression is a finite non-positive Double
literal,
* it will return a NegZFiniteDouble
representing that value.
* Otherwise, the passed Double
expression is either a literal
* that is not finite non-positive, 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 Double
literals at compile
* time, whereas from
inspects Double
* values at run time.
*
*
* @param value the Double
literal expression to
* inspect at compile time, and if finite non-positive, to return
* wrapped in a NegZFiniteDouble
at run time.
* @return the specified, valid Double
literal
* value wrapped in a NegZFiniteDouble
. (If the
* specified expression is not a valid Double
* literal, the invocation of this method will not
* compile.)
*/
inline implicit def apply(value: => Double): NegZFiniteDouble = ${ NegZFiniteDoubleMacro('{value}) }
/**
* Implicit widening conversion from NegZFiniteDouble
to
* Double
.
*
* @param pos the NegZFiniteDouble
to widen
* @return the Double
value underlying the specified
* NegZFiniteDouble
*/
implicit def widenToDouble(pos: NegZFiniteDouble): Double = pos.value
/**
* Implicit widening conversion from NegZFiniteDouble
to NegZDouble
.
*
* @param pos the NegZFiniteDouble
to widen
* @return the Double
value underlying the specified NegZFiniteDouble
,
* widened to Double
and wrapped in a NegZDouble
.
*/
implicit def widenToNegZDouble(pos: NegZFiniteDouble): NegZDouble = NegZDouble.ensuringValid(pos.value)
/**
* Implicit Ordering instance.
*/
implicit val ordering: Ordering[NegZFiniteDouble] =
new Ordering[NegZFiniteDouble] {
def compare(x: NegZFiniteDouble, y: NegZFiniteDouble): Int = x.toDouble.compare(y.toDouble)
}
}