Skip to content

Instantly share code, notes, and snippets.

@romainmenke
Created December 14, 2015 17:59
Show Gist options
  • Save romainmenke/93332ab7f5824caafa17 to your computer and use it in GitHub Desktop.
Save romainmenke/93332ab7f5824caafa17 to your computer and use it in GitHub Desktop.
public struct OtherFloat {
public var value: Float
/// Create an instance initialized to zero.
public init() {
self.value = 0
}
/// Create an instance initialized to `value`.
public init(_ value: Float) {
self.value = value
}
public init(_ value: OtherFloat) {
self.value = value.value
}
}
extension OtherFloat : CustomStringConvertible {
/// A textual representation of `self`.
public var description: String {
return String(value)
}
}
extension OtherFloat : FloatingPointType {
public typealias _BitsType = UInt32
public static func _fromBitPattern(bits: _BitsType) -> OtherFloat {
let float = Float(bits)
return OtherFloat(float)
}
public func _toBitPattern() -> _BitsType {
return UInt32(self.value)
}
/// The positive infinity.
public static var infinity: OtherFloat { return OtherFloat(Float.infinity) }
/// A quiet NaN.
public static var NaN: OtherFloat { return OtherFloat(Float.NaN) }
/// A quiet NaN.
public static var quietNaN: OtherFloat { return OtherFloat(Float.quietNaN) }
/// `true` if `self` is negative.
public var isSignMinus: Bool { return (self.value.isSignMinus) }
/// `true` if `self` is normal (not zero, subnormal, infinity, or
/// NaN).
public var isNormal: Bool { return (self.value.isNormal) }
/// `true` if `self` is zero, subnormal, or normal (not infinity
/// or NaN).
public var isFinite: Bool { return (self.value.isFinite) }
/// `true` if `self` is +0.0 or -0.0.
public var isZero: Bool { return (self.value.isZero) }
/// `true` if `self` is subnormal.
public var isSubnormal: Bool { return (self.value.isSubnormal) }
/// `true` if `self` is infinity.
public var isInfinite: Bool { return (self.value.isInfinite) }
/// `true` if `self` is NaN.
public var isNaN: Bool { return (self.value.isNaN) }
/// `true` if `self` is a signaling NaN.
public var isSignaling: Bool { return (self.value.isSignaling) }
}
extension OtherFloat {
/// The IEEE 754 "class" of this type.
public var floatingPointClass: FloatingPointClassification { return value.floatingPointClass }
}
extension OtherFloat : IntegerLiteralConvertible {
public init(_builtinIntegerLiteral value: IntegerLiteralType) {
self.value = Float(value.toIntMax())
}
/// Create an instance initialized to `value`.
public init(integerLiteral value: Int64) {
self.value = Float(value.toIntMax())
}
}
extension OtherFloat {
public init(_builtinFloatLiteral value: Float) {
self.value = value
}
}
extension OtherFloat : FloatLiteralConvertible {
/// Create an instance initialized to `value`.
public init(floatLiteral value: Float) {
self.value = value
}
}
extension OtherFloat : Comparable, Equatable {
}
public func ==(lhs:OtherFloat,rhs:OtherFloat) -> Bool {
return lhs.value == rhs.value
}
public func !=(lhs:OtherFloat,rhs:OtherFloat) -> Bool {
return !(lhs == rhs)
}
public func <(lhs:OtherFloat,rhs:OtherFloat) -> Bool {
return lhs.value < rhs.value
}
public func <=(lhs:OtherFloat,rhs:OtherFloat) -> Bool {
return lhs.value <= rhs.value
}
public func >(lhs:OtherFloat,rhs:OtherFloat) -> Bool {
return lhs.value > rhs.value
}
public func >=(lhs:OtherFloat,rhs:OtherFloat) -> Bool {
return lhs.value >= rhs.value
}
extension OtherFloat : Hashable {
/// The hash value.
///
/// **Axiom:** `x == y` implies `x.hashValue == y.hashValue`.
///
/// - Note: The hash value is not guaranteed to be stable across
/// different invocations of the same program. Do not persist the
/// hash value across program runs.
public var hashValue: Int { return value.hashValue }
}
extension OtherFloat : AbsoluteValuable {
/// Returns the absolute value of `x`.
@warn_unused_result
public static func abs(x: OtherFloat) -> OtherFloat {
let absValue : Float = Float.abs(x.value)
return OtherFloat(absValue)
}
}
extension OtherFloat : SignedNumberType {
}
@warn_unused_result
prefix public func -(x: OtherFloat) -> OtherFloat {
return OtherFloat(-x.value)
}
extension OtherFloat {
public init(_ v: UInt8) {
value = Float(v.toIntMax())
}
public init(_ v: Int8) {
value = Float(v.toIntMax())
}
public init(_ v: UInt16) {
value = Float(v.toIntMax())
}
public init(_ v: Int16) {
value = Float(v.toIntMax())
}
public init(_ v: UInt32) {
value = Float(v.toIntMax())
}
public init(_ v: Int32) {
value = Float(v.toIntMax())
}
public init(_ v: UInt64) {
value = Float(v.toIntMax())
}
public init(_ v: Int64) {
value = Float(v.toIntMax())
}
public init(_ v: UInt) {
value = Float(v.toIntMax())
}
public init(_ v: Int) {
value = Float(v.toIntMax())
}
}
extension OtherFloat {
/// Construct an instance that approximates `other`.
public init(_ other: Double) {
value = Float(other)
}
/// Construct an instance that approximates `other`.
public init(_ other: Float80) {
value = Float(other)
}
}
extension OtherFloat : Strideable {
/// Returns a stride `x` such that `self.advancedBy(x)` approximates
/// `other`.
///
/// - Complexity: O(1).
public func distanceTo(other: OtherFloat) -> OtherFloat {
let dis = self.value.distanceTo(other.value)
return OtherFloat(dis)
}
/// Returns a `Self` `x` such that `self.distanceTo(x)` approximates
/// `n`.
///
/// - Complexity: O(1).
public func advancedBy(amount: OtherFloat) -> OtherFloat {
let adv = self.value.advancedBy(amount.value)
return OtherFloat(adv)
}
}
extension OtherFloat {
/// Construct from an ASCII representation.
///
/// Returns the result of calling the POSIX function
/// `strtof_l` using the "C" locale, unless
/// `text` contains non-ASCII text or whitespace, or is not
/// completely consumed by the call. Otherwise, returns `nil`.
///
/// See the `strtof (3)` man page for details of
/// the exact format accepted.
public init?(_ text: String) {
if let floatValue = Float(text) {
self.value = floatValue
} else {
return nil
}
}
}
// maths
public func /(lhs:OtherFloat,rhs:OtherFloat) -> OtherFloat {
return OtherFloat(lhs.value / rhs.value)
}
public func *(lhs:OtherFloat,rhs:OtherFloat) -> OtherFloat {
return OtherFloat(lhs.value * rhs.value)
}
public func +(lhs:OtherFloat,rhs:OtherFloat) -> OtherFloat {
return OtherFloat(lhs.value + rhs.value)
}
public func -(lhs:OtherFloat,rhs:OtherFloat) -> OtherFloat {
return OtherFloat(lhs.value - rhs.value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment